0

所以我正在为一个类编写一个程序,它需要通过命令提示符运行。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

有什么我可以做的吗?谢谢你的帮助!

4

1 回答 1

2

您的类路径中缺少 JGoodies Forms jar。你可以从这里下载

编译:

javac .;forms-1.2.1.jar QA.java

跑步:

appletviewer my-qa-test.html

其中 HTML 文档包含一个小程序标签

<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++;
    }
});
于 2013-05-14T23:41:32.227 回答