首 页 行业资讯 新车 试驾评测 养车用车 车型库

java图形界面(GUI)

发布网友 发布时间:2022-04-22 22:52

我来回答

3个回答

热心网友 时间:2023-10-07 06:59

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Window extends Frame{
Button button1 = new Button("开始");
Button button2 = new Button("结束");
TextField textfield = new TextField();
static Random random = new Random();
String string = null;
boolean bstart = false;
Thread thread;

public void launchFrame() {
this.setLocation(500, 200);
this.setLayout(new BorderLayout());
this.add(button1, new BorderLayout().WEST);
this.add(button2, new BorderLayout().EAST);
this.add(textfield, new BorderLayout().NORTH);
button1.addActionListener(new Button1Listener());
button2.addActionListener(new Button2Listener());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setVisible(true);

}

private class Button1Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
bstart = true;
thread = new Thread(new NumProce());
thread.start();
}
}

private class Button2Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
bstart = false;
}
}

private class NumProce implements Runnable {

private void numproce() {
string = Integer.toString(random.nextInt()); /*nextint的括号中处可以修改产生随机数的范围*/
textfield.setText(string);
}

public void run() {
while(bstart) {
try {
numproce();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
new Window().launchFrame();
}
}

热心网友 时间:2023-10-07 07:00

随机数有什么要求吗?范围?整数还是小数?追问随机数,没任何要求

追答搞了半天原来是个多线程 我去~代码如下,建个类叫Test,把代码复制进去运行看看

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test {
private JFrame jf;
private JTextField jtf;
private JButton jb1;
private JButton jb2;
private boolean state;
private Mythread th;

public Test(){
jf = new JFrame("随机数");
jtf = new JTextField(15);
jb1 = new JButton("开始");
jb2 = new JButton("结束");
state = true;

}

public void init(){
jf.setLayout(new FlowLayout());
jtf.setEditable(false);
jf.add(jtf);
jf.add(jb1);
jf.add(jb2);
}

public void eventHander(){
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent action) {
th = new Mythread(jtf, state);
th.start();
}
});

jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent action) {
th.state=false;
}
});
}

public void show(){
init();
eventHander();
jf.setVisible(true);
jf.setSize(350, 120);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

new Test().show();
}
}

class Mythread extends Thread{
public JTextField jtf;
public boolean state;
public Mythread(JTextField jtf,boolean state){
this.jtf=jtf;
this.state = state;
}

public void run() {
while(state){
jtf.setText(Math.random()*10000000+"");
try {
new Thread().sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}

}

热心网友 时间:2023-10-07 07:00

先占楼,编好代码上贴上

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com