Java swing简单地址簿事件错误
Java swing simple address book event error
我正在尝试制作一个简单的地址簿 GUI,它最终能够将所有字段(如姓名编号)添加到数据库中。目前,我正在尝试添加方法来更改按下按钮时会发生什么,但我一直收到此错误...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddressBook.actionPerformed(AddressBook.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
.
.
. theres some more but you get the point
我想知道是否有人可以帮助我找出为什么我不断收到此错误,甚至可以帮助我改进该程序的结构。谢谢!
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class AddressBook implements ActionListener{
JFrame frame;
JPanel Master, centerInfo, buttonInfo;
GridBagConstraints constraints;
GridBagConstraints topconstraint;
JLabel jlb_firstName, jlb_lastName, jlb_phoneNum, jlb_searchInfo;
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
JTextArea jta_searchInfo;
JButton jb_add, jb_search, jb_share;
Container pane;
String firstName, lastName, phoneNum;
//address book designed in GridBagLayout via swing
public static void main(String[] args)
{
AddressBook a = new AddressBook();
System.out.println("hello world");
a.start_gui();
}
public void start_gui()
{
JFrame frame = new JFrame("Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new Container();
pane = frame.getContentPane();
layComponents(pane);
//frame.setSize(400, 400);
frame.setSize(400,400);
frame.setVisible(true);
}
private void layComponents(Container contentPane)
{
//PANELS,CONTENTPANE,CONSTRAINTS
JPanel Master = new JPanel();
JPanel centerInfo = new JPanel(new GridBagLayout());
JPanel buttonInfo = new JPanel(new FlowLayout(FlowLayout.LEADING,10,5));
Master.add(centerInfo, BorderLayout.CENTER);
Master.add(buttonInfo, BorderLayout.SOUTH);
contentPane.add(Master); //set style of layout for GUI
GridBagConstraints constraints = new GridBagConstraints(); //layout constraints
GridBagConstraints topconstraint = new GridBagConstraints(); //constraint against top of GUI
constraints.insets = new Insets(5,5,10,10);
//components: firstName, lastName, phoneNum, info, Add, Search(need to add oracle sql db)
//LABELS
JLabel jlb_firstName = new JLabel("First Name");
topconstraint.gridx =1;
topconstraint.gridy =0;
topconstraint.insets = new Insets(20,5,10,10); //Insets(top,left,right,down)
centerInfo.add(jlb_firstName, topconstraint);
JLabel jlb_lastName = new JLabel("Last Name");
constraints.gridx =1;
constraints.gridy =2;
centerInfo.add(jlb_lastName,constraints);
JLabel jlb_phoneNum = new JLabel("Phone Number");
constraints.gridx =1;
constraints.gridy =4;
centerInfo.add(jlb_phoneNum,constraints);
JLabel jlb_searchInfo = new JLabel("Search Info");
constraints.gridx =1;
constraints.gridy =6;
centerInfo.add(jlb_searchInfo, constraints);
//TEXTFIELDS
JTextField jtf_firstName = new JTextField("Enter New First Name"); //to clear textfield when clicked need actionlisteners
topconstraint.gridx =3;
topconstraint.gridy =0;
centerInfo.add(jtf_firstName,topconstraint);
JTextField jtf_lastName = new JTextField("Enter New Last Name");
constraints.gridx =3;
constraints.gridy =2;
centerInfo.add(jtf_lastName, constraints);
JTextField jtf_phoneNum = new JTextField("Enter New Phone Number");
constraints.gridx =3;
constraints.gridy =4;
centerInfo.add(jtf_phoneNum, constraints);
//TEXTAREA
Border loweredetched;
loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
JTextArea jta_searchInfo = new JTextArea(10,20);
jta_searchInfo.append("click me");
jta_searchInfo.selectAll();
jta_searchInfo.requestFocus();
jta_searchInfo.setBorder(loweredetched);
constraints.gridx =3;
constraints.gridy =6;
centerInfo.add(jta_searchInfo, constraints);
//buttons will use flowlayout
//BUTTONS
JButton jb_add = new JButton("Add");
buttonInfo.add(jb_add);
JButton jb_search = new JButton("Search");
buttonInfo.add(jb_search);
JButton jb_share = new JButton("Share");
buttonInfo.add(jb_share); //share info with ppl. need a new share gui
//Listen for any component actions performed
jb_add.addActionListener(this);
//jb_search.addActionListener(this);
//jb_share.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
firstName = jtf_firstName.getText();
System.out.println(firstName);
}
}
您已经隐藏了您的实例变量...
您已声明...
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
作为实例字段,但在 layComponents
中,您已重新声明它们
JTextField jtf_firstName = new JTextField("Enter New First Name");
这意味着实例字段是null
。
我正在尝试制作一个简单的地址簿 GUI,它最终能够将所有字段(如姓名编号)添加到数据库中。目前,我正在尝试添加方法来更改按下按钮时会发生什么,但我一直收到此错误...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddressBook.actionPerformed(AddressBook.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
.
.
. theres some more but you get the point
我想知道是否有人可以帮助我找出为什么我不断收到此错误,甚至可以帮助我改进该程序的结构。谢谢!
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class AddressBook implements ActionListener{
JFrame frame;
JPanel Master, centerInfo, buttonInfo;
GridBagConstraints constraints;
GridBagConstraints topconstraint;
JLabel jlb_firstName, jlb_lastName, jlb_phoneNum, jlb_searchInfo;
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
JTextArea jta_searchInfo;
JButton jb_add, jb_search, jb_share;
Container pane;
String firstName, lastName, phoneNum;
//address book designed in GridBagLayout via swing
public static void main(String[] args)
{
AddressBook a = new AddressBook();
System.out.println("hello world");
a.start_gui();
}
public void start_gui()
{
JFrame frame = new JFrame("Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new Container();
pane = frame.getContentPane();
layComponents(pane);
//frame.setSize(400, 400);
frame.setSize(400,400);
frame.setVisible(true);
}
private void layComponents(Container contentPane)
{
//PANELS,CONTENTPANE,CONSTRAINTS
JPanel Master = new JPanel();
JPanel centerInfo = new JPanel(new GridBagLayout());
JPanel buttonInfo = new JPanel(new FlowLayout(FlowLayout.LEADING,10,5));
Master.add(centerInfo, BorderLayout.CENTER);
Master.add(buttonInfo, BorderLayout.SOUTH);
contentPane.add(Master); //set style of layout for GUI
GridBagConstraints constraints = new GridBagConstraints(); //layout constraints
GridBagConstraints topconstraint = new GridBagConstraints(); //constraint against top of GUI
constraints.insets = new Insets(5,5,10,10);
//components: firstName, lastName, phoneNum, info, Add, Search(need to add oracle sql db)
//LABELS
JLabel jlb_firstName = new JLabel("First Name");
topconstraint.gridx =1;
topconstraint.gridy =0;
topconstraint.insets = new Insets(20,5,10,10); //Insets(top,left,right,down)
centerInfo.add(jlb_firstName, topconstraint);
JLabel jlb_lastName = new JLabel("Last Name");
constraints.gridx =1;
constraints.gridy =2;
centerInfo.add(jlb_lastName,constraints);
JLabel jlb_phoneNum = new JLabel("Phone Number");
constraints.gridx =1;
constraints.gridy =4;
centerInfo.add(jlb_phoneNum,constraints);
JLabel jlb_searchInfo = new JLabel("Search Info");
constraints.gridx =1;
constraints.gridy =6;
centerInfo.add(jlb_searchInfo, constraints);
//TEXTFIELDS
JTextField jtf_firstName = new JTextField("Enter New First Name"); //to clear textfield when clicked need actionlisteners
topconstraint.gridx =3;
topconstraint.gridy =0;
centerInfo.add(jtf_firstName,topconstraint);
JTextField jtf_lastName = new JTextField("Enter New Last Name");
constraints.gridx =3;
constraints.gridy =2;
centerInfo.add(jtf_lastName, constraints);
JTextField jtf_phoneNum = new JTextField("Enter New Phone Number");
constraints.gridx =3;
constraints.gridy =4;
centerInfo.add(jtf_phoneNum, constraints);
//TEXTAREA
Border loweredetched;
loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
JTextArea jta_searchInfo = new JTextArea(10,20);
jta_searchInfo.append("click me");
jta_searchInfo.selectAll();
jta_searchInfo.requestFocus();
jta_searchInfo.setBorder(loweredetched);
constraints.gridx =3;
constraints.gridy =6;
centerInfo.add(jta_searchInfo, constraints);
//buttons will use flowlayout
//BUTTONS
JButton jb_add = new JButton("Add");
buttonInfo.add(jb_add);
JButton jb_search = new JButton("Search");
buttonInfo.add(jb_search);
JButton jb_share = new JButton("Share");
buttonInfo.add(jb_share); //share info with ppl. need a new share gui
//Listen for any component actions performed
jb_add.addActionListener(this);
//jb_search.addActionListener(this);
//jb_share.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
firstName = jtf_firstName.getText();
System.out.println(firstName);
}
}
您已经隐藏了您的实例变量...
您已声明...
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
作为实例字段,但在 layComponents
中,您已重新声明它们
JTextField jtf_firstName = new JTextField("Enter New First Name");
这意味着实例字段是null
。