JAVA:Java Swing 练习题,
分享于 点击 31022 次 点评:190
JAVA:Java Swing 练习题,
图形界面的实现
使用Java Swing设计与编写一个账号注册的图形界面程序,要求:
难点:
MVC结构
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
- Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
- View(视图) - 视图代表模型包含的数据的可视化。
- Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。
思路:
我个人的理解是MVC结构由三个类构成,一个主类和两个其他的类。主类则是Model(模型类),作用是初始化一个界面。View(视图)类主要是画出界面和监听器。Controller(控制器)类则是处理数据。
Login类
// public class Login {
public static void main(String[] args) {
// TODO Auto-generated method stub
Loginview log = new Loginview();
log.setTitle("注册界面");
}
}
Logincontroller类
public class Logincontroller {
private String yonghuming;
private String shoujihao;
private String mima;
void SetName(String yonghuming)
{
this.yonghuming = yonghuming;
}
void SetCode(String mima)
{
this.mima = mima;
}
void SetNumber(String shoujihao)
{
this.shoujihao = shoujihao;
}
String getName()
{
return yonghuming;
}
String getCode()
{
return mima;
}
String getNumber()
{
return shoujihao;
}
boolean JudgeName()
{
if (yonghuming.length() >= 14) //判断用户名长度是否超过14字节,若没有超过则返回true给Loginview的监听方法。
{
return false;
}
else
{
return true;
}
}
}
Loginview类
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Loginview extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
Logincontroller user = new Logincontroller();
//此处声明的为注册界面的变量,为全局变量
Box baseBox, boxV1, boxV3, boxV4; //声明变量
JTextField yonghuming = new JTextField(15); //用户名文本框
JLabel label3; //出错注释
JTextField shoujihao = new JTextField(15); //手机号文本框
JTextField mima = new JTextField(15); //密码文本框
JButton zhuce = new JButton("注册"); //注册按钮
ComputerListener computer = new ComputerListener(); //监视ActionEvent事件的监视器
public Loginview() {
setLayout(new java.awt.FlowLayout());
setBounds(75,750,450,450);
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口后的动作:结束窗口所在的应用程序
}
class ComputerListener implements ActionListener //监听方法
{
JLabel label1;
JLabel label2;
JLabel label4;
Box baseBox, boxV1, boxV2;
JFrame f1 = new JFrame();
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == zhuce)
{
user.SetName(yonghuming.getText());
if (user.JudgeName() == true) //与Logincontroller类产生交互
{
user.SetCode(mima.getText());
user.SetNumber(shoujihao.getText());
label1 = new JLabel(user.getName());
label2 = new JLabel(user.getCode());
label4 = new JLabel(user.getNumber());
//构建注册成功后显示的界面
boxV1 = Box.createHorizontalBox();
boxV1.add(new JLabel("用户名"));
boxV1.add(Box.createHorizontalStrut(10));
boxV1.add(label1);
boxV2 = Box.createHorizontalBox();
boxV2.add(new JLabel("密 码"));
boxV2.add(label2);
baseBox = Box.createVerticalBox();
baseBox.add(boxV1);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(boxV2);
add(baseBox);
FlowLayout f2 = new FlowLayout();
f2.setAlignment(FlowLayout.CENTER);
f1.setTitle("注册成功");
f1.setLayout(f2);
f1.setBounds(200,200,300,300);
f1.add(baseBox);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
else
{
label3.setText("用户名不能超过14字节,请重新输入。");
label3.setForeground(Color.red);
}
}
}
}
void init() { //构建出注册界面,采用BoxLayout嵌套布局。
//行型盒式布局
boxV1 = Box.createHorizontalBox();
boxV1.add(new JLabel("用户名"));
boxV1.add(Box.createHorizontalStrut(10));
boxV1.add(yonghuming);
boxV1.add(label3 = new JLabel("最长14个英文或7个汉字"));
boxV1.add(label3);
boxV3 = Box.createHorizontalBox();
boxV3.add(new JLabel("密 码"));
boxV3.add(Box.createHorizontalStrut(10));
boxV3.add(mima);
boxV4 = Box.createHorizontalBox();
boxV4.add(zhuce);
zhuce.addActionListener(computer);
//列型盒式布局
baseBox = Box.createVerticalBox();
baseBox.add(boxV1);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(boxV3);
baseBox.add(Box.createVerticalStrut(10));
baseBox.add(boxV4);
add(baseBox);
}
}
运行:
心得:
注册的图形界面不应该用BoxLayout布局,做出来很奇怪很丑,应该用FlowLayout布局,效果应该会好点。
相关文章
- 暂无相关文章
用户点评