java Swing GUI 入门-简易加法器
觉得有用的话,欢迎一起讨论相互学习~
设计布局
注意一定不要选中Custom Create
- 放上三个Jlabel,不用设置field name 但是可以设置一下foreground 然后设置一下text 分别为First Number Second Number Result
- 然后再在右边添加三个JTextField 并且设置一下颜色和字体,其中field name 分别设置为num1Field,num2Field,resultField,其中最后的一个文本框用于输出计算结果,因此去掉框中的editable复选框.
- 下面放置三个JButton,field name分别设置为ADDButton, MODButton,CLEARButton,text分别设置为ADD,MOD,CLEAR三种.
- 拖动控件调整一下控件的布局.
注意一定不要选中Custom Create
- 可以右键Preview查看一下目前的布局
添加事件操作
- 右键ADDButton Create Listener
- 在ADDButton函数中添加以下的事件
ADDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String num1 = num1Field.getText().trim();//trim()的作用是去掉字符串两端多余的空格
String num2 = num2Field.getText().trim();
double result = Double.parseDouble(num1) + Double.parseDouble(num2);
resultField.setText(result+"");
}
});
- 右键MODButton Create Listener
- 在MODButton函数中添加以下的事件
MODButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String num1 = num1Field.getText().trim();//trim()的作用是去掉字符串两端多余的空格
String num2 = num2Field.getText().trim();
double result = Double.parseDouble(num1) % Double.parseDouble(num2);
resultField.setText(result+"");
}
});
- 右键CLEARButton Create Listener
- 在CLEARButton函数添加以下的事件
CLEARButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1Field.setText("");
num2Field.setText("");
resultField.setText("");
}
});
}//end constructor
//end constructor函数反括号末尾添加注释,养成好习惯!
Generate a Form main() 生成一个main函数
演示一下
完整代码
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleCalculator {
private JPanel MainPanel;
private JPanel topPanel;
private JPanel bottomPanel;
private JTextField num1Field;
private JButton ADDButton;
private JButton MODButton;
private JButton CLEARButton;
private JTextField num2Field;
private JTextField resultField;
public SimpleCalculator() {
ADDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String num1 = num1Field.getText().trim();//trim()的作用是去掉字符串两端多余的空格
String num2 = num2Field.getText().trim();
double result = Double.parseDouble(num1) + Double.parseDouble(num2);
resultField.setText(result+"");
}
});
MODButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String num1 = num1Field.getText().trim();//trim()的作用是去掉字符串两端多余的空格
String num2 = num2Field.getText().trim();
double result = Double.parseDouble(num1) % Double.parseDouble(num2);
resultField.setText(result+"");
}
});
CLEARButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1Field.setText("");
num2Field.setText("");
resultField.setText("");
}
});
}//end constructor
public static void main(String[] args) {
JFrame frame = new JFrame("SimpleCalculator");
frame.setContentPane(new SimpleCalculator().MainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}//end SimpleCalculator