欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

优化版计算器 博客分类: Java基础 小程序 

程序员文章站 2024-03-14 15:21:34
...
在做完xp画板后对计算器进行了一次优化改进
图片:
首先是优化了界面的样式和代码,界面采用了默认的边框布局
private void showUI() {
// TODO Auto-generated method stub
JFrame jf = new JFrame();
jf.setTitle("计算器");
jf.setSize(new Dimension(220, 350));
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setResizable(false);
createBackground(jf);
createMenuPane(jf);
createShowPane(jf);
createFuctionPane(jf);
jf.setVisible(true);
}
在设计界面上加入了菜单栏,以及增加了一些功能按钮
private void createMenuPane(JFrame jf) {
// TODO Auto-generated method stub
JMenuBar jm = new JMenuBar();
String [] array = {"查看","编辑","帮助"};
String [][] arrayItem = {{"标准型","科学型","程序员"},{"复制","粘贴"},
{"查看帮助","开发者"}};
MenuListener ml = new MenuListener();
//利用数组进行循环,创建JMenu对象
for(int i=0;i<array.length;i++){
JMenu menu = new JMenu(array[i]);

for(int j=0;j<arrayItem[i].length;j++){
JMenuItem jbm = new JMenuItem(arrayItem[i][j]);
menu.add(jbm);

jbm.addActionListener(ml);
}
jm.add(menu);
}
jf.add(jm,BorderLayout.NORTH);
}
这是菜单栏的代码,虽然对于查看和编辑栏的功能还没有实现
private void createShowPane(JFrame jf) {
// TODO Auto-generated method stub
JPanel panel = new JPanel();

panel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

text1.setPreferredSize(new Dimension(200, 50));

text1.setEditable(false);

text1.setText("0");

text1.setHorizontalAlignment(JTextField.RIGHT);
Font font1 = new Font("宋体", Font.BOLD, 20);
text1.setFont(font1);


panel.add(text1);

panel.setOpaque(false);
jf.add(panel, BorderLayout.CENTER);
}
文本显示框的代码

private void createFuctionPane(JFrame jf) {
// TODO Auto-generated method stub
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(220,220));
panel.setLayout(new BorderLayout());
JPanel NumP = new JPanel();
NumP.setLayout(new FlowLayout(FlowLayout.LEFT,7,7));

// 定义一个字符串数组
String[] s1 = { "mc", "mr", "ms", "m+","back", "ce", "c",
"*", "7", "8", "9", "chu", "4", "5",
"6", "cheng", "1", "2", "3", "jian", "0", "dian", "jia" };

//循环数组
for(int i=0;i<s1.length;i++){
//实例化ImageIcon的对象
ImageIcon image = new ImageIcon(this.getClass().getResource(s1[i]+".png"));
//System.out.println(image);
JButton jbu = new JButton(image);

jbu.setActionCommand(s1[i]);

if(s1[i].equals("0")){
jbu.setPreferredSize(new Dimension(73,27));
}else{
jbu.setPreferredSize(new Dimension(34,27));
}
//给事件源jbu添加一个动作监听器方法,绑定事件处理类的对象cl
jbu.addActionListener(cl);
NumP.add(jbu);
}
panel.add(NumP,BorderLayout.CENTER);
JPanel FucP = new JPanel();
FucP.setLayout(new FlowLayout(FlowLayout.LEFT, 7, 7));
FucP.setPreferredSize(new Dimension(50,0));
String[] s2 = { "m-", "gh", "bfh", "fs", "dh" };
//循环数组
for(int i=0;i<s2.length;i++){
//实例化ImageIcon的对象
ImageIcon image = new ImageIcon(this.getClass().getResource(s2[i]+".png"));
JButton jbu = new JButton(image);
//设置按钮的动作命令
jbu.setActionCommand(s2[i]);
if(s2[i].equals("dh")){
jbu.setPreferredSize(new Dimension(34,61));
}else{
jbu.setPreferredSize(new Dimension(34,27));
}
//给事件源jbu添加一个动作监听器方法,绑定事件处理类的对象cl
jbu.addActionListener(cl);
FucP.add(jbu);
}
panel.add(FucP, BorderLayout.EAST);
jf.add(panel, BorderLayout.SOUTH);
}
功能按钮的代码,可以实现基础四则运算和√,M+,M-等复杂运算,
其中由于由于文本框和运算监听器反复使用所以设置为了属性

private JTextField text1 = new JTextField();

private calListener cl = new calListener(text1);
相关标签: 小程序