问题描述
packagestu.view;importjava.awt.BorderLayout;importjava.awt.Component;importjava.awt.EventQueue;importjava.awt.TextField;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.border.EmptyBorder;importjavax.swing.text.JTextComponent;importjavax.swing.JTextField;importjavax.swing.JButton;importjava.awt.event.ActionListener;importjava.awt.event.ActionEvent;publicclasseduextendsJFrame{privateJFrameframe;privatefinalTextFieldtextResult=newTextField();privateJPanelcontentPane;privateJTextFieldtextField;protectedComponentoperateSub;protectedComponentoperateAdd;protectedComponentoperateRes;/***Launchtheapplication.*/publicstaticvoidmain(String[]args){EventQueue.invokeLater(newRunnable(){publicvoidrun(){try{eduframe=newedu();frame.setVisible(true);}catch(Exceptione){e.printStackTrace();}}});}/***Createtheframe.*/publicedu(){initialize();}privatevoidinitialize(){//TODO自动生成的方法存根setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100,100,450,300);contentPane=newJPanel();contentPane.setBorder(newEmptyBorder(5,5,5,5));setContentPane(contentPane);contentPane.setLayout(null);textField=newJTextField();textField.setBounds(10,10,401,28);contentPane.add(textField);textField.setColumns(10);JButtonbutton_1=newJButton("1");button_1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(1);}});button_1.setBounds(10,60,93,23);contentPane.add(button_1);JButtonbutton_2=newJButton("2");button_2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(2);}});button_2.setBounds(150,60,93,23);contentPane.add(button_2);JButtonbutton_3=newJButton("3");button_3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(3);}});button_3.setBounds(282,60,93,23);contentPane.add(button_3);JButtonbutton_4=newJButton("4");button_4.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(4);}});button_4.setBounds(10,120,93,23);contentPane.add(button_4);JButtonbutton_5=newJButton("5");button_5.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(5);}});button_5.setBounds(150,120,93,23);contentPane.add(button_5);JButtonbutton_6=newJButton("6");button_6.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(6);}});button_6.setBounds(282,120,93,23);contentPane.add(button_6);JButtonbutton_7=newJButton("7");button_7.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(7);}});button_7.setBounds(10,172,93,23);contentPane.add(button_7);JButtonbutton_8=newJButton("8");button_8.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(8);}});button_8.setBounds(150,172,93,23);contentPane.add(button_8);JButtonbutton_9=newJButton("9");button_9.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){inputProc(9);}});button_9.setBounds(282,172,93,23);contentPane.add(button_9);JButtonbutton_10=newJButton("+");button_10.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){contentPane.add(operateAdd);}});button_10.setBounds(10,229,93,23);contentPane.add(button_10);JButtonbutton_12=newJButton("=");button_12.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){contentPane.add(operateRes);}});button_12.setBounds(282,229,93,23);contentPane.add(button_12);JButtonbutton_11=newJButton("-");button_11.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){contentPane.add(operateSub);}});button_11.setBounds(150,229,93,23);contentPane.add(button_11);}protectedvoidinputProc(inti){//TODO自动生成的方法存根StringnewNum=String.valueOf(i);JTextComponenttextResult=null;StringstrOut=textResult.getText();strOut+=newNum;textResult.setText(strOut);}}
解决方案
解决方案二:
protectedvoidinputProc(inti){//TODO自动生成的方法存根StringnewNum=String.valueOf(i);JTextComponenttextResult=null;//textResult为空,再getText()肯定报错啊StringstrOut=textResult.getText();strOut+=newNum;textResult.setText(strOut);}