win7计算器(三)

JAVA | 2018-02-19 22:25:17 | 阅读 661 次 | 评论(0)

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.*;
import javax.swing.border.LineBorder;

public class MyCaculate extends JFrame {
private JPanel jp1,jp2;
private JButton jb;
private JTextField display1, display2, display3;
private JMenuBar jmb;
private JMenu jm;
private JMenuItem jmi;
private final String[] firstmenu = { "查看(V)", "编辑(E)", "帮助(H)" };
private final String[][] secondMenu = {
{ "标准型", "科学型", "程序员", "统计信息", "历史信息", "数字分组", "基本", "单位转换",
"日期计算", "工作表" }, { "复制(C)", "粘贴(P)", "历史记录" },
{ "查看帮助(V)", "关于计算器(A)" } };

private final char[] Mnemonic = { 'V', 'E', 'H' };
private String buttonsName[]={"MC","MR","MS","M+","M-",
"←","CE","C","±","√",
"7","8","9","/","%",
"4","5","6","*","1/x",
"1","2","3","-","=",
"0",".","+"};
public MyCaculate() {
// this.setTitle("计算器");
// this.setLocation(500, 200);
// this.setSize(230, 320);
// this.setBackground(new Color(217, 228, 241));
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setVisible(true);
}

protected JButton makebutton(String name,GridBagLayout gridbag,GridBagConstraints c) {
JButton button = new JButton(name);
gridbag.setConstraints(button, c);
return button;
}

public void initFace() {
jmb = new JMenuBar();
for (int i = 0; i < firstmenu.length; i++) {
jm = new JMenu(firstmenu[i]);
jm.setMnemonic(Mnemonic[i]);
for (int j = 0; j < secondMenu[i].length; j++) {
jmi = new JMenuItem(secondMenu[i][j]);
jm.add(jmi);
if (j == 3 || j == 5) {
jm.addSeparator();
}
}
jmb.add(jm);
}

this.setLayout(null);
display2 = new JTextField("0");
display2.setBounds(12, 12, 190, 50);
display2.setFont(new Font("宋体", Font.BOLD, 26));
display2.setHorizontalAlignment(JLabel.RIGHT);
display2.setEnabled(false);
display2.setDisabledTextColor(Color.BLACK);
display2.setBackground(new Color(247, 250, 254));
this.add(display2);
//做按钮组
jp1 = new JPanel();
jp1.setLayout(new GridLayout(4, 4, 2, 2));
jp1.setBounds(12, 70, 190, 132);
for (int i = 0; i < 20; i++) {
jb = new JButton(buttonsName[i]);
jb.setMargin(new Insets(0, 0, 0, 0));
jp1.add(jb);
}
this.add(jp1);
// jp2 = new JPanel();
// jp2.setLayout(new GridBagLayout());
// jp2.setBounds(12, 200, 190, 66);
// for (int i = 20; i < buttonsName.length; i++) {
// jb = new JButton(buttonsName[i]);
// jb.setMargin(new Insets(0, 0, 0, 0));
// jp2.add(jb);
// }
// this.add(jp2);
GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        jp2 = new JPanel();
jp2.setLayout(gridbag);
jp2.setFont(new Font("SansSerif", Font.PLAIN, 14));
        jp2.setBounds(12, 204, 190, 200);
for (int i = 20; i < buttonsName.length; i++) {
if (i == 24) {
c.gridwidth = 1;                //reset to the default
        c.gridheight = 2;
        c.weighty = 1.0;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
}else if (i == 25) {
c.gridwidth = GridBagConstraints.RELATIVE;
}else {
c.gridwidth = 1;                //reset to the default
        c.gridheight = 1;
        c.weighty = 1.0;
}
jb = makebutton(buttonsName[i], gridbag, c);
jb.setMargin(new Insets(0, 0, 0, 0));
jp2.add(jb);
}
        
//        c.gridwidth = GridBagConstraints.REMAINDER; //end row
//        makebutton("Button4", gridbag, c);
//
//        c.weightx = 0.0;                  //reset to the default
//        makebutton("Button5", gridbag, c); //another row
//
//          c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
//        makebutton("Button6", gridbag, c);
//
//          c.gridwidth = GridBagConstraints.REMAINDER; //end row
//        makebutton("Button7", gridbag, c);
//
//          c.gridwidth = 1;                //reset to the default
//          c.gridheight = 2;
//        c.weighty = 1.0;
//        makebutton("Button8", gridbag, c);
//
//        c.weighty = 0.0;                  //reset to the default
//          c.gridwidth = GridBagConstraints.REMAINDER; //end row
//          c.gridheight = 1;               //reset to the default
//        makebutton("Button9", gridbag, c);
//        makebutton("Button10", gridbag, c);
        
        
this.add(jp2);
// display1 = new JTextField("");
// display2 = new JTextField("0");
// display3 = new JTextField("");
// display1.setBounds(0, 0, 204, 26);
// display2.setBounds(20, 20, 184, 34);
// display3.setBounds(0, 20, 20, 34);
// display1.setHorizontalAlignment(JLabel.RIGHT);
// display2.setHorizontalAlignment(JLabel.RIGHT);
// display3.setHorizontalAlignment(JLabel.CENTER);
// display1.setFont(new Font("宋体", Font.PLAIN, 12));
// display2.setFont(new Font("宋体", Font.BOLD, 20));
// display3.setFont(new Font("宋体", Font.PLAIN, 20));
// display1.setDisabledTextColor(Color.BLACK);
// display2.setDisabledTextColor(Color.BLACK);
// display3.setDisabledTextColor(Color.BLACK);
// display1.setBorder(new LineBorder(new Color(242, 247, 252)));
// display2.setBorder(new LineBorder(new Color(242, 247, 252)));
// display3.setBorder(new LineBorder(new Color(242, 247, 252)));
// display1.setBackground(new Color(242, 247, 252));
// display2.setBackground(new Color(242, 247, 252));
// display3.setBackground(new Color(242, 247, 252));
//
// this.add(display1);
// this.add(display2);
// this.add(display3);
// jm = new JMenu("查看(V)");
// jmi = new JMenuItem("标准型");
// 将菜单项放到对应的菜单中,将菜单放到菜单栏中
// jm.add(jmi);
// jmb.add(jm);
// 为当前窗体设置菜单栏
// this.setLayeredPane(null);
this.setJMenuBar(jmb);
this.setTitle("计算器");
this.setLocation(500, 200);
this.setSize(230, 480);
this.setBackground(new Color(217, 228, 241));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
// this.setBackground(new Color(255, 0, 0));
}

public static void main(String[] args) {
new MyCaculate().initFace();
}
}

文章评论,共0条
游客请输入验证码
最新评论
  • 詹小不点:佩服
  • 陈同志:厉害