实现效果
友情提示:建议您把代码复制到自己的编译器里去研究算法,博文看着实属有些累,前提您需要一定的 java 基础哦~
网格袋类
package jsj;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class LayoutUtil {
//参数含义介绍 1. Container container 容器对象
// 2. int fill 填充方式
// 3. int anchor 停靠位置(对齐方式)
// 4. int weightx 水平权值 与填充方式有内在关系
// 5. int weighty 垂直权值 与填充方式有内在关系
// 6. int x 水平起点
// 7. int y 垂直起点
// 8. int width 水平宽度(水平占锯的单元格数)
// 9. int height 垂直宽度(垂直占锯的单元格数)
// 10. Component comp 组件对象
public static void add(Container container,int fill,
int anchor,int weightx,int weighty,
int x,int y,int width,int height,Component comp){
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=fill;
constraints.anchor=anchor;
constraints.weightx=weightx;
constraints.weighty=weighty;
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=width;
constraints.gridheight=height;
container.add(comp,constraints);
} // 参数含义同上,只新增一个 外边距参数
public static void add(Container container,int fill,
int anchor,int weightx,int weighty, // Insets insets 外边距填充方式
int x,int y,int width,int height,Component comp,Insets insets){
GridBagConstraints constraints=new GridBagConstraints();
constraints.insets=insets;
constraints.fill=fill;
constraints.anchor=anchor;
constraints.weightx=weightx;
constraints.weighty=weighty;
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=width;
constraints.gridheight=height;
container.add(comp,constraints);
}
}
计算器的实现
package jsj;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import jsj.LayoutUtil;
public class Calculator extends JFrame {
static int count1=0;//计数第一位数点击按钮次数,用来判断小数位数,除以多少个10
static int count2=0;//计数第二位数点击按钮次数,用来判断小数位数,除以多少个10
static int k=0; //判断是否点过运算符+-*/
static float a=0; //存第一位计算数
static float b=0; //存第二位计算数
static float c=0; //存运算结果
static int pointt=0;//判断是否点过小数点 .
static int jiat=0;//判断是否点过加号+
static int jiant=0;//判断是否点过减号-
static int chengt=0;//判断是否点过乘号*
static int chut=0;//判断是否点过除号/
/*各个按钮*/
JButton jia=new JButton("+");
JButton jian=new JButton("-");
JButton cheng=new JButton("*");
JButton chu=new JButton("/");
JButton one=new JButton("1");
JButton two=new JButton("2");
JButton three=new JButton("3");
JButton four=new JButton("4");
JButton five=new JButton("5");
JButton six=new JButton("6");
JButton seven=new JButton("7");
JButton eight=new JButton("8");
JButton nine=new JButton("9");
JButton zero=new JButton("0");
JButton clean=new JButton("CE");
JButton point=new JButton(".");
JButton result=new JButton("=");
/*显示文字与运算结果面板*/
JPanel showJPanel=new JPanel();
/*显示最上面文字标签*/
JLabel showresult2=new JLabel("凤兮凤兮归故乡,遨游四海求其凰");
/*显示运算结果标签*/
JLabel showresult=new JLabel("0");
/*存放各个按钮面板*/
JPanel show=new JPanel();
public static void main(String[] args) {
Calculator calculator=new Calculator();
calculator.setVisible(true);
}
public Calculator(){
super();
setTitle("计算器");//设置框体标题
setBounds(300, 300, 300, 250);//设置框体默认大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
showresult2.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null,
null));//给最上方文字加边框
showresult.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null,
null));//给运算结果加边框
showresult2.setForeground(new Color(255, 0, 0));//给最上方文字设置红色
showresult2.setFont(new Font("", Font.BOLD, 15));//给最上方文字设置大小
showJPanel.setLayout(new GridBagLayout());//对显示文字与运算结果面板设置网格袋布局
LayoutUtil.add(showJPanel,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,0,1,1,showresult2);
LayoutUtil.add(showJPanel,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,1,0,0,1,1,1,showresult);
getContentPane(<