您的位置:首页 > 产品设计 > UI/UE

Java GUI计算器实现

2017-08-15 16:57 615 查看
一个Java GUI计算器,功能完善,界面与逻辑处理分离。

全部代码下载传送:http://download.csdn.net/download/weixin_39858069/9934981

有需要的可以看看哦。

界面部分代码

package com.calc.ui;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.calc.Util.ScreenUtil;
import com.calc.logic.JButtonLogic;
import com.calc.logic.TextAreaLogic;
public class MainUI {
// 设置计算器高度为:屏幕高度的3/4,宽度为:屏幕宽度的1/4
private final int HEIGHT = (int) (ScreenUtil.Height * 0.75);
private final int WIDTH = ScreenUtil.Width / 4;
// 设置计算器的起始位置
private final int POINT_X = ScreenUtil.Width / 2;
private final int POINT_Y = ScreenUtil.Height / 8;
// 设置窗口名称
private final String WINDOW_NAME = "计算器";
// 设置通用字体
private final Font font = new Font("Times New Roman", Font.BOLD, 30);
private final Font font1 = new Font("Times New Roman", Font.BOLD, 40);
private final Color BG = new Color(245,245,245);
private final Color TEXTAREA = new Color(230,230,250);
private JFrame frame;
private JPanel buttonPanel;
private JPanel rootPanel;
private static JTextArea textArea;
public void drawMainUI() {
initFrame();
initTextArea();
initButton();
initLast();

}

public static JTextArea getJTextArea() {
return textArea;
}
/**
* 初始化最后操作,添加Jpanel到JFrame上,设置JFrame其它属性。
*/
private void initLast() {
rootPanel = new JPanel();
rootPanel.setLayout(new GridLayout(2, 1));
//给textarea添加上滚动条
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBorder(null);
rootPanel.add(scrollPane);
rootPanel.add(buttonPanel);
frame.add(rootPanel);
frame.setVisible(true);
}
/**
* 初始化主框架
*/
private void initFrame() {
frame = new JFrame(WINDOW_NAME);
frame.setSize(WIDTH, HEIGHT);
frame.setLocation(POINT_X, POINT_Y);
frame.setResizable(false);
//设置边距
((JPanel)frame.getContentPane()).setBorder(BorderFactory.createEmptyBorder(0,3,0,3));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* 初始化显示框
*/
private void initTextArea() {
textArea = new JTextArea("0");
textArea.setFont(font);
textArea.setLineWrap(true);// 自动换行
textArea.setWrapStyleWord(true);// 断行不断字
textArea.setBackground(TEXTAREA);
textArea.setEditable(false);
textArea.setSize(WIDTH - 10, 20);
TextAreaLogic.addKeyListener(textArea); //添加键盘监听事件
}
/**
* 初始化按钮
*/
private void initButton() {
String allChars = "AC÷×789-456+123.";
JButton button = null;
buttonPanel = new JPanel();
buttonPanel.setBackground(BG);
buttonPanel.setLayout(new GridLayout(0, 1));
buttonPanel.requestFocus();
char[] chars = allChars.toCharArray();
JPanel panel = new JPanel();
for (int i = 0; i < chars.length; i++) {
if(i % 4 == 0){
panel = new JPanel();
panel.setLayout(new GridLayout(1,0));
}
button = new JButton(String.valueOf(chars[i]));
commonButtonSetting(button);
panel.add(button);
buttonPanel.add(panel);
}

panel = new JPanel();
panel.setLayout(new GridLayout(1,0,10,10));
panel.setBorder(BorderFactory.createEmptyBorder(0,10,0,10));
for(char c:"0=".toCharArray()){
button = new JButton(String.valueOf(c));
commonButtonSetting(button);
button.setFont(font1);
panel.add(button);

}
buttonPanel.add(panel);;
}

private void commonButtonSetting(JButton button){	;
button.setFont(font);
button.setContentAreaFilled(false); // 设置背景色透明
button.setBorderPainted(false); // 设置不绘制边框
button.setFocusPainted(false); // 设置选中时不显示文字周围框框
JButtonLogic.addActionListener(button,textArea);// 添加点击事件
JButtonLogic.addMouseListener(button);// 添加鼠标事件
}
}


部分截图





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java gui 界面