尝试使用JButtons

huangapple 未分类评论46阅读模式
英文:

Trying to use JButtons

问题

我正在尝试在同一个包中的两个不同的.java文件中使用JButton组件。当我尝试从同一个包中的两个不同的.java文件编译代码时,会显示编译错误。当我尝试将主类移动到与LoginScreen.java相同的文件中时,我可以无问题地运行代码。我试图理解为什么如果主类在同一个.java文件中,代码会正常工作。但是当主类在同一个包中的另一个文件中时,它无法编译。

这是我的代码:

LoginScreen

package PasswordLockbox;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LoginScreen extends JFrame {
    public JButton submit;
    public JButton user;

    public LoginScreen() {
        super("Password Lockbox");
        setLayout(new FlowLayout());

        submit = new JButton("Submit");
        add(submit);

        user = new JButton("Create new user");
        add(user);

        HandlerClass handler = new HandlerClass();
        submit.addActionListener(handler);
        user.addActionListener(handler);
    }

    private class HandlerClass implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent event) {
            JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
        }
    }
}

PasswordLockbox

package PasswordLockbox;
import javax.swing.JFrame;

public class PasswordLockbox {
    public static void main(String[] args) {
        LoginScreen log = new LoginScreen();
        log.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        log.setSize(500,400);
        log.setVisible(true);
    }
}

当我尝试编译代码时,会出现以下错误:

javac PasswordLockbox.java
PasswordLockbox.java:19: error: cannot find symbol
        LoginScreen object;
        ^
  symbol:   class LoginScreen
  location: class PasswordLockbox
PasswordLockbox.java:20: error: cannot find symbol
        object = new LoginScreen();
                     ^
  symbol:   class LoginScreen
  location: class PasswordLockbox
2 errors
英文:

I'm trying to use JButton components in two different .java files in the same package. When I try compiling the code from two different java files in the same package it shows compiler errors. When I try moving the main class to the same file as LoginScreen.java then I can the run the code with no problem. I'm trying to understand why the code will work if the main is in the same .java file. but when the main class is in a separate file in the same package it doesn't compile.

Here is my code:

##LoginScreen

package PasswordLockbox;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class LoginScreen extends JFrame
{
public JButton submit;
public JButton user;

public LoginScreen()
{
super("Password Lockbox");
setLayout(new FlowLayout());

submit = new JButton("Submit");
add(submit);

user = new JButton("Create new user");
add(user);

HandlerClass handler = new HandlerClass();
submit.addActionListener(handler);
user.addActionListener(handler);



}

private class HandlerClass implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(null,String.format("%s", event.getActionCommand()));
    }
}
}

##PasswordLockbox

package PasswordLockbox;
import javax.swing.JFrame;
/**
 *
 * @author xxx
 */
public class PasswordLockbox {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    LoginScreen log = new LoginScreen();
    log.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    log.setSize(500,400);
    log.setVisible(true);
}

}

When I try to compile the code it gives me these errors:

javac PasswordLockbox.java
PasswordLockbox.java:19: error: cannot find symbol
        LoginScreen object;
        ^
  symbol:   class LoginScreen
  location: class PasswordLockbox
PasswordLockbox.java:20: error: cannot find symbol
        object = new LoginScreen();
                     ^
  symbol:   class LoginScreen
  location: class PasswordLockbox
2 errors

答案1

得分: 0

首先,您实际上并未运行代码,而是使用 javac 进行编译。
如果您选择仅编译 PasswordLockbox.java,则编译器必须知道在哪里可以找到依赖类 PasswordLockbox.LoginScreen

因此,为了解决这个问题,您应该使用以下命令编译所有的类:

project>javac PasswordLockbox/*.java

请注意,在这种情况下,您会在存放 *.java 文件的同一目录中得到所有的 .class 文件,因此建议使用参数 -d 来指定编译后的类的位置。

然后,您可以使用 java 命令运行您的程序,现在您需要使用 -cp 参数提供类路径(可选),并使用全限定类名(即包括包名):

project>java -cp PasswordLockbox; PasswordLockbox.PasswordLockbox

例如,如果切换到 PasswordLockbox 目录,您将需要使用以下命令:

project/PasswordLockbox>java -cp ..; PasswordLockbox.PasswordLockbox

以下是使用另一个目录来存放编译后类文件的示例:

project>javac -d classes PasswordLockbox/*.java
project>java -cp classes PasswordLockbox.PasswordLockbox
英文:

First, you are not actually running the code, with javac you compile it.
And if you select to compile only PasswordLockbox.java then the compiler must know where it can find depending class PasswordLockbox.LoginScreen

Thus, to overcome this issue, you should use this command to compile all classes:

project>javac PasswordLockbox/*.java

Please note that in such case you'll get all .class files in the same directory where you hold *.java files so it is recommended to use parameter -d to place compiled classes

Then you can run your program using java command and now you need to provide classpath using -cp parameter (optionally) and use fully-qualified class name (that is, including package name):

project>java -cp PasswordLockbox; PasswordLockbox.PasswordLockbox 

For instance, if you change to PasswordLockbox directory. you'll have to use this command:

project/PasswordLockbox>java -cp ..; PasswordLockbox.PasswordLockbox 

Examples using another directory for compiled classes:

project>javac -d classes PasswordLockbox/*.java
project>java -cp classes PasswordLockbox.PasswordLockbox

huangapple
  • 本文由 发表于 2020年5月4日 02:01:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61579242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定