JAVA Swing(GUI)简易计算器Jframe(图形化界面)
实现功能:
输入整数,选择运算符号,再点击确定得出结果,点击退出时,关闭窗口
效果图(例子)
注意:准备步骤
若要做该项目需要下载WindowBuilder插件
若未安装WindowBuilder需要安装,如下是安装步骤
WindowBuilder的安装
部分步骤省略,点默认的就行,点完之后下载的时间会有些长,需要耐心等待一会儿,下载好之后会弹出一个窗口,点击默认的,eclipse就会重启,WindowBuilder就装好了
步骤1:先新建JFrame
步骤2:将控件拖入框内
若控件无法拖进窗口游离在外的话,请进行以下操作
步骤3:1 修改控件变量名,2 修改控件上显示的内容
步骤4:为按钮添加监听事件:鼠标点击事件
再回到Source界面进行代码的编写
package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
public class Windowbuilder extends JFrame {
private JPanel contentPane;
private JTextField text1;
private JTextField text2;
private JComboBox comboBox1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Windowbuilder frame = new Windowbuilder();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Windowbuilder() {
setTitle("\u8BA1\u7B97\u5668");
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);
text1 = new JTextField();
text1.setBounds(21, 53, 66, 21);
contentPane.add(text1);
text1.setColumns(10);
text2 = new JTextField();
text2.setBounds(141, 53, 66, 21);
contentPane.add(text2);
text2.setColumns(10);
JLabel lbResult = new JLabel("=");
lbResult.setFont(new Font("宋体", Font.PLAIN, 14));
lbResult.setBounds(231, 56, 58, 15);
contentPane.add(lbResult);
JButton btnNewButton = new JButton("\u786E\u5B9A");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//接收数据
String strData1=text1.getText().trim();//从text1里取值传strData1
String strData2=text2.getText().trim();//从text2里取值传strData2
String cmd1=comboBox1.getSelectedItem().toString();//取出下拉框里面的符号并转为字符串型
//验证数据
//验证是否为空
if(strData1.equals("")||strData2.equals("")) {
JOptionPane.showMessageDialog(null, "不能为空","警告",JOptionPane.ERROR_MESSAGE);//提示框:不能为空
return;
}
//验证是否为数值
if(strData1.matches("/^(^-?\\d+$)$/")) {//相应的正则表达式(整数)
JOptionPane.showMessageDialog(null,"请正确输入数值","警告",JOptionPane.ERROR_MESSAGE);//提示框:请输入数值
return;
}
//处理数据;1)类型转换 2)计算
double data1=Integer.parseInt(strData1);//并强制转换成数值类型,然后赋值给data1
double data2=Integer.parseInt(strData2);//从strData2里取值并强制转换成数值类型,然后赋值给data2
double result=0.0;
if(cmd1.equals("+"))
{
result=data1+data2;
}
if(cmd1.equals("-"))
{
result=data1-data2;
}
if(cmd1.equals("*"))
{
result=data1*data2;
}
if(cmd1.equals("/"))
{
result=data1/data2;
}
lbResult.setText("= "+result);//根据下拉框中的选择进行计算
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.setBounds(21, 93, 66, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u9000\u51FA");
btnNewButton_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {//点击退出按钮
//取消
System.exit(0);//退出
}
});
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton_1.setBounds(141, 93, 66, 23);
contentPane.add(btnNewButton_1);
comboBox1 = new JComboBox();//新建一个JComboBox;
comboBox1.setFont(new Font("宋体", Font.PLAIN, 14));//设置的字体大小
comboBox1.addItem("+");
comboBox1.addItem("-");
comboBox1.addItem("*");
comboBox1.addItem("/");//下拉框中的选项
comboBox1.setBounds(97, 52, 34, 23);
contentPane.add(comboBox1);
}
}
大部分代码都会自动生成,只要再进行关键部分逻辑编写即可,别被这么长的代码吓住了
结尾
希望对大家有帮助!!!
有问题可在下面留言,谢谢…