小弟初学java,很多都不懂,希望大神看到这个能告诉我是怎么回事。
我写了差不多有10个这样带JButton按钮的简单GUI界面程序,但是每一个都跑不起来,全是报空指针异常。
这是其中一个源代码程序,基本上我写的每个带点击事件的都是这种,差不多的问题。
import java.awt.EventQueue;
import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.BorderLayout; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.border.EmptyBorder;
public class StringConvert extends JFrame {
private JPanel contentPane; private JTextField textField; private JTextField textField_1; private final ButtonGroup buttonGroup = new ButtonGroup(); private JPanel panel; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { StringConvert frame = new StringConvert(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
/** * Create the frame. */ public StringConvert() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); JPanel panel = new JPanel(); contentPane.add(panel, BorderLayout.CENTER); panel.setLayout(null); textField = new JTextField(); textField.setBounds(0, 45, 440, 28); panel.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setEditable(false); textField_1.setBounds(0, 205, 440, 28); panel.add(textField_1); textField_1.setColumns(10); JRadioButton rdbtnUppercase = new JRadioButton("UpperCase"); rdbtnUppercase.setBounds(225, 109, 99, 23); buttonGroup.add(rdbtnUppercase); panel.add(rdbtnUppercase); JRadioButton rdbtnLowercase = new JRadioButton("LowerCase"); rdbtnLowercase.setBounds(322, 109, 99, 23); buttonGroup.add(rdbtnLowercase); panel.add(rdbtnLowercase); JButton btnConvert = new JButton("Convert"); btnConvert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { do_button_actionPerformed(arg0); } }); btnConvert.setBounds(0, 108, 117, 29); panel.add(btnConvert); JLabel lblInputText = new JLabel("Input Text:"); lblInputText.setBounds(6, 18, 69, 16); panel.add(lblInputText); JLabel lblOutputText = new JLabel("Output Text:"); lblOutputText.setBounds(6, 177, 86, 16); panel.add(lblOutputText); } private void do_button_actionPerformed(ActionEvent e) { String command = buttonGroup.getSelection().getActionCommand(); boolean upper = command.equals("UpperCase"); String text = textField.getText(); if(upper) { textField_1.setText(text.toUpperCase()); } else { textField_1.setText(text.toLowerCase()); } } }
异常是这样写的:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at StringConvert.do_button_actionPerformed(StringConvert.java:95) at StringConvert.access$0(StringConvert.java:93)
at StringConvert$2.actionPerformed(StringConvert.java:79)
|