当前位置:网站首页>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 .
边栏推荐
- Robot stitching gesture recognition and classification
- Résoudre le problème de l'ambiguïté de la cible dans l'interaction de fixation 3D par l'estimation de la profondeur vor
- Eye tracking in virtual reality
- Acwing daily question three thousand five hundred and eleven
- 从输入URL到展示出页面
- Restclient multi conditional aggregation
- 实习笔试解答
- Depth first search (DFS for short)
- Quelques concepts de base dans le réseau
- Automatic completion & (custom) Pinyin word Separator &
猜你喜欢

SalGaze:使用视觉显著性的个性化注视估计

读取图片 进行空间转换 展现不同颜色空间

Solution: unable to load file c:\program files\ Because running scripts is forbidden on this system

What kind of deep learning is most suitable for your enterprise?

Color histogram grayscale image & color image

Depth first search (DFS for short)

颜色直方图 灰度图&彩色图

Cygwin 配合 Listary 切换当前目录快速打开

Operation of documents in index library

实验五: GUI
随机推荐
Learning video saliency from human gaze using candidate selection
ACWing每日一题.3511
浅谈跨域的几种解决方案
大龄程序员都去哪了?
Cable TV network (tree grouping)
Positional Change of the Eyeball During Eye Movements: Evidence of Translatory Movement眼球运动过程中眼球的位
Automatic completion & (custom) Pinyin word Separator &
Restapi implements aggregation (dark horse tutorial)
Read pictures and convert them to show different color spaces
Make config analysis of configuration commands before uboot compilation
Acwing daily question three thousand five hundred and eleven
C language specifies how many days to display from the date
Volatile function of embedded C language
从零开始的 Rust 语言 blas 库之预备篇(2)—— blas 矩阵格式详解
C language calls the file browser to realize the effect of selecting files
What kind of deep learning is most suitable for your enterprise?
Solutions to slow transmission speed of FileZilla virtual machine
【力扣】用队列实现栈
数据库的查询(二)
[usaco06dec]the fewest coins g (hybrid backpack)