Java作业四 图形用户界面程序设计和多线程
掌握窗体、容器组件、基本组件、菜单的创建和设置;熟悉图形绘制方法和程序结构。
掌握多线程控制程序编程方法。
一、简单的计算器
开发一个加、减、乘、除四则运算器。用户界面如下图:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/** 继承 JFrame就可以使你定义的这个类成为一个窗体
* 可以对窗体的属性进行扩展并且可以定义自己需要的特殊操作方法
* */
public class JCalculator extends JFrame
{
JPanel fPanel;
JPanel sPanel;
JTextField tfFirstOp;
JTextField tfSecondOp;
JTextField tfResult;
JButton btnAdd;
JButton btnMinus;
JButton btnMultiply;
JButton btnDivide;
String str;
Double op1;
Double op2;
Double rst;
public JCalculator()
{
//创建父面板,表格布局,四行一列
fPanel = new JPanel();
fPanel.setLayout(new GridLayout(4,1));
//加入第一操作数
tfFirstOp = new JTextField();
fPanel.add(tfFirstOp);
//加入第二操作数
tfSecondOp = new JTextField();
fPanel.add(tfSecondOp);
//创建子面板容纳四个按钮
sPanel = new JPanel();
sPanel.setLayout(new GridLayout(1,1));
//将四个按钮放到子面板
btnAdd = new JButton("+");
btnMinus = new JButton("-");
btnMultiply = new JButton("×");
btnDivide = new JButton("÷");
sPanel.add(btnAdd);
sPanel.add(btnMinus);
sPanel.add(btnMultiply);
sPanel.add(btnDivide);
btnAdd.setForeground(Color.red);
btnMinus.setForeground(Color.red);
btnMultiply.setForeground(Color.red);
btnDivide.setForeground(Color.red);
//将子面板放到父面板
fPanel.add(sPanel);
//将结果栏放到父面板
tfResult = new JTextField();
fPanel.add(tfResult);
//将父面板放进窗体
add(fPanel);
//为四个按钮添加监听事件
myListener listener = new myListener();
btnAdd.addActionListener(listener);
btnMinus.addActionListener(listener);
btnMultiply.addActionListener(listener);
btnDivide.addActionListener(listener);
}
private class myListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
//处理每个按钮对应的事件
if(e.getSource()==btnAdd)
{
str = tfFirstOp.getText();
op1 = Double.parseDouble(str.trim());
str = tfSecondOp.getText();
op2 = Double.parseDouble(str.trim());
rst = op1 + op2;
tfResult.setText(rst+"");
}
if(e.getSource()==btnMinus)
{
str = tfFirstOp.getText();
op1 = Double.parseDouble(str.trim());
str = tfSecondOp.getText();
op2 = Double.parseDouble(str.trim());
rst = op1 - op2;
tfResult.setText(rst+"");
}
if(e.getSource()==btnMultiply)
{
str = tfFirstOp.getText();
op1 = Double.parseDouble(str.trim());
str = tfSecondOp.getText();
op2 = Double.parseDouble(str.trim());
rst = op1 * op2;
tfResult.setText(rst+"");
}
if(e.getSource()==btnDivide)
{
str = tfFirstOp.getText();
op1 = Double.parseDouble(str.trim());
str = tfSecondOp.getText();
op2 = Double.parseDouble(str.trim());
rst = op1 / op2;
tfResult.setText(rst+"");
}
}
}
public static void main(String args[])
{
//主方法入口
JCalculator frm = new JCalculator();
frm.setTitle("A Simple Calculator");
frm.setSize(400,300);
frm.setLocation(300,300);
frm.setVisible(true);
}
}
运行截图
二、实现笑脸哭脸的切换
编写一个“变脸”程序:程序运行后出现一张笑脸,鼠标点击一次则变成哭脸,再点击一次又变成笑脸,依次轮换。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SmileToCry extends JFrame
{
public static int i = 0;
public static boolean bool = true;
@Override
public void paint(Graphics g)
{
//这一步操作会调用父类update();update()方法中paint()方法为空操作
super.paint(g);
g.setColor(Color.red);
if(bool)
{
g.drawOval(50,50,80,100);
g.drawArc(70,80,14,7,0,180);
g.drawArc(110,80,14,7,0,180);
g.drawArc(70,110,40,20,180,180);
g.drawString("I'm Smiling!",30,55);
}
else
{
g.drawOval(50,50,80,100);
g.drawArc(70,80,14,7,180,180);
g.drawArc(110,80,14,7,180,180);
g.drawArc(70,110,40,20,0,180);
g.drawString("I'm Crying!",30,55);
}
}
public static void main(String[] args)
{
SmileToCry stc = new SmileToCry();
stc.setTitle("Click to change face");
stc.setBounds(200,200,200,200);
stc.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
super.mouseClicked(e);
i++;
if (i%2 == 1)
{
bool = false;
}
else
{
bool = true;
}
stc.repaint();
}
});
stc.setVisible(true);
}
}
运行截图
三、多线程绘图
利用鼠标事件启动多个线程在多个窗体中同时绘制动画。
思路:程序运行后每点击一次按钮出现一个窗体。
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
//继承JFrame,并且实现Runnable接口
public class ThreadCartoon extends JFrame implements Runnable
{
int X;
int Y;
public static boolean bool = true;
//构造方法,实现JFram的初始化,传参设置三个窗体出现的位置
public ThreadCartoon(int x,int y)
{
X = x;
Y = y;
this.setBounds(X,Y,300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("动画演示");
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
if (bool)
{
g.drawRect(40,50,150,100);
}
else
{
g.drawOval(40,50,150,100);
}
bool = !bool;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e){}
repaint();
}
@Override
public void run()
{
//线程要做的事,创建窗体,并且调用paint方法绘制动画
ThreadCartoon tc = new ThreadCartoon(X,Y);
tc.setVisible(true);
}
public static void main(String[] args)
{
//x,y是每个子窗体的坐标
final int[] x = new int[20];
Arrays.fill(x,300);
final int y = 300;
JFrame f1 = new JFrame("初始界面");
f1.setBounds(1000,100,200,200);
JButton btn = new JButton("start");
btn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Thread t = new Thread(new ThreadCartoon(x[0], y));
x[0] +=300;
t.start();
}
});
f1.add(btn);
f1.setVisible(true);
}
}
运行截图