java课程设计-计算器
运行截图
所包含的类:
CalculatorFram包主函数的类,整合各个面板
package Calculator;
import javax.swing.*;
import java.awt.*;
public class CalculatorFram extends JFrame {
public CalculatorFram() {
super("计算器");
Toolkit kit = Toolkit.getDefaultToolkit();//绑定本地系统,用于查询本机的一些信息
Dimension screenSize = kit.getScreenSize();//获取屏幕分辨率,Dimension实例有wight和height属性,用来存组件的大小
setSize(screenSize.width/9*5,screenSize.height/5*3);//大小
setLocation(screenSize.width/4,screenSize.height/4);//位置
this.setBackground(new Color(248,248,248));
setLayout(new BorderLayout());
ImageIcon imageIcon=new ImageIcon(".\\tubiao.png");
this.setIconImage(imageIcon.getImage());
LogPanel logPanel = new LogPanel();
add(logPanel,BorderLayout.EAST);//新建log面板添加到主窗口
JPanel panel= new JPanel();
panel.setLayout(new BorderLayout());//新建panel面板
TextPanel textPanel = new TextPanel();
panel.add(textPanel, BorderLayout.NORTH);//新建结果显示窗口并添加到panel
CalculatePanel calculatePanel=new CalculatePanel(textPanel, logPanel);
panel.add(calculatePanel,BorderLayout.CENTER);//新建按钮面板并添加到panel面板
add(panel,BorderLayout.CENTER);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
CalculatorFram c = new CalculatorFram();
}
}
LogPanel存放历史记录面板
package Calculator;
import javax.swing.*;
import java.awt.*;
public class LogPanel extends JPanel {
JTextArea log;
StringBuffer logMessage = new StringBuffer("历史记录:\n\n");
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
JLabel label ;
public LogPanel() {
setLayout(gridBag);
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1.0;
gc.weighty = 1.0;
log = new JTextArea( 2, 23);
log.setBackground(new Color(243,243,243));
log.setBorder(null);
log.setText(logMessage.toString());
log.setSelectedTextColor(Color.RED);
log.setFont(new Font("黑体",Font.BOLD,15));
log.setEditable(false);
log.setLineWrap(true); //激活自动换行功能
log.setWrapStyleWord(true); //设置在单词过长的时候是否要把长单词移到下一行。
JScrollPane scroll = new JScrollPane(log);
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//设置滚动条自动出现
scroll.setBackground(Color.BLUE);
scroll.setBorder(null); //添加滚动条的jtextarea设置无边框
scroll.setOpaque(true); //设置背景透明
gc.gridx=0;gc.gridy=1;
add(scroll,gc);
TextPanel text= new TextPanel();
}
}
TextPanel显示区面板
package Calculator;
import javax.swing.*;
import java.awt.*;
public class TextPanel extends JPanel {
JLabel hint = new JLabel(" ");
JLabel input = new JLabel(" ");
JLabel answer= new JLabel("0");
public TextPanel() {
setLayout(new BorderLayout());
this.setBackground(new Color(244,244,244));
answer.setHorizontalAlignment(JLabel.RIGHT);
hint.setHorizontalAlignment(JLabel.RIGHT);
input.setHorizontalAlignment(JLabel.RIGHT);//设置右对齐
answer.setFont(new Font("黑体",Font.BOLD, 35));
input.setFont(new Font("黑体",Font.PLAIN, 25));
hint.setFont(new Font("宋体",Font.BOLD, 25));
add(hint,BorderLayout.NORTH);
add(input,BorderLayout.CENTER)