当前位置:网站首页>Experiment 5: Gui
Experiment 5: Gui
2022-07-19 06:36:00 【A student of programming】
- The experiment purpose
Design related classes through graphical interfaces 、 Interfaces, etc. , Realize the development of user graphical application ; Further consolidate JDBC Connect to the database and read and write files .
- The goal of the experiment
- Be able to master common GUI How to use the control components , adopt java.awt Packages and Javax.swing The classes and interfaces in the package realize the development of user graphical interface ;
- Be able to use Java Event handling mechanism of , adopt JDBC Operating the database , Realize user login function ;
- Be able to master and use I/O Stream operates on files .
- Experimental content
1 Experimental environment
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
2 The specific content of the experiment
- utilize GUI Design and implement a calculator program ( notes : At least four basic operations of addition, subtraction, multiplication and division should be realized ).
- Design a graphic application about file operation , At least realize the following functions :
a) Contains a text box and add button , After entering text in the text box , Click the Add button to write the text in the text box in the file ;
b) Contains a read button , After clicking this button , Can read file contents , And display it in the text box .
- Analysis of experimental process
1 The experimental steps
1. Calculator program , Create a window page , At the same time, add several methods of requirements to this window , To realize the methods of addition, subtraction, multiplication and division .
The code is as follows :
package demo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJfram extends JFrame implements ActionListener {
private final JPanel upBottom = new JPanel();// Upper component
private final Button clearButton = new Button("clear");
private JTextField show = new JFormattedTextField();
private final JPanel downBottom = new JPanel();// Next set up
private final String string1 = "0123456789.";
private final String string2 = "[\\+\\-*/]{1}";
public void inti() {
this.setBounds(500, 200, 300, 300);
this.setTitle(" Computing machine ");
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.upBottom();
this.downBottom();
}
public void upBottom() {
this.upBottom.setLayout(new FlowLayout());// Fluid layout
this.show.setPreferredSize(new Dimension(220, 30));// Set the size method in the component
this.upBottom.add(show);
this.upBottom.add(clearButton);
this.upBottom.setForeground(Color.RED);
this.add(upBottom, BorderLayout.NORTH);
clearButton.addActionListener(this);// Global monitoring
}
public void downBottom() {
String s = new String("123+456-789/0.*=");
this.downBottom.setLayout(new GridLayout(4, 4));// Grid layout
for (int i = 0; i < 16; i++) {
Button button = new Button();
button.setLabel(String.valueOf(s.charAt(i)));
button.addActionListener(this);
downBottom.add(button);
}
this.add(downBottom, BorderLayout.CENTER);
}
public static void main(String[] args) {
MyJfram myJfram = new MyJfram();
myJfram.inti();
}
String firstNum = null;//
String operate = null;
String secondNum = null;
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (string1.contains(command)) {
this.show.setText(this.show.getText() + command);
this.show.setHorizontalAlignment(JTextField.RIGHT);
} else if (command.matches(string2)) {
operate = command;
firstNum = this.show.getText();
this.show.setText("");
} else if ("=".equals(command)) {
secondNum = this.show.getText();
Double a = Double.valueOf(firstNum);
Double b = Double.valueOf(secondNum);
Double result = null;
switch (operate) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
if (b != 0) {
result = a / b;
} else {
JOptionPane.showMessageDialog(this, " The dividend cannot be zero 0");
}
break;
default:
break;
}
assert result != null;
this.show.setText(result.toString());
} else if ("clear".equals(command)) {
this.show.setText("");
firstNum = null;
secondNum = null;
}
}
}
Calculation 12*5=60
- Graphical applications , Create a window interface , Add two components to this window , A window for input to the system , A window is used to output to the system .
The code is as follows :
package demo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class MyJfram extends JFrame implements ActionListener {
public TextArea textArea = new TextArea();
public Button button = new Button();
public Button button1 = new Button();
public StringBuffer inputString = new StringBuffer();
public void inti() {
this.setBounds(500, 200, 300, 300);
this.setVisible(true);
this.setResizable(false);
this.setTitle(" Input and output ");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
up();
down();
}
private void down() {
button.setLabel("InputText");
button.setPreferredSize(new Dimension(300, 60));
button.addActionListener(this);
this.add(button, BorderLayout.SOUTH);
button1.setLabel("OutText");
button1.setPreferredSize(new Dimension(300, 60));
button1.addActionListener(this);
this.add(button1, BorderLayout.SOUTH);
}
public void up() {
textArea.setPreferredSize(new Dimension(270, 100));
textArea.setFont(new Font(" Song style ", Font.BOLD, 20));
this.add(textArea);
}
public static void main(String[] args) {
MyJfram myJfram = new MyJfram();
myJfram.inti();
}
String string = "InputText";
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (string.equals(command)) {
String text = textArea.getText();
writerText(text);
} else {
readText();
}
}
private void readText() {
File file = new File("D:\\VM\\a.txt");
try (FileReader fileReader = new FileReader(file)) {
BufferedReader bufferedReader = new BufferedReader(fileReader);
String len = null;
StringBuilder result = new StringBuilder();
while ((len = bufferedReader.readLine()) != null) {
result.append(len);
}
JOptionPane.showMessageDialog(this, " Read successful ");
textArea.setText(result.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
private void writerText(String text) {
File file = new File("D:\\VM\\a.txt");
try (FileWriter fileWriter = new FileWriter(file)) {
inputString.append(text);
fileWriter.write(inputString.toString());
textArea.setText("");
JOptionPane.showMessageDialog(this, " Archive success ");
} catch (IOException e) {
e.printStackTrace();
}
}
}
initialization :
Then the output :
4.2 error analysis
1. In this experiment , It must be considered that the data during calculation cannot exceed the range , Otherwise, data loss will occur , Also consider that the divisor cannot be 0.
2. When inputting and outputting files , If you do not consider the closure of the file stream, the file data may be garbled , You have to set it up Java structure , Set the character type to UTF8.
- Summary of the experiment
answer :Java The basic component of GUI is component , A component is an object that is displayed graphically on the screen and can interact with users. Components cannot be displayed independently , The components must be placed in a container (container) Can be displayed in . Containers can hold multiple components , By calling the add(Component comp) Method to add a component to the container . Graphical user interface (GUI) is an interface display format for communication between people and computers , Allows users to manipulate icons or menu options on the screen using input devices such as a mouse , To select the command 、 Call file 、 Start a program or perform other routine tasks . Compared to a character interface that uses keyboard input text or character commands to perform routine tasks , GUI has many advantages . The GUI consists of windows 、 The drop-down menu 、 Dialog box and its corresponding control mechanism constitute , It's standardized in all kinds of new applications , That is, the same operation is always done in the same way , In the graphical user interface , What users see and operate are graphic objects , The application is the technology of computer graphics .
边栏推荐
- Busybox specified date modification temporarily does not require clock -w to write to hardware
- Interview review nth time
- [force buckle] realize queue with stack
- Busybox date date increases by one day, and decreases by one day on the Internet tomorrow
- Longest bracket match (linear DP)
- 量子三体问题: 数值计算概述
- 【力扣】环形链表 II
- [Niuke] traversal of binary tree
- What kind of deep learning is most suitable for your enterprise?
- Antd is not defined
猜你喜欢
Learning non posture gaze deviation with head movement
斑点检测 记录
Interview review nth time
[force buckle] design cycle queue
QT creator flashback solution
[force buckle] ring list II
[Li Kou] a subtree of another tree
Creation and implementation of WebService interface
Depth first search (DFS for short)
DSL realizes automatic completion query
随机推荐
Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位
Robot stitching gesture recognition and classification
Pytorch deep learning practice-b station Liu erden-day7
实验一 简单程序设计
通过VOR深度估计解决三维注视交互中的目标模糊问题
Leetcode tree
Eye tracking in virtual reality
单表查询、添加、更新与删除数据
实习笔试解答
CUDA与大数组的双调排序
基于视觉显著性的外观注视估计
WebService接口的创建与实现
Operation of documents in index library
[bjoi2019] platoon formation (Group backpack)
一种基于凝视的平板电脑手势控制系统的设计与实现
Quelques concepts de base dans le réseau
Positional change of the eyeball during eye movements: evidence of translational movement
Preorder traversal of binary tree
Markdown语法和常用快捷键
EOG based eye movement detection and gaze estimation for an asynchronous virtual keyboard