跪求一个编程大神,有一个Java程序不会写,求源代码。 图片可能看不清楚 200
如图所示的一个Applet,点击start按钮,文本框开始计数."count="后面的值增加,计数的数值,每100毫秒更新。点击suspend按钮是暂停,点击Resume...
如图所示的一个Applet,点击start按钮,文本框开始计数."count="后面的值增加,计数的数值,每100毫秒更新。点击suspend按钮是暂停,点击Resume按钮是重新开始计数,"count="后面的值增加。
在线等,挺急的 展开
在线等,挺急的 展开
2个回答
展开全部
start 和supend 用了一个啊。懒得改了。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
public class Count {
public static void main(String[] args) {
View view = new View();
}
static class View implements ActionListener {
private JFrame frame;
private JButton suspend;
private JButton resume;
private JTextField count;
private int index;
private boolean stop = false;
public View() {
frame = new JFrame("haha");
frame.setBounds(300, 200, 500, 500);
frame.getContentPane().setLayout(null);
frame.setBackground(Color.blue);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
suspend = new JButton("suspend");
suspend.setBounds(38, 197, 93, 60);
suspend.addActionListener(this);
frame.getContentPane().add(suspend);
resume = new JButton("resume");
resume.setBounds(200, 197, 93, 60);
resume.addActionListener(this);
frame.getContentPane().add(resume);
count = new JTextField();
count.addActionListener(this);
count.setBounds(38, 100, 93, 40);
count.setBackground(Color.WHITE);
count.setEditable(false);
frame.getContentPane().add(count);
try {
init();
start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void init() throws InterruptedException {
index = 0;
count.setText("count is " + index);
}
private void start() throws InterruptedException {
while (true) {
if (!stop) {
count.setText("count is " + ++index);
}
TimeUnit.MILLISECONDS.sleep(100L);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == suspend) {
stop = stop ? false : true;
} else if (e.getSource() == resume) {
try {
init();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
追问
运行出来了,但和那个题不太一样,这是我的作业,所以……我自己写了一个,但还有点不太清楚,具体的我发你百度云了,能帮我看看吗?
展开全部
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
@SuppressWarnings("all")
public class AppJava extends JApplet implements Serializable, ActionListener {
private static final long serialVersionUID = 9L;
private JButton jb1, jb2, jb3;
private JTextField jtf;
private JPanel jp1, jp2;
private long count;
private Thread t1;
public AppJava() {
this.setLayout(new GridLayout(2, 1));
jtf = new JTextField(10);
jb1 = new JButton("Start");
jb2 = new JButton("Suspend");
jb3 = new JButton("Resume");
jp1 = new JPanel();
jp2 = new JPanel();
jp1.add(jtf);
jp1.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
this.add(jp1);
this.add(jp2);
Jinit();
}
public void Jinit() {
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jtf.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
e.consume();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
String name = e.getActionCommand();
if (name.equals("Start")) {
method1(count);
} else if (name.equals("Suspend")) {
if(t1==null) {
return;
}
if (t1.isAlive()) {
t1.interrupt();
}
} else {
count = 0;
jtf.setText("\"count\"=" + count);
}
}
private void method1(long c) {
if (t1 == null) {
t1 = new Thread(new run());
} else {
if (t1.isAlive())
return;
t1.start();
}
}
private class run implements Runnable {
@Override
public void run() {
while (true) {
try {
if (Thread.interrupted()) {
break;
}
Thread.currentThread().sleep(100);
} catch (Exception e) {
break;
}
count++;
jtf.setText("\"count\"=" + count);
}
t1=null;
}
}
}
追问
这是用什么软件运行出来的?我用NetBeans照着敲的代码一直有错,没法运行
能给个联系方式吗?wx或者q
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询