JAVA实验五: 图形用户界面
实验目的
- 掌握图形用户界面的要素。
- 掌握窗体类。
- 掌握布局管理器。
- 掌握中间容器类
- 掌握事件处理机制。
- 掌握图形界面中的基本组件。
实验内容
第一部分
编写一个Java源程序,其中包含:
编写一个包含主方法main的公共类(访问权限为public的类),该类继承自窗体类JFrame,并且,该类实现了接口ActionListener(实现接口ActionListener的方法actionPerformed)。
源程序实现的任务是实现如下图所示的图形用户界面(当窗口大小改变时,其中的组件的大小也会随之改变),并且完成以下要求:(1)可以在“请输入文件路径和名称:”后面的文本框中输入文件名和路径。
(2)可以在文本区中写文本。
(3)可以在“请输入需要追加的内容:”后面的文本域中输入内容,按回车键之后,其中的内容会添加到文本区中(添加的内容另起一行)。
(4)单击“将文本区的内容写入文件”按钮,文本区中的内容就会被写到指定的文件中。第二部分
编写一个Java源程序,其中包含:
编写一个包含主方法main的公共类(访问权限为public的类),该类继承自窗体类JFrame,并且,该类实现了接口ActionListener(实现接口ActionListener的方法actionPerformed)。
源程序实现的任务是实现如下图所示的图形用户界面(当窗口大小改变时,第1行和第2行的组件的大小也会随之改变,其他组件的大小不发生改变),并且完成以下要求:(1)窗体的标题是“猜数字小游戏”。
(2)单击“得到一个随机数”按钮,系统会产生一个1~10之间的随机整数(使用Math类的方法random( ))。
(3)在“输入您的猜测:”后面的文本框中输入你要猜测的整数,之后单击“确定”按钮。
(4)对你猜测的整数,图形用户界面的最下面会有提示信息,没有猜的时候,显示“无反馈信息”,如果猜大了,显示“猜大了”,如果猜小了,显示“猜小了”,如果猜对了,显示“猜对了”。(其中,蓝色字体是JLabel类的对象调用方法setForeground(Color.blue)实现的。)
实验代码
第一部分
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
File file;
FileWriter fileWriter;
JLabel path=new JLabel("请输入文件路径和名称:");
JTextField filePath = new JTextField();
JLabel append = new JLabel("请输入需要追加的内容:");
JTextField appendContent = new JTextField();
JButton button=new JButton("将文本区的内容写入文件");
JTextArea area = new JTextArea();
Main()
{
JPanel north = new JPanel();
north.setLayout(new GridLayout(3,2,0,5));
north.add(path);
north.add(filePath);
north.add(append);
north.add(appendContent);
appendContent.addActionListener(this);
north.add(button);
button.addActionListener(this);
add(north, BorderLayout.NORTH);
add(new JScrollPane(area), BorderLayout.CENTER);
setLocation(100, 100);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if (obj == button)
{
try
{
file = new File(filePath.getText());
fileWriter = new FileWriter(file);
fileWriter.write(area.getText());
fileWriter.flush();
fileWriter.close();
}
catch(IOException ee)
{
appendContent.setText("文件出现异常");
}
}
else
{
String block = appendContent.getText();
String temp=area.getText();
if(temp.equals(""))
area.append(block);
else
area.append("\n"+block);
}
}
public static void main(String[] args)
{
new Main();
}
}
第二部分
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
JLabel label1 = new JLabel("获取0-10之间的随机数");
JButton number = new JButton("得到一个随机数");
JLabel label2 = new JLabel("输入你的猜测");
JTextField guess = new JTextField();
JLabel label3 = new JLabel("单击确定按钮");
JButton ok = new JButton("确定");
JLabel info = new JLabel();
Main()
{
setTitle("猜数字小游戏");
setLayout(new GridLayout(3,1));
JPanel fixer = new JPanel();
fixer.setLayout(new GridLayout(2, 2));
fixer.add(label1);
fixer.add(number);
number.addActionListener(this);
fixer.add(label2);
fixer.add(guess);
add(fixer, BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(label3);
p2.add(ok);
ok.addActionListener(this);
add(p2, BorderLayout.CENTER);
JPanel p3 = new JPanel();
p3.add(info);
info.setForeground(Color.BLUE);
info.setText("无反馈信息");
add(p3, BorderLayout.NORTH);
setLocation(100, 100);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
int gussNumber;
int myguess;
public void actionPerformed(ActionEvent e)
{
Object obj = e.getSource();
if(obj == number)
{
gussNumber =(int)(Math.random()*9)+1;
}
obj = e.getSource();
if(obj == ok)
{
myguess = Integer.parseInt(guess.getText());
if(gussNumber == myguess)
{
info.setForeground(Color.RED);
info.setText("恭喜你,猜对了!");
}
else if(gussNumber < myguess)
{
info.setText("猜大了");
}
else if(gussNumber > myguess)
{
info.setText("猜小了");
}
}
}
public static void main(String [] args)
{
new Main();
}
}
Comments | NOTHING