所以我正在编写一个类的程序,它需要通过命令提示符运行。 IE javac filename.java然后是java文件名。我在eclipse中编写了代码,这就是我遇到麻烦的原因。我的代码如下:
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.Font;
public class QA extends JApplet implements ActionListener{
int y=0;
int x=0;
int a=0;
int b=0;
static int q=-1;
JButton btnYes = new JButton("YES");
JButton btnNo = new JButton("NO");
static JLabel lblNewLabel = new JLabel("Use the buttons to answer this question: Do you like pizza?");
public QA() {
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("220px"),
FormFactory.UNRELATED_GAP_COLSPEC,
ColumnSpec.decode("220px"),},
new RowSpec[] {
RowSpec.decode("97px"),
RowSpec.decode("50px"),
RowSpec.decode("73px"),
RowSpec.decode("42px"),}));
btnYes.setFont(new Font("Tahoma", Font.PLAIN, 10));
getContentPane().add(btnYes, "1, 4, right, fill");
btnYes.setActionCommand("Yes");
btnYes.addActionListener(this);
btnNo.setFont(new Font("Tahoma", Font.PLAIN, 10));
getContentPane().add(btnNo, "3, 4, left, fill");
btnNo.setActionCommand("No");
btnNo.addActionListener(this);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(lblNewLabel, "1, 2, 3, 1, fill, fill");
}
public void actionPerformed(ActionEvent evt) {
Object cmd = evt.getActionCommand();
if (cmd == "Yes")
{
++q;
}
else if (cmd == "No")
{
++q;
}
}
}
}
我得到的错误是:
C:\Users\*****\Desktop>javac QA.java
QA.java:8: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.FormLayout;
^
QA.java:9: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.ColumnSpec;
^
QA.java:10: error: package com.jgoodies.forms.factories does not exist
import com.jgoodies.forms.factories.FormFactory;
^
QA.java:11: error: package com.jgoodies.forms.layout does not exist
import com.jgoodies.forms.layout.RowSpec;
^
QA.java:32: error: cannot find symbol
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
^
symbol: class FormLayout
location: class QA
QA.java:32: error: cannot find symbol
getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
^
symbol: class ColumnSpec
location: class QA
QA.java:33: error: cannot find symbol
ColumnSpec.decode("220px"),
^
symbol: variable ColumnSpec
location: class QA
QA.java:34: error: cannot find symbol
FormFactory.UNRELATED_GAP_COLSPEC,
^
symbol: variable FormFactory
location: class QA
QA.java:35: error: cannot find symbol
ColumnSpec.decode("220px"),},
^
symbol: variable ColumnSpec
location: class QA
QA.java:36: error: cannot find symbol
new RowSpec[] {
^
symbol: class RowSpec
location: class QA
QA.java:37: error: cannot find symbol
RowSpec.decode("97px"),
^
symbol: variable RowSpec
location: class QA
QA.java:38: error: cannot find symbol
RowSpec.decode("50px"),
^
symbol: variable RowSpec
location: class QA
QA.java:39: error: cannot find symbol
RowSpec.decode("73px"),
^
symbol: variable RowSpec
location: class QA
QA.java:40: error: cannot find symbol
RowSpec.decode("42px"),}));
^
symbol: variable RowSpec
location: class QA
14 errors
我能做些什么吗?谢谢你的帮助!
答案 0 :(得分:2)
你错过了类路径中的JGoodies Forms jar。您可以从here
下载编译:
javac .;forms-1.2.1.jar QA.java
运行:
appletviewer my-qa-test.html
HTML文档包含applet标记
<applet code="QA.class" width=400 height=75 arhive="forms-1.2.1.jar"> </applet>
除此之外:在比较String
内容时,请使用String#equals
==
运算符比较对象引用。在这种情况下,通过使用匿名ActionListener
类来应用关注点分离:
btnYes.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
q++;
}
});