实验11 12 13
Posted jcdz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验11 12 13相关的知识,希望对你有一定的参考价值。
实验11 实现功能
源代码:
import java.awt.Color;
import java.awt.Font;
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;
public class CaculatorDemo extends JFrame
private static final long serialVersionUID = 1L;
private StringBuilder sBuilder = new StringBuilder();
private Double a;//中间变量用于存储输入的第一个数
private Double b;//中间变量,用于存储输入的第二个数
private Double double1;//用于接收计算结果
private Integer i;// i用于表示加减乘除
public CaculatorDemo()
this.setTitle("计算器");
this.setSize(318, 457);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
this.getContentPane().add(panel);
panel.setLayout(null);
// 定义一个label用于显示输入数据和计算结果
final JLabel label = new JLabel();
label.setBounds(0, 0, 300, 50);
label.setFont(new Font("dialog", 1, 30));
label.setOpaque(true);// 由于jlabel默认透明,直接设置背景色无效,需要先将不透明设置为true
label.setBackground(Color.white);
panel.add(label);
// 定义按钮组件
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton button0 = new JButton("0");
JButton buttonadd = new JButton("+");// 加
JButton buttonminus = new JButton("-");// 减
JButton buttontime = new JButton("×");// 乘
JButton buttondivid = new JButton("÷");// 除
JButton buttonequal = new JButton("=");// 等于
JButton buttondecimal = new JButton(".");// 小数点
JButton buttondelet = new JButton("←");// 删除
JButton buttonclear = new JButton("C");// 清除
// 定义按钮组件位置
button0.setBounds(0, 50, 100, 60);
button0.setFont(new Font("dialog", 1, 30));
panel.add(button0);
button1.setBounds(100, 50, 100, 60);
button1.setFont(new Font("dialog", 1, 30));
panel.add(button1);
button2.setBounds(200, 50, 100, 60);
button2.setFont(new Font("dialog", 1, 30));
panel.add(button2);
button3.setBounds(0, 110, 100, 60);
button3.setFont(new Font("dialog", 1, 30));
panel.add(button3);
button4.setBounds(100, 110, 100, 60);
button4.setFont(new Font("dialog", 1, 30));
panel.add(button4);
button5.setBounds(200, 110, 100, 60);
button5.setFont(new Font("dialog", 1, 30));
panel.add(button5);
button6.setBounds(0, 170, 100, 60);
button6.setFont(new Font("dialog", 1, 30));
panel.add(button6);
button7.setBounds(100, 170, 100, 60);
button7.setFont(new Font("dialog", 1, 30));
panel.add(button7);
button8.setBounds(200, 170, 100, 60);
button8.setFont(new Font("dialog", 1, 30));
panel.add(button8);
button9.setBounds(0, 230, 100, 60);
button9.setFont(new Font("dialog", 1, 30));
panel.add(button9);
buttonadd.setBounds(100, 230, 100, 60);
buttonadd.setFont(new Font("dialog", 1, 30));
panel.add(buttonadd);//加
buttonminus.setBounds(200, 230, 100, 60);
buttonminus.setFont(new Font("dialog", 1, 30));
panel.add(buttonminus);//减
buttontime.setBounds(0, 290, 100, 60);
buttontime.setFont(new Font("dialog", 1, 30));
panel.add(buttontime);//乘
buttondivid.setBounds(100, 290, 100, 60);
buttondivid.setFont(new Font("dialog", 1, 30));
panel.add(buttondivid);//除
buttonequal.setBounds(200, 290, 100, 60);
buttonequal.setFont(new Font("dialog", 1, 30));
panel.add(buttonequal);//等于
buttondecimal.setBounds(0, 350, 100, 60);
buttondecimal.setFont(new Font("dialog", 1, 30));
panel.add(buttondecimal);//小数点
buttonclear.setBounds(100, 350, 100, 60);
buttonclear.setFont(new Font("dialog", 1, 30));
panel.add(buttonclear);//复位
buttondelet.setBounds(200, 350, 100, 60);
buttondelet.setFont(new Font("dialog", 1, 30));
panel.add(buttondelet);//删除
// 给各个按钮设置动作监听器
// 输入数值操作0~9
button0.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("0");
label.setText(sBuilder.toString());
);
button1.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("1");
label.setText(sBuilder.toString());
);
button2.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent e)
sBuilder.append("2");
label.setText(sBuilder.toString());
);
button3.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("3");
label.setText(sBuilder.toString());
);
button4.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("4");
label.setText(sBuilder.toString());
);
button5.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("5");
label.setText(sBuilder.toString());
);
button6.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("6");
label.setText(sBuilder.toString());
);
button7.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("7");
label.setText(sBuilder.toString());
);
button8.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("8");
label.setText(sBuilder.toString());
);
button9.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append("9");
label.setText(sBuilder.toString());
);
// 输入运算符操作,需要先判断a是否为0.0
buttonadd.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("+");
i = 0;
);
buttonminus.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("-");
i = 1;
);
buttontime.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("×");
i = 2;
);
buttondivid.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
a = Double.parseDouble(sBuilder.toString());
sBuilder = new StringBuilder();
label.setText("÷");
i = 3;
);
buttonequal.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
// 该判断中间变量是否为空
if (!"".equals(sBuilder.toString()) && (!(a == 0.0)))
b = Double.parseDouble(sBuilder.toString());
if (i == 0)
double1 = a + b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
else if (i == 1)
double1 = a - b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
else if (i == 2)
double1 = a * b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
else if (i == 3)
double1 = a / b;
label.setText(double1.toString());
sBuilder = new StringBuilder();
sBuilder.append(double1);
else
label.setText(sBuilder.toString());
);
buttondecimal.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder.append(".");
label.setText(sBuilder.toString());
);
buttonclear.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
sBuilder = new StringBuilder();
label.setText("");
);
buttondelet.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
if (!"".equals(sBuilder.toString()))
sBuilder.deleteCharAt(sBuilder.length() - 1);
label.setText(sBuilder.toString());
);
this.setVisible(true);
public static void main(String[] args)
new CaculatorDemo();
实验12 SWING界面设计
源程序:
ackage jiemian;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Container;
public class shao extends JFrame
public shao()
JFrame jf1=new JFrame ("简历");
jf1.setLayout(new FlowLayout());
jf1.getContentPane().add(new JButton("姓名")) ;
jf1.getContentPane().add(new JTextField("江才东周",25)) ;
jf1.getContentPane().add(new JButton("性别")) ;
jf1.getContentPane().add(new JTextField("男",25)) ;
jf1.getContentPane().add(new JButton("年龄")) ;
jf1.getContentPane().add(new JTextField("19",25)) ;
jf1.getContentPane().add(new JButton("民族")) ;
jf1.getContentPane().add(new JTextField("藏",25)) ;
jf1.getContentPane().add(new JButton("籍贯")) ;
jf1.getContentPane().add(new JTextField("青海玉树",25)) ;
jf1.getContentPane().add(new JButton("学院")) ;
jf1.getContentPane().add(new JTextField("计算机学院",25)) ;
jf1.getContentPane().add(new JButton("专业")) ;
jf1.getContentPane().add(new JTextField("网络工程",25)) ;
JPanel p1=new JPanel();
jf1.getContentPane().add(p1);
jf1.setSize(420,310);
Container conPane = getContentPane();
jf1.add(conPane);
jf1.setVisible(true);
public static void main (String[] args)
new shao();
实验13 窗口设计
源程序:
package chuangkou;
import java.awt.Font;
import javax.swing.*;
public class p
public static void main(String[] args)
// TODO 自动生成的方法存根
JFrame frame=new JFrame("简历"); //创建窗体
JPanel p=new JPanel(); //创建面板
frame.add(p);
frame.setResizable(false); //不可拖动窗体
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(500,300,560,340);
JTextField Field1=new JTextField(); //创建文本框
p.add(Field1);
JTextField Field2=new JTextField(); //创建文本框
p.add(Field2);
JTextField Field3=new JTextField(); //创建文本框
p.add(Field3);
JTextField Field4=new JTextField(); //创建文本框
p.add(Field4);
JLabel L1=new JLabel("姓 名:"); //创建标签
p.add(L1);
JLabel L2=new JLabel("性 别:");
p.add(L2);
JLabel L3=new JLabel("专 业:");
p.add(L3);
JLabel L4=new JLabel("年 龄:");
p.add(L4);
JLabel L5=new JLabel("兴趣爱好:");
p.add(L5);
JLabel L6=new JLabel("民 族:");
p.add(L6);
JLabel L7=new JLabel("籍 贯:");
p.add(L7);
JRadioButton r1=new JRadioButton("男"); //创建单选框
JRadioButton r2=new JRadioButton("女");
JRadioButton r3=new JRadioButton("网络工程"); //创建单选框
JRadioButton r4=new JRadioButton("物联网工程");
JRadioButton r5=new JRadioButton("软件工程"); //创建单选框
ButtonGroup group1=new ButtonGroup(); //创建一个组,将r1与r2放在一起
ButtonGroup group2=new ButtonGroup();
group1.add(r1);
group1.add(r2);
group2.add(r3);
group2.add(r4);
group2.add(r5);
p.add(r1);
p.add(r2);
p.add(r3);
p.add(r4);
p.add(r5);
frame.add(p);
JCheckBox box1=new JCheckBox("篮球"); //创建复选框
JCheckBox box2=new JCheckBox("足球");
JCheckBox box3=new JCheckBox("排球");
JCheckBox box4=new JCheckBox("羽毛球");
p.add(box1);
p.add(box2);
p.add(box3);
p.add(box4);
frame.add(p);
p.setLayout(null); //自定义组件位置
L1.setBounds(110,35,250,25);
L1.setFont(new Font("黑体",Font.BOLD,15)); //设置组件位置
L2.setBounds(110,60,90,30);
L2.setFont(new Font("黑体",Font.BOLD,15));
L3.setBounds(110,170,340,35);
L3.setFont(new Font("黑体",Font.BOLD,15));
L4.setBounds(110,140,140,25);
L4.setFont(new Font("黑体",Font.BOLD,15));
L5.setBounds(100,210,140,25);
L5.setFont(new Font("黑体",Font.BOLD,15));
L6.setBounds(110,115,340,25);
L6.setFont(new Font("黑体",Font.BOLD,15));
L7.setBounds(110,90,340,25);
L7.setFont(new Font("黑体",Font.BOLD,15));
r1.setBounds(200,60,90,30);
r2.setBounds(300,60,110,30);
r3.setBounds(170,172,110,40);
r4.setBounds(280,172,115,40);
r5.setBounds(390,172,130,40);
box1.setBounds(170,210,100,25);
box2.setBounds(270,210,100,25);
box3.setBounds(370,210,100,25);
box4.setBounds(470,210,100,25);
Field1.setBounds(170,35,250,22);
Field2.setBounds(170,140,250,22);
Field3.setBounds(170,90,250,22);
Field4.setBounds(170,115,250,22);
frame.setVisible(true); //设置窗体状态显示
以上是关于实验11 12 13的主要内容,如果未能解决你的问题,请参考以下文章