计算器界面代码
package jisuan; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Label; import javax.swing.*; public class jisuan{ private JPanel panel0; private JPanel panel1; private JLabel text1, text2; JFrame frame; public jisuan(){ frame = new JFrame("科学计算器"); frame.setLayout(new GridLayout(2,1,3,3));//两行一列,水平距离和垂直距离为三 Container tainer = frame.getContentPane(); panel0 = new JPanel(new GridLayout(2,1,3,3)); text1 = new JLabel("kjsdhjkhfjk"); text1.setFont(new Font("隶书",1,20));//设置字体 text1.setHorizontalAlignment(SwingConstants.RIGHT);//右对齐 text1.setBorder(BorderFactory.createLineBorder(Color.black, 2));//边框为黑色,线条粗细为2 text2 = new JLabel("khjhkgk"); text2.setFont(new Font("隶书",1,20)); text2.setHorizontalAlignment(SwingConstants.RIGHT); text2.setBorder(BorderFactory.createLineBorder(Color.black, 2)); panel0.add(text1); panel0.add(text2); frame.add(panel0); JButton b[]=new JButton[16]; panel1 = new JPanel(new GridLayout(4,4));//四行四列 String str[]={"1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", "/", "=", "C"}; for (int i=0; i<16; i++) { b[i] = new JButton(str[i]); if (i==3 || i==7 || i==11 || i==13 ||i==14||i==15)//设置字体颜色为红色 b[i].setForeground(Color.red); panel2.add(b[i]); } panel1.setSize(300, 300); tainer.add(panel1); frame.setSize(300,300); frame.setVisible(true); } public static void main(String[] args){ new jisuan(); } }
原文:http://blog.csdn.net/u010499449/article/details/38492099