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