//package swing_test; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; class swing { public void init() { JFrame f = new JFrame("test"); f.setTitle("简易计算器"); f.setSize(500, 300); f.setLocationRelativeTo(null); Font font = new Font("楷体", Font.PLAIN, 40); Font font1 = new Font("楷体", Font.PLAIN, 25); JPanel panel = new JPanel(new BorderLayout()); JLabel t = new JLabel("四则运算", JLabel.CENTER); t.setFont(font); panel.add(t, BorderLayout.NORTH); JLabel l1 = new JLabel("第一个数:"); l1.setFont(font1); JLabel l2 = new JLabel("第二个数:"); l2.setFont(font1); JLabel l = new JLabel(" 结 果:"); l.setFont(font1); JLabel l3 = new JLabel("*"); l3.setFont(font1); JLabel l4 = new JLabel("*"); l4.setFont(font1); JButton b1 = new JButton("+"); b1.setFont(font1); JButton b2 = new JButton("-"); b2.setFont(font1); JButton b3 = new JButton("*"); b3.setFont(font1); JButton b4 = new JButton("/"); b4.setFont(font1); JButton b5=new JButton("清除"); b5.setFont(font1); b5.setBackground(Color.GRAY); l3.setForeground(Color.red); l4.setForeground(Color.red); JTextField in1 = new JTextField(20); in1.setFont(font1); JTextField in2 = new JTextField(20); in2.setFont(font1); JTextField in3 = new JTextField(20); in3.setFont(font1); JPanel p = new JPanel(); p.setLayout(null); l1.setBounds(65, 10, 180, 30); in1.setBounds(215, 10, 150, 30); l3.setBounds(385, 10, 30, 30); l2.setBounds(65, 50, 180, 30); in2.setBounds(215, 50, 150, 30); l4.setBounds(385, 50, 30, 30); l.setBounds(65, 90, 180, 30); in3.setBounds(215, 90, 150, 30); b1.setBounds(40, 145, 50, 50); b2.setBounds(115, 145, 50, 50); b3.setBounds(190, 145, 50, 50); b4.setBounds(265, 145, 50, 50); b5.setBounds(350,145,100,50); /* JLabel label = new JLabel(" "); label.setFont(font1); label.setForeground(Color.red);*/ b1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { in3.setText(String.valueOf(Float.parseFloat(in1.getText()) + Float.parseFloat(in2.getText()))); } catch (NumberFormatException q) { JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE); } } }); b2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { in3.setText(String.valueOf(Float.parseFloat(in1.getText()) - Float.parseFloat(in2.getText()))); } catch (NumberFormatException q) { // label.setText("请输入数据后再进行运算!"); JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE); } } }); b3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ in3.setText(String.valueOf(Float.parseFloat(in1.getText()) * Float.parseFloat(in2.getText()))); }catch (NumberFormatException q){ // label.setText("请输入数据后再进行运算!"); JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE); } } }); b4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ in3.setText(String.valueOf(Float.parseFloat(in1.getText()) / Float.parseFloat(in2.getText()))); }catch (NumberFormatException q){ // label.setText("请输入数据后再进行运算!"); JOptionPane.showMessageDialog(null, "请正确输入数据后再进行运算!", "错误", JOptionPane.ERROR_MESSAGE); } } }); b5.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { in1.setText(""); in2.setText(""); in3.setText(""); JOptionPane.showMessageDialog(null, "清除已完成!", "清除", JOptionPane.INFORMATION_MESSAGE); } }); panel.add(p, BorderLayout.CENTER); // panel.add(label, BorderLayout.SOUTH); p.add(l1); p.add(in1); p.add(l3); p.add(l2); p.add(in2); p.add(l4); p.add(l); p.add(in3); p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5); f.add(panel); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } public class Demo { public static void main(String[] args) { swing s = new swing(); s.init(); } }