简体   繁体   中英

Trying to use JButtons

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM