Why am I getting this exception? Trying to make a simple GUI in Java
import java.awt.*;
import javax.swing.*;
public class userInput extends JFrame {
private JButton newEntry;
private JButton deleteEntry;
private JButton editEntry;
private JButton saveEntry;
private JButton cancelEntry;
private FlowLayout layout;
public userInput() {
开发者_JAVA百科 super("My Address Book"); //sets the title!
JTextField field = new JTextField(20);
Container content = getContentPane();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(newEntry);
buttonPanel.add(deleteEntry);
buttonPanel.add(editEntry);
buttonPanel.add(saveEntry);
buttonPanel.add(cancelEntry);
add(buttonPanel, BorderLayout.SOUTH);
content.setLayout(new BorderLayout());
content.add(buttonPanel, "South");
setVisible(true);
}
}
Here is my driver program:
import javax.swing.*;
public class AddressBookGui {
public static void main (String[] args)
{
userInput addressBook = new userInput();
addressBook.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //whenever you hit x you will exit the program
addressBook.setSize(750, 600);
addressBook.setVisible(true);
}
}
you have to initialize newEntry before doing
newEntry = new JButton("foo");
buttonPanel.add(newEntry);
along with the other buttons
You forgot to allocate your Buttons:
newEntry = new JButton();
deleteEntry = new JButton();
...
精彩评论