组件的定义:- private JRadioButton jRB_2,jRB_8,jRB_10,jRB_16; //进制转换单选按钮
- private JTextField jTextField; //显示文本框
- private JPanel jPanel1,jPanel2;
- private JButton[] jButtons;
- private JButton jb_delete,jb_clear;
- private static int curFlag = 0; //标识当前进制,取值为2、8、10、16,初始化为0
- private static String displayStr = "";
-
-
- public JZChange(){
- //设置标题
- super("进制转换器");
- //初始化面板
- jRB_2 = new JRadioButton("二进制"); //二进制单选按钮
- jRB_2.setFont(new Font("宋体",Font.PLAIN,12));
- jRB_8 = new JRadioButton("八进制"); //八进制单选按钮
- jRB_8.setFont(new Font("宋体",Font.PLAIN,12));
- jRB_10 = new JRadioButton("十进制"); //十进制单选按钮
- jRB_10.setFont(new Font("宋体",Font.PLAIN,12));
- jRB_16 = new JRadioButton("十六进制"); //十六进制单选按钮
- jRB_16.setFont(new Font("宋体",Font.PLAIN,12));
- jb_delete = new JButton("删减");
- jb_delete.setFont(new Font("宋体",Font.PLAIN,12));
- jb_clear = new JButton("清空");
- jb_clear.setFont(new Font("宋体",Font.PLAIN,12));
-
- jPanel1 = new JPanel(new GridLayout(3,2));
- jPanel1.add(jRB_2);
- jRB_2.addActionListener(this);
- jPanel1.add(jRB_8);
- jRB_8.addActionListener(this);
- jPanel1.add(jRB_10);
- jRB_10.addActionListener(this);
- jPanel1.add(jRB_16);
- jRB_16.addActionListener(this);
- jPanel1.add(jb_clear);
- jb_clear.addMouseListener(this);
- jPanel1.add(jb_delete);
- jb_delete.addMouseListener(this);
- jTextField = new JTextField();
- jTextField.setEditable(false);
- //jTextField.setFont(new Font("新宋体",Font.BOLD,17));
-
- jPanel2 = new JPanel(new GridLayout(4,4));
- jButtons = new JButton[16];
- //初始化0-9的数字按钮
- for(int i = 0;i < 10;i++){
- String digitStr = "";
- digitStr = digitStr+i;
- //System.out.println(digitStr);
- jButtons[i] = new JButton(digitStr);
- }
- //初始化A-Z的字母按钮
- for(int i = 10;i < 16;i++){
- String letterStr = "";
- char letter = (char)(i+55);
- letterStr = letterStr+letter;
- //System.out.println(letterStr);
- jButtons[i] = new JButton(letterStr);
- }
- //添加按钮
- for(int i = 0;i < 16;i++){
- jPanel2.add(jButtons[i]);
- jButtons[i].addMouseListener(this);
- }
- this.add(jTextField,BorderLayout.NORTH);
- this.add(jPanel1,BorderLayout.CENTER);
- this.add(jPanel2,BorderLayout.SOUTH);
- //初始化按钮状态
- for(int i = 0;i < 16;i++){
- jButtons[i].setEnabled(false);
- }
- this.setSize(250,250);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLocationRelativeTo(null);
- this.setVisible(true);
- }
复制代码 |