java实验五正式报告
Posted _DiMinisH
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实验五正式报告相关的知识,希望对你有一定的参考价值。
实验五 图形化应用程序开发(实验报告)
一、实验目的
1.通过图形化界面设计相关类、接口等,实现用户图形化应用程序的开发
2.进一步巩固JDBC连接数据库以及文件读写操作
二、实验目标
1.能够掌握常用GUI控制组件的使用方法,通过java.awt包以及Javax.swing包中的类和接口实现用户图形界面的开发;
2.能够运用Java的事件处理机制,通过JDBC操作数据库,实现用户登录功能;
3.能够掌握利用I/O流对文件进行操作。
三、实验内容
3.1 实验环境
IntelliJ IDEA Ultimate Edition 2021.3 x64. + openjdk-17.0.1.
3.2 具体实验内容
问题一
- 利用GUI模拟用户登录,界面设计下图:
① 所有的用户名密码存储在数据库中;
② 定义一个类使用JDBC连接数据库,读取用户名密码数据进行匹配以实现用户登录,若登录成功,提示用户登录成功,否则,提示用户登录失败;
问题分析:GUI设计需要使用JFrame类创建窗体,按钮需要JButton类,文本域需要JTextField类,其他控件都有对应的类,数据库操作需要对应的JAR包,本实验我使用的是Spring Data操控数据库
项目结构
package com.example.interaction8001.Controller;
import com.example.interaction8001.Data.ExperimentUser;
import com.example.interaction8001.Data.ExperimentUserService;
import com.example.interaction8001.HttpOperation.HttpOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@org.springframework.stereotype.Controller
@ResponseBody
public class Controller
@Autowired
private ExperimentUserService service;
//用于接收按钮的请求
@RequestMapping(value = "/button/findByUserName/username", method = RequestMethod.GET)
Object findByUserName (@PathVariable String username)
ExperimentUser user = service.findExperimentUserByUserName(username);
if (user == null)
return "null";
else
return service.findExperimentUserByUserName(username).getPassword();
public static void main(String[] args)
JFrame frame = new MyFrame("数据库操控");
class MyFrame extends JFrame
private final JFrame thisFrame = this;
private final JPanel rootPanel = new JPanel();
private final JLabel titleLabel = new JLabel("数 据 库 操 控");
private final JLabel urlLabel = new JLabel("账户");
private final JLabel passwordLabel = new JLabel("密码");
private final JTextField urlTextField = new JTextField();
private final JPasswordField passwordPasswordField = new JPasswordField();
private final JCheckBox rememberPasswordCheckBox = new JCheckBox("记住账号密码");
private final JButton loginButton = new JButton("登录");
MyFrame(String name)
super(name);
this.setBounds(new Rectangle(600, 300, 400,300));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
this.dataInitialize();
this.componentInitial();
this.setParameter();
this.dealLogic();
private void dataInitialize()
private void componentInitial()
this.setComponent();
this.setComponentsLayout();
this.setComponentsPreferredSize();
this.setComponentsHorizontalAlignment();
private void setComponent()
rootPanel.add(titleLabel);
rootPanel.add(urlLabel);
rootPanel.add(passwordLabel);
rootPanel.add(urlTextField);
rootPanel.add(passwordPasswordField);
rootPanel.add(rememberPasswordCheckBox);
rootPanel.add(loginButton);
private void setComponentsLayout()
this.setContentPane(rootPanel);
rootPanel.setLayout(null);
titleLabel.setBounds(new Rectangle(0,0,400,120));
urlLabel.setBounds(new Rectangle(0,140,100,25));
passwordLabel.setBounds(new Rectangle(0,200,100,25));
urlTextField.setBounds(new Rectangle(100,140,250,25));
passwordPasswordField.setBounds(new Rectangle(100,200,250,25));
rememberPasswordCheckBox.setBounds(new Rectangle(0,230,100,25));
loginButton.setBounds(new Rectangle(300,230,70,25));
private void setComponentsPreferredSize()
titleLabel.setPreferredSize(new Dimension(100, 100));
urlLabel.setPreferredSize(new Dimension(80, 50));
passwordLabel.setPreferredSize(new Dimension(80, 50));
urlTextField.setPreferredSize(new Dimension(200,40));
passwordPasswordField.setPreferredSize(new Dimension(200,40));
rememberPasswordCheckBox.setPreferredSize(new Dimension(80,25));
loginButton.setPreferredSize(new Dimension(60,25));
private void setComponentsHorizontalAlignment ()
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
urlLabel.setHorizontalAlignment(SwingConstants.CENTER);
passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
urlTextField.setHorizontalAlignment(SwingConstants.LEFT);
passwordPasswordField.setHorizontalAlignment(SwingConstants.LEFT);
rememberPasswordCheckBox.setHorizontalAlignment(SwingConstants.CENTER);
loginButton.setHorizontalAlignment(SwingConstants.CENTER);
private void setParameter()
titleLabel.setFont(new Font("华文彩云", Font.BOLD, 40));
urlLabel.setFont(new Font("华文彩云", Font.BOLD, 20));
passwordLabel.setFont(new Font("华文彩云", Font.BOLD, 20));
private void dealLogic()
rememberPasswordCheckBox.setSelected(false);
loginButton.addActionListener(new LoginButtonActionListener());
class LoginButtonActionListener implements ActionListener
@Override
public void actionPerformed(ActionEvent e)
String url = urlTextField.getText();
String password = passwordPasswordField.getText();
JDialog jDialog = new JDialog(thisFrame, "登录信息");
jDialog.setBounds(new Rectangle(650, 350, 150,100));
jDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
jDialog.setModal(true);
JLabel message = new JLabel();
message.setFont(new Font("微软雅黑", Font.BOLD, 20));
message.setForeground(Color.red);
jDialog.getContentPane().add(message);
String passWord = HttpOperation.sendGet("http://localhost:8001/button/findByUserName/" + url, "");
if (passWord != null )
if (password.equals(passWord))
message.setText("登陆成功");
else
message.setText("账号错误");
else
message.setText("账号错误");
jDialog.setVisible(true);
- 实验结果
问题二
- 设计一个关于文件操作的图形化应用程序,至少实现以下功能:
① 包含一个文本框以及添加按钮,在文本框中输入文字后,点击添加按钮可以在文件中写入文本框中的文字;
② 包含一个读取按钮,点击该按钮后,可以读取文件内容,并显示到文本框中。
问题分析:GUI设计需要使用JFrame类创建窗体,按钮需要JButton类,文本域需要JTextField类,其他控件都有对应的类,文件操作类使用InputStream类和OutputStream类
class MyFrame extends JFrame
private final JFrame thisFrame = this;
private final JPanel rootPanel = new JPanel();
private final JLabel titleLabel = new JLabel("文 本");
private final JButton saveButton = new JButton("保存");
private final JButton openButton = new JButton("打开");
private final JTextArea inputTextArea = new JTextArea();
public static void main(String[] args) throws InterruptedException
Thread thread = new Thread(new Runnable()
@Override
public void run()
JFrame frame = new MyFrame("");
);
thread.start();
thread.join();
MyFrame(String name)
super(name);
this.setBounds(new Rectangle(600, 300, 400,300));
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.dataInitialize();
this.componentInitial();
this.setParameter();
this.dealLogic();
private void dataInitialize()
private void componentInitial()
this.setComponent();
this.setComponentsLayout();
this.setComponentsPreferredSize();
this.setComponentsHorizontalAlignment();
private void setComponent()
rootPanel.add(titleLabel);
rootPanel.add(saveButton);
rootPanel.add(openButton);
rootPanel.add(inputTextArea);
private void setComponentsLayout()
this.setContentPane(rootPanel);
rootPanel.setLayout(null);
titleLabel.setBounds(new Rectangle(30,0,30,30));
saveButton.setBounds(new Rectangle(240,0,60,30));
openButton.setBounds(new Rectangle(320,0,60,30));
inputTextArea.setBounds(new Rectangle(5,40,375,200));
private void setComponentsPreferredSize()
titleLabel.setPreferredSize(new Dimension(100, 100));
private void setComponentsHorizontalAlignment ()
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
saveButton.setHorizontalAlignment(SwingConstants.CENTER);
openButton.setHorizontalAlignment(SwingConstants.CENTER);
private void setParameter()
private void dealLogic()
saveButton.addActionListener(new saveButtonActionListener());
openButton.addActionListener(new openButtonActionListener());
class saveButtonActionListener implements ActionListener
@Override
public void actionPerformed(ActionEvent e)
String contents = inputTextArea.getText();
File file = new File("../data.txt");
JDialog dialog = new JDialog(thisFrame);
dialog.setBounds(new Rectangle(650, 350, 200,10));
if(!file.exists())
try
if (!file.createNewFile())
dialog.setModal(true);
dialog.setTitle实验五 网络编程与安全-----实验报告
2018-2019-2 20175306实验五《网络编程与安全》实验报告
2017-2018-2 20165237 实验五《网络编程与安全》实验报告