实验一 设计以下应用程序的界面
一、实验目的
掌握图形用户界面中的容器和组件的基本设计方法
二、实验内容
1、设计一个用户注册页面
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MyJFrame extends JFrame{
JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
JTextField jtf;//文本框
JPasswordField jpf1,jpf2;//密码
JTextArea jta;
JButton jb1,jb2;
JRadioButton jrb1,jrb2;
JCheckBox jcb1,jcb2,jcb3;
JComboBox<String> jc;
public MyJFrame() {
this.setTitle("注册");
setBounds(300,300,500,600);
//设置窗体为流式布局
//setLayout(new FlowLayout());
//设置窗体为网格布局
setLayout(new GridLayout(20,1));
//空布局
setLayout(null);
init();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jb1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
String info="";
info="姓名:"+jtf.getText();
info+="\r\n"+"密码:"+jpf1.getText();
info+="\r\n"+"密码:"+jpf2.getText();
info+="\r\n"+"性别:";
if (jrb1.isSelected())
info+=jrb1.getText();
if (jrb2.isSelected())
info+=jrb2.getText();
info+="\r\n爱好:";
if(jcb1.isSelected())
info+=jcb1.getText();
if(jcb2.isSelected())
info+=jcb2.getText();
if(jcb3.isSelected())
info+=jcb3.getText();
info+="\r\n学历:"+jc.getSelectedItem();
info+="\r\n简历:"+jta.getText();
JOptionPane.showMessageDialog(null, info);
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// System.exit(0);
//获取事件源对象
JButton jb=(JButton)e.getSource();
jtf.setText(jb.getText());
}
});
}
//注册用的组件初始化
public void init() {
// TODO Auto-generated method stub
jl1=new JLabel("用户名:");
jl2=new JLabel("密码:");
jl7=new JLabel("再次密码:");
jl3=new JLabel("简历:");
jl4=new JLabel("性别:");
jtf=new JTextField(10);
jpf1=new JPasswordField(10);
jpf2=new JPasswordField(10);
jta=new JTextArea(4,20);
jb1=new JButton("注册");
jb2=new JButton("取消");
jl1.setBounds(100,30,180,30);
jtf.setBounds(200,30,180,30);
jl2.setBounds(100, 70, 180, 30);
jpf1.setBounds(200, 70, 180, 30);
jl7.setBounds(100, 110, 180, 30);
jpf2.setBounds(200, 110, 180, 30);
jl3.setBounds(100, 280, 100, 30);
jta.setBounds(100,280,400,200);
JScrollPane jsp=new JScrollPane(jta);
jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jsp.setBounds(170, 280, 240, 100);
jb1.setBounds(100, 400, 100, 30);
jb2.setBounds(220, 400, 100, 30);
jrb1=new JRadioButton("男");
jrb2=new JRadioButton("女");
jl4.setBounds(100, 150, 180, 30);
jrb1.setBounds(200,150, 100, 30);
jrb2.setBounds(300,150, 100, 30);
ButtonGroup bg=new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jl5=new JLabel("爱好:");
jcb1=new JCheckBox("游戏");
jcb2=new JCheckBox("篮球");
jcb3=new JCheckBox("旅游");
jl5.setBounds(100 , 240, 100, 30);
jcb1.setBounds(160 , 240, 100, 30);
jcb2.setBounds(260 , 240, 100, 30);
jcb3.setBounds(360 , 240, 100, 30);
jl6=new JLabel("学历:");
jc=new JComboBox<>();
jc.addItem("高中");
jc.addItem("大专");
jc.addItem("本科");
jc.addItem("研究生");
jl6.setBounds(100 , 200, 100, 30);
jc.setBounds(200 , 200, 100, 30);
add(jl1);
add(jtf);
add(jl2);
add(jpf1);
add(jl7);
add(jpf2);
add(jl3);
add(jsp);
add(jl4);
add(jrb1);
add(jrb2);
add(jl5);
add(jcb1);
add(jcb2);
add(jcb3);
add(jl6);
add(jc);
add(jb1);
add(jb2);
}
public static void main(String[] args) {
new MyJFrame();
}
}
2、设计一个用户登录页面
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
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.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
public class Regulation extends JFrame{
JLabel jl1,jl2,jl3,jl4;
JTextField jtf;//文本框
JPasswordField jpf;//密码
JButton jb1,jb2;
public Regulation() {
this.setTitle("登录窗口");
setBounds(300,300,500,400);
//设置窗体为流式布局
setLayout(new GridLayout(20,1));
//空布局
setLayout(null);
init();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String info="";
info="姓名:"+jtf.getText();
info+="\r\n"+"密码:"+jpf.getText();
JOptionPane.showMessageDialog(null, info);
}
});
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// System.exit(0);
//获取事件源对象
JButton jb=(JButton)e.getSource();
jtf.setText(jb.getText());
}
});
}
public void init() {
jl1=new JLabel("企业人事管理系统");
jl2=new JLabel("用户名:");
jl3=new JLabel("密码:");
jtf=new JTextField(10);
jpf=new JPasswordField(10);
jb1=new JButton("注册");
jb2=new JButton("取消");
jl1.setBounds(150,30,200,60);
jl2.setBounds(100, 120, 180, 30);
jtf.setBounds(200, 120, 180, 30);
jl3.setBounds(100, 180, 180, 30);
jpf.setBounds(200, 180, 180, 30);
jb1.setBounds(100, 260, 100, 30);
jb2.setBounds(220, 260, 100, 30);
jl1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.blue, Color.blue));
Font font = new Font("楷体",Font.PLAIN,25);
jl1.setFont(font);
jl1.setForeground(Color.red);
add(jl1);
add(jl2);
add(jtf);
add(jl3);
add(jpf);
add(jb1);
add(jb2);
}
public static void main(String[] args) {
new Regulation();
}
}
3、设计一个计算器的页面
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import javax.swing.border.BevelBorder;
public class CalcJFrame extends JFrame{
JButton[] jbs;
JTextField jtf;
String[] cons={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","/","="};
JPanel jp1;
String op1="",ops="";
double num1,num2;
//flag=0,表示连续输入操作数,flag=1表示新的操作数开始
int flag=0;
public CalcJFrame() {
setTitle("计算器");
setBounds(400,300,400,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
for(int i=0;i<16;i++){
jbs[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton jb=(JButton)e.getSource();
//获取用户单击的按钮上面的文本
char ch=jb.getText().charAt(0);
//判断单击的按钮属于哪个类型
if (ch>='0' && ch<='9'|| ch=='.'){
//如果是数字
if (flag==1) {
//如果之前进行了计算,文本框中是计算结果,准备接收下一个操作数,op1清空,
op1="";
//准备接收下一个操作数的连续输入
flag=0;
}
//数字连接,并显示在文本框
op1=op1+ch;
jtf.setText(op1);
}else if (ch=='='){//表示要对两个操作数进行运算
//记录刚才单击的是等号
flag=1;
//获取第二个操作数
num2=Double.parseDouble(op1);
//根据运算符进行计算
switch(ops){
case "+": num1=num1+num2;break;
case "-": num1=num1-num2;break;
case "*": num1=num1*num2;break;
case "/": if (num2!=0) {num1=num1/num2;}
else JOptionPane.showMessageDialog(null, "出错!除数不能为零");
num1=0.0;break;
}
//运算符清空
ops="";
//将结果放在op1中
op1=num1+"";
//结果显示在文本框
jtf.setText(op1);
}else{//单击了运算符,表示第二个操作数录入结束
if (ops!=""){//如果连续运算
//记录要进行计算,
flag=1;
//将文本框中的内容保存在num2中
num2=Double.parseDouble(jtf.getText());
//根据运算符计算结果
switch(ops){
case "+": num1=num1+num2;break;
case "-": num1=num1-num2;break;
case "*": num1=num1*num2;break;
// case "/": if (num2!=0) num1=num1/num2;break;
}
//获取运算符到ops中
ops=ch+"";
//将计算结果放在op1中
op1=num1+"";
//将结果显示在文本框
jtf.setText(op1);
}else{//独立的单个运算,只需要记录运算符和第一个操作数
ops=ch+"";
num1=Double.parseDouble(op1);
//保存完第一个操作数后,清空op1内容,准备存储第二个操作数。
op1="";
}
}
}
});
}
}
public void init() {
jtf=new JTextField();
jtf.setFont(new Font("宋体", 0, 30));
jtf.setHorizontalAlignment(jtf.RIGHT);
jtf.setText("");
jtf.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,Color.blue, Color.blue));
add(jtf,BorderLayout.NORTH);
jp1=new JPanel();
GridLayout gridLayout = new GridLayout(4,4);
gridLayout.setHgap(8);
gridLayout.setVgap(8);
jp1.setLayout(gridLayout);
jbs=new JButton[16];
for(int i=0;i<16;i++){
jbs[i]=new JButton();
jbs[i].setText(cons[i]);
jbs[i].setFont(new Font("宋体",0,30));
jp1.add(jbs[i]);
}
add(jp1);
}
public static void main(String[] args) {
new CalcJFrame();
}}