自己写的计算器,输出栏无法显示,求大神

wengdnpxun1 2015-01-26 03:59:23
package stu.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class edu extends JFrame {
private JFrame frame;
private final TextField textResult = new TextField();
private JPanel contentPane;
private JTextField textField;
protected Component operateSub;
protected Component operateAdd;
protected Component operateRes;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
edu frame = new edu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public edu() {
initialize();
}

private void initialize() {
// TODO 自动生成的方法存根
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

textField = new JTextField();
textField.setBounds(10, 10, 401, 28);
contentPane.add(textField);
textField.setColumns(10);


JButton button_1 = new JButton("1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(1);
}

});
button_1.setBounds(10, 60, 93, 23);
contentPane.add(button_1);

JButton button_2 = new JButton("2");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(2);
}
});
button_2.setBounds(150, 60, 93, 23);
contentPane.add(button_2);

JButton button_3 = new JButton("3");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(3);
}
});
button_3.setBounds(282, 60, 93, 23);
contentPane.add(button_3);

JButton button_4 = new JButton("4");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(4);
}
});
button_4.setBounds(10, 120, 93, 23);
contentPane.add(button_4);

JButton button_5 = new JButton("5");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(5);
}
});
button_5.setBounds(150, 120, 93, 23);
contentPane.add(button_5);

JButton button_6 = new JButton("6");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(6);
}
});
button_6.setBounds(282, 120, 93, 23);
contentPane.add(button_6);

JButton button_7 = new JButton("7");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(7);
}
});
button_7.setBounds(10, 172, 93, 23);
contentPane.add(button_7);

JButton button_8 = new JButton("8");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(8);
}
});
button_8.setBounds(150, 172, 93, 23);
contentPane.add(button_8);

JButton button_9 = new JButton("9");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
inputProc(9);
}
});
button_9.setBounds(282, 172, 93, 23);
contentPane.add(button_9);

JButton button_10 = new JButton("+");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contentPane.add(operateAdd);
}
});
button_10.setBounds(10, 229, 93, 23);
contentPane.add(button_10);

JButton button_12 = new JButton("=");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contentPane.add(operateRes);
}
});
button_12.setBounds(282, 229, 93, 23);
contentPane.add(button_12);

JButton button_11 = new JButton("-");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contentPane.add(operateSub);
}
});
button_11.setBounds(150, 229, 93, 23);
contentPane.add(button_11);
}

protected void inputProc(int i) {
// TODO 自动生成的方法存根
String newNum = String.valueOf(i);
JTextComponent textResult = null;
String strOut = textResult.getText();
strOut += newNum;
textResult.setText(strOut);
}
}
...全文
117 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
染指黄昏 2015-01-26
  • 打赏
  • 举报
回复

	protected void inputProc(int i) {
		// TODO 自动生成的方法存根
		String newNum = String.valueOf(i);
		JTextComponent textResult = null;
		//textResult 为空,再getText() 肯定报错啊
		String strOut = textResult.getText();
		strOut += newNum;
		textResult.setText(strOut);
	}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧