首页>代码>Java swing简单Demo,计算器,界面计算分离>/CalculatorTest/src/calculator/CalculatorFrame.java
package calculator;

/**
 *
 * @author 张祖旺
 */
public class CalculatorFrame extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
     * Creates new form CalculatorFrame
     */
    public CalculatorFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

    	Controller controller = new Controller(this);
        backPanel = new javax.swing.JPanel();
        TextField = new javax.swing.JTextField();
        Button0 = new javax.swing.JButton();
        Button1 = new javax.swing.JButton();
        Button2 = new javax.swing.JButton();
        Button3 = new javax.swing.JButton();
        Button4 = new javax.swing.JButton();
        Button5 = new javax.swing.JButton();
        Button6 = new javax.swing.JButton();
        Button7 = new javax.swing.JButton();
        Button8 = new javax.swing.JButton();
        Button9 = new javax.swing.JButton();
        point = new javax.swing.JButton();
        add = new javax.swing.JButton();
        sub = new javax.swing.JButton();
        multi = new javax.swing.JButton();
        divide = new javax.swing.JButton();
        equal = new javax.swing.JButton();
        CEButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        backPanel.setPreferredSize(new java.awt.Dimension(600, 420));

        TextField.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        TextField.setText("0");
        TextField.setPreferredSize(new java.awt.Dimension(300, 50));

        Button0.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button0.setText("0");
        Button0.setPreferredSize(new java.awt.Dimension(50, 50));
        Button0.addActionListener(controller);

        Button1.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button1.setText("1");
        Button1.setPreferredSize(new java.awt.Dimension(50, 50));
        Button1.addActionListener(controller);

        Button2.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button2.setText("2");
        Button2.setPreferredSize(new java.awt.Dimension(50, 50));
        Button2.addActionListener(controller);

        Button3.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button3.setText("3");
        Button3.setPreferredSize(new java.awt.Dimension(50, 50));
        Button3.addActionListener(controller);
        
        Button4.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button4.setText("4");
        Button4.setPreferredSize(new java.awt.Dimension(50, 50));
        Button4.addActionListener(controller);
        
        Button5.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button5.setText("5");
        Button5.setPreferredSize(new java.awt.Dimension(50, 50));
        Button5.addActionListener(controller);
        
        Button6.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button6.setText("6");
        Button6.setPreferredSize(new java.awt.Dimension(50, 50));
        Button6.addActionListener(controller);
        
        Button7.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button7.setText("7");
        Button7.setPreferredSize(new java.awt.Dimension(50, 50));
        Button7.addActionListener(controller);
        
        Button8.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button8.setText("8");
        Button8.setPreferredSize(new java.awt.Dimension(50, 50));
        Button8.addActionListener(controller);

        Button9.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        Button9.setText("9");
        Button9.setPreferredSize(new java.awt.Dimension(50, 50));
        Button9.addActionListener(controller);
        
        point.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        point.setText(".");
        point.setPreferredSize(new java.awt.Dimension(50, 50));
        point.addActionListener(controller);
        
        add.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        add.setText("+");
        add.setPreferredSize(new java.awt.Dimension(50, 50));
        add.addActionListener(controller);

        sub.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        sub.setText("-");
        sub.setPreferredSize(new java.awt.Dimension(50, 50));
        sub.addActionListener(controller);
        
        multi.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        multi.setText("*");
        multi.setPreferredSize(new java.awt.Dimension(50, 50));
        multi.addActionListener(controller);
        
        divide.setFont(new java.awt.Font("微软雅黑", 1, 18)); // NOI18N
        divide.setText("/");
        divide.setPreferredSize(new java.awt.Dimension(50, 50));
        divide.addActionListener(controller);
        
        equal.setText("=");
        equal.setPreferredSize(new java.awt.Dimension(50, 50));
        equal.addActionListener(controller);
        
        CEButton.setFont(new java.awt.Font("微软雅黑", 1, 14)); // NOI18N
        CEButton.setText("CE");
        CEButton.setPreferredSize(new java.awt.Dimension(50, 50));
        CEButton.addActionListener(controller);

        javax.swing.GroupLayout backPanelLayout = new javax.swing.GroupLayout(backPanel);
        backPanel.setLayout(backPanelLayout);
        backPanelLayout.setHorizontalGroup(
            backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backPanelLayout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(point, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(backPanelLayout.createSequentialGroup()
                        .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(backPanelLayout.createSequentialGroup()
                                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(backPanelLayout.createSequentialGroup()
                                .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(66, 66, 66)
                                .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(66, 66, 66)
                        .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, backPanelLayout.createSequentialGroup()
                                    .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(68, 68, 68)
                                    .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(backPanelLayout.createSequentialGroup()
                                    .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(sub, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(backPanelLayout.createSequentialGroup()
                                    .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(multi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(backPanelLayout.createSequentialGroup()
                                    .addComponent(equal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(backPanelLayout.createSequentialGroup()
                                .addGap(118, 118, 118)
                                .addComponent(CEButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(100, Short.MAX_VALUE))
        );
        backPanelLayout.setVerticalGroup(
            backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CEButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(add, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sub, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(multi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(backPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(point, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(equal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(divide, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(backPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(backPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                                                          

	/**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CalculatorFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CalculatorFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify  
    public javax.swing.JButton getCEButton() {
		return CEButton;
	}

	public void setCEButton(javax.swing.JButton cEButton) {
		CEButton = cEButton;
	}    
    
    public javax.swing.JButton getButton0() {
		return Button0;
	}

	public void setButton0(javax.swing.JButton button0) {
		Button0 = button0;
	}

	public javax.swing.JButton getButton1() {
		return Button1;
	}

	public void setButton1(javax.swing.JButton button1) {
		Button1 = button1;
	}

	public javax.swing.JButton getButton2() {
		return Button2;
	}

	public void setButton2(javax.swing.JButton button2) {
		Button2 = button2;
	}

	public javax.swing.JButton getButton3() {
		return Button3;
	}

	public void setButton3(javax.swing.JButton button3) {
		Button3 = button3;
	}

	public javax.swing.JButton getButton4() {
		return Button4;
	}

	public void setButton4(javax.swing.JButton button4) {
		Button4 = button4;
	}

	public javax.swing.JButton getButton5() {
		return Button5;
	}

	public void setButton5(javax.swing.JButton button5) {
		Button5 = button5;
	}

	public javax.swing.JButton getButton6() {
		return Button6;
	}

	public void setButton6(javax.swing.JButton button6) {
		Button6 = button6;
	}

	public javax.swing.JButton getButton7() {
		return Button7;
	}

	public void setButton7(javax.swing.JButton button7) {
		Button7 = button7;
	}

	public javax.swing.JButton getButton8() {
		return Button8;
	}

	public void setButton8(javax.swing.JButton button8) {
		Button8 = button8;
	}

	public javax.swing.JButton getButton9() {
		return Button9;
	}

	public void setButton9(javax.swing.JButton button9) {
		Button9 = button9;
	}

	public javax.swing.JTextField getTextField() {
		return TextField;
	}

	public void setTextField(javax.swing.JTextField textField) {
		TextField = textField;
	}

	public javax.swing.JButton getAdd() {
		return add;
	}

	public void setAdd(javax.swing.JButton add) {
		this.add = add;
	}

	public javax.swing.JPanel getBackPanel() {
		return backPanel;
	}

	public void setBackPanel(javax.swing.JPanel backPanel) {
		this.backPanel = backPanel;
	}

	public javax.swing.JButton getDivide() {
		return divide;
	}

	public void setDivide(javax.swing.JButton divide) {
		this.divide = divide;
	}

	public javax.swing.JButton getEqual() {
		return equal;
	}

	public void setEqual(javax.swing.JButton equal) {
		this.equal = equal;
	}

	public javax.swing.JButton getMulti() {
		return multi;
	}

	public void setMulti(javax.swing.JButton multi) {
		this.multi = multi;
	}

	public javax.swing.JButton getPoint() {
		return point;
	}

	public void setPoint(javax.swing.JButton point) {
		this.point = point;
	}

	public javax.swing.JButton getSub() {
		return sub;
	}

	public void setSub(javax.swing.JButton sub) {
		this.sub = sub;
	}
    
    private javax.swing.JButton Button0;
    private javax.swing.JButton Button1;
    private javax.swing.JButton Button2;
    private javax.swing.JButton Button3;
    private javax.swing.JButton Button4;
    private javax.swing.JButton Button5;
    private javax.swing.JButton Button6;
    private javax.swing.JButton Button7;
    private javax.swing.JButton Button8;
    private javax.swing.JButton Button9;
    private javax.swing.JTextField TextField;
    private javax.swing.JButton add;
    private javax.swing.JPanel backPanel;
    private javax.swing.JButton divide;
    private javax.swing.JButton equal;
    private javax.swing.JButton CEButton;
    private javax.swing.JButton multi;
    private javax.swing.JButton point;
    private javax.swing.JButton sub;
    // End of variables declaration                   
}
最近下载更多
Xiaoyang_7787  LV1 2023年3月3日
wadadd  LV7 2022年9月13日
风允婼  LV1 2021年6月20日
dengge123  LV13 2021年6月3日
荒唐的羊  LV27 2020年12月14日
VICTORYll  LV15 2020年12月10日
段朝洪  LV15 2020年11月21日
liangge2115  LV27 2020年10月23日
ma406805131  LV15 2020年6月18日
2869580060  LV10 2020年5月26日
最近浏览更多
好烦好烦 9月4日
暂无贡献等级
xlqb999  LV3 4月29日
clumsy191 2023年12月17日
暂无贡献等级
gk_xmj  LV1 2023年12月6日
fesfefe  LV13 2023年10月3日
lmml2307831267  LV2 2023年6月25日
微信网友_6533174388953088  LV1 2023年6月24日
ja12121  LV2 2023年6月17日
另类清晨  LV8 2023年5月15日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友