简体   繁体   English

在eclipse中编写程序会导致命令提示符出错

[英]writing program in eclipse causes errors in command prompt

so I'm writing a program for a class and it needs to be runnable via command prompt. 所以我正在为一个类编写一个程序,它需要通过命令提示符运行。 IE javac filename.java then java filename. IE javac filename.java然后是java文件名。 I wrote the code in eclipse which is why I'm having trouble. 我在eclipse中编写了代码,这就是我遇到麻烦的原因。 My code is as follows: 我的代码如下:

    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;
        }
    }
}
}

the errors I get are along the lines of: 我得到的错误是:

    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

Is there anything I can do? 有什么我能做的吗? Thanks for any help! 谢谢你的帮助!

You're missing the JGoodies Forms jar from your classpath. 你错过了类路径中的JGoodies Forms jar。 You can download it from here 你可以从这里下载

To compile: 编译:

javac .;forms-1.2.1.jar QA.java

To run: 跑步:

appletviewer my-qa-test.html

where the HTML document contains an applet tag HTML文档包含applet标记的位置

<applet code="QA.class" width=400 height=75 arhive="forms-1.2.1.jar"> </applet>

Aside: When comparing String contents, use String#equals The == operator compares Object references. 另外:比较String内容时,使用String#equals ==运算符比较Object引用。 In this case apply separation of concerns by using anonymous ActionListener classes: 在这种情况下,通过使用匿名ActionListener类来应用关注点分离:

btnYes.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
       q++;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 程序在Eclipse中编译,但不在命令提示符下编译 - Program compiling in Eclipse but not in Command Prompt 在命令提示符下编译并运行Java程序(在Eclipse中运行) - Compiling and running java program from command prompt (It runs in Eclipse) Java线程程序可在Eclipse中运行,但不能在终端/命令提示符下运行 - Java thread program works in Eclipse, but not in terminal / command prompt 第一个 hello world java 程序可以在 Eclipse 中运行,但不能在命令提示符下运行 - Very first hello world java program works in Eclipse but not in command prompt 与命令提示符相比,Eclipse中的程序执行速度非常慢 - Program execution in Eclipse is very slow when compared to command prompt 执行程序时,额外的输入命令会导致逻辑错误java - when executing program, extra input command causes logical errors java 在Eclipse中,如何在运行另一个程序作为第一个参数的同时,将它当作命令提示符中的第二个参数来运行Java程序? - In Eclipse, how to run a java program as if it's the second argument in the command prompt while running another program as the first argument? 通过 Java 编写命令提示符? - Writing in Command Prompt through Java? svn导致Eclipse中的构建错误 - svn causes build errors in eclipse 使用Eclipse打开命令提示符 - Open command prompt using Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM