- 实验目的
通过图形化界面设计相关类、接口等,实现用户图形化应用程序的开发;进一步巩固JDBC连接数据库以及文件读写操作。
- 实验目标
- 能够掌握常用GUI控制组件的使用方法,通过java.awt包以及Javax.swing包中的类和接口实现用户图形界面的开发;
- 能够运用Java的事件处理机制,通过JDBC操作数据库,实现用户登录功能;
- 能够掌握利用I/O流对文件进行操作。
- 实验内容
1 实验环境
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 具体实验内容
- 利用GUI设计实现一个计算器程序(注:至少要实现基本的加减乘除运算四则运算)。
- 设计一个关于文件操作的图形化应用程序,至少实现以下功能:
a)包含一个文本框以及添加按钮,在文本框中输入文字后,点击添加按钮可以在文件中写入文本框中的文字;
b)包含一个读取按钮,点击该按钮后,可以读取文件内容,并显示到文本框中。
- 实验过程分析
1 实验步骤
1.计算器程序,创建一个窗口页面,同时再这个窗口上添加几个需求的方法,要实现加减乘除这几个方法。
代码如下:
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();//上组件
private final Button clearButton = new Button("clear");
private JTextField show = new JFormattedTextField();
private final JPanel downBottom = new JPanel();//下组建
private final String string1 = "0123456789.";
private final String string2 = "[\\+\\-*/]{1}";
public void inti() {
this.setBounds(500, 200, 300, 300);
this.setTitle("计算机器");
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.upBottom();
this.downBottom();
}
public void upBottom() {
this.upBottom.setLayout(new FlowLayout());//流式布局
this.show.setPreferredSize(new Dimension(220, 30));//组件内设置大小方式
this.upBottom.add(show);
this.upBottom.add(clearButton);
this.upBottom.setForeground(Color.RED);
this.add(upBottom, BorderLayout.NORTH);
clearButton.addActionListener(this);//全局监听
}
public void downBottom() {
String s = new String("123+456-789/0.*=");
this.downBottom.setLayout(new GridLayout(4, 4));//网格布局
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, "被除数不能为0");
}
break;
default:
break;
}
assert result != null;
this.show.setText(result.toString());
} else if ("clear".equals(command)) {
this.show.setText("");
firstNum = null;
secondNum = null;
}
}
}
计算12*5=60
- 图形化应用程序,创建一个窗口界面,在这个窗口上添加两个组件,一个窗口用于向系统的输入,一个窗口用向系统的输出。
代码如下:
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("输入与输出");
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("宋体", 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, "读取成功");
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, "存档成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
初始化:
再输出:
4.2 错误分析
1.在该实验中,一定要考虑计算时的数据不能超范围,否则就会出现数据丢失问题等,另外还要考虑被除数不能为0。
2.文件的输入和输出时,若不考虑文件流的关闭可能会导致文件数据乱码,还得设置一下Java结构,将字符类型设置为UTF8。
- 实验总结
答:Java的图形用户界面的基本组成部分是组件,组件是一个以图形化的方式显示在屏幕上并能与用户进行交互的对象组件不能独立地显示出来,必须将组件放在一定的容器(container)中才可以显示出来。容器可以容纳多个组件,通过调用容器的add(Component comp)方法向容器中添加组件。图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠标等输入设备操纵屏幕上的图标或菜单选项,以选择命令、调用文件、启动程序或执行其它一些日常任务。与通过键盘输入文本或字符命令来完成例行任务的字符界面相比,图形用户界面有许多优点。图形用户界面由窗口、下拉菜单、对话框及其相应的控制机制构成,在各种新式应用程序中都是标准化的,即相同的操作总是以同样的方式来完成,在图形用户界面,用户看到和操作的都是图形对象,应用的是计算机图形学的技术。