有两个关键的地方:
其一: JTextField field=new JTextField(10);
这是一个文本输入框,里面的参数10的意思是,这个输入框的长度为10列
其二:点击求和按钮,出结果 第一:对求和按钮设置监听 第二对按下求和按钮(按钮动作的实现)
设置监听:bt = new JButton("求和")
bt.addActionListener(new ActionListener() { // 设置监听有圆括号(关键词是addActionListener)
@Override // 接下来就是按钮动作的实现 (关键词ActionEvent )
public void actionPerformed(ActionEvent e) {
int value1 = Integer.parseInt(t1.getText());
int value2 = Integer.parseInt(t2.getText());
t3.setText(Integer.toString(value1+value2)); //把加号换成乘,相应的变成乘法计算器
}
});
自己修改的一个模式,尽可能的接近绣花的小PS软件
bt = new JButton("求和")
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jiafa(e);
}
});
//接下来独立在上面的程序,在后面写了一个程序
void jiafa(ActionEvent e) {
int value1 = Integer.parseInt(t1.getText());
int value2 = Integer.parseInt(t2.getText());
t3.setText(Integer.toString(value1+value2));
}
经典的用java扣蓝技术也是这个模式:
JMenuItem Item1;
Item1 = new JMenuItem("抠蓝"); //, undoIcon);
Item1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Item1_process(e); //子菜单1处理程序
}
});
void Item1_process(ActionEvent e) {
BufferedImage image1=null;
chooser.setDialogType(JFileChooser.OPEN_DIALOG);
if(chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION) {
try { image1 = ImageIO.read(chooser.getSelectedFile()); }
catch(Exception ex) { return ;}
}
image=koulanImage.image_add(image,image1);
imagePanel.setImage(image);
imagePanel.repaint();
}
在同一个包下有koulanImage.java 综合起来就能扣蓝了
加法计算器的源程序如下
package dsfa; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; class AddDemo extends JFrame { JLabel b1, b2; JTextField t1, t2, t3; JButton bt; public AddDemo() { b1 = new JLabel("请你输入第一个数", JLabel.CENTER); b2 = new JLabel("请你输入第二个数", JLabel.CENTER); b1.setBorder(BorderFactory.createEtchedBorder()); b2.setBorder(BorderFactory.createEtchedBorder()); t1 = new JTextField(2); t2 = new JTextField(2); t3 = new JTextField(2); t3.setEditable(false); bt = new JButton("求和"); setLayout(new GridLayout(3, 2)); add(b1); add(t1); add(b2); add(t2); add(bt); add(t3); bt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int value1 = Integer.parseInt(t1.getText()); int value2 = Integer.parseInt(t2.getText()); t3.setText(Integer.toString(value1+value2)); } }); setSize(500, 500); setVisible(true); // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String arg[]) { new AddDemo(); } }
下面这个程序在迟静老师最后一节的几何产品设计课前做的
package dsfa; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; class AddDemo extends JFrame { JLabel b1, b2; JTextField t1, t2, t3; JButton bt; public AddDemo() { b1 = new JLabel("请你输入第一个数", JLabel.CENTER); b2 = new JLabel("请你输入第二个数", JLabel.CENTER); b1.setBorder(BorderFactory.createEtchedBorder()); b2.setBorder(BorderFactory.createEtchedBorder()); t1 = new JTextField(2); t2 = new JTextField(2); t3 = new JTextField(2); t3.setEditable(false); bt = new JButton("求和"); setLayout(new GridLayout(3, 2)); add(b1); add(t1); add(b2); add(t2); add(bt); add(t3); bt.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jiafa(e); } }); setSize(500, 500); setVisible(true); } void jiafa(ActionEvent e) { //遗留问题 int value1 = Integer.parseInt(t1.getText()); int value2 = Integer.parseInt(t2.getText()); t3.setText(Integer.toString(value1+value2)); } public static void main(String arg[]) { new AddDemo(); } }
遗留问题:void jiafa(ActionEvent e) 将这个拿进构造方法里面去,编译显示错误