实验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); //设置窗体状态显示

}

}

原文地址:https://www.cnblogs.com/jcdz/p/11073667.html

时间: 2024-11-08 13:40:54

实验11 12 13的相关文章

多线程-线程一打印1,2,3,4,5线程二打印6,7,8,9,10,线程三打印11,12,13,14,15,...直到45结束

多线程-线程一打印1,2,3,4,5线程二打印6,7,8,9,10,线程三打印11,12,13,14,15,...知道45结束 public class Exam3{ public static void main(String[]args){ MyThread t1=new MyThread("线程一"); MyThread t2=new MyThread("线程二"); MyThread t3=new MyThread("线程三"); t1.

剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

1 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10. 2 思路和方法 直接定义一个矩形,在矩形的四条边取值,程序大大简化. 3 核心代码 1 class Solution { 2 public: 3 vector<int> printMatrix(vector<

一个嵌套很多层的列表,如l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]],用递归取出所有的值

'''一个嵌套很多层的列表,如l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]],用递归取出所有的值''' l=[1,2,[3,[4,5,6,[7,8,[9,10,[11,12,13,[14,15]]]]]]] def get(seq): for item in seq: if type(item) is list: get(item) else: print(item) get(l) 原文地址:https://www.cnblogs.com/

11 12 13 14 py单引号字符串、双引号字符串和转义符 字符串拼接 保持字符串的原汁原味

第11课 单引号字符串.双引号字符串和转义符 # 单引号字符串.双引号字符串和转义符 print('Hello World') print("Hello World") print("Let's go!") print('"OK"') print("'H',\"W\"") -------------------------- 输出结果 Let's go! "OK" 'H',"W

11.10/11.11/11.12 安装PHP511.13安装PHP7

- 11.10/11.11/11.12 安装PHP5 - 11.13 安装PHP7 - 扩展 - php中mysql,mysqli,mysqlnd,pdo到底是什么 - http://blog.csdn.net/u013785951/article/details/60876816 - 查看编译参数 http://ask.apelearn.com/question/1295 # 11.10安装PHP5 上 -  PHP官网www.php.net -  当前主流版本为5.6/7.1  1.   c

【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/

前言 Android Studio(以下简称AS)在13年I/O大会后放出预览版到现在放出的正式版1.0(PS.今天又更新到1.0.1了)历时一年多了,虽然Google官方推出的Android开发者的IDE对我们Android DEV是很有吸引力的,但考虑到beta版还是太多问题所以自己主要还是把AS当做尝鲜为主,每放出一个较大更新就下载下来试试,感觉还是挺好的,渐渐用AS的人越来越多,Github上的项目也基本是AS的了,Google的sample也采用AS,所以使用Eclipse跟外界交流越

11.10/11.11/11.12 安装PHP5 11.13 安装PHP7

11.10/11.11/11.12 安装PHP5根据提示完成安装,安装过程中会报错,按实际报错的需要安装我这安装出现这个错误:configure: error: mcrypt.h not found. Please reinstall libmcrypt.解决办法是安装这2个包:首行安装:yum install -y epel-release再安装:yum install -y libmcrypt-devel 因为这个包是依赖上一个包的查看加载的模块查看apache的配置文件输入/php5.so

已知如下数组: var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]; 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组

已知如下数组: var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]; 编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组 var dt= arr.toString().split(",").sort(function(a,b){return a-b}).map(Number);Array.from(new Set(dt)) 代码如下 var d

LINUX块设备驱动&lt;12/13/14/15&gt;

第 12章 +---------------------------------------------------+ |                 写一个块设备驱动                   | +---------------------------------------------------+ | 作者:赵磊                                         | | email: [email protected]