一个GUI和线程的基本问题

quansheng3510 2008-03-30 01:38:57
我今天写了个程序,实现的功能是在JTEXTFIELD中连续显示顺序数字,但是我写了好久,功能却始终不能实现,调试很久也不行,求助
import java.awt.BorderLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class ThreadTest1 extends JFrame {
JTextField l;

private static int count = 0;

private Test12 m;

public ThreadTest1() {
super();
this.setSize(300, 400);
this.setLayout(new BorderLayout());
JPanel m1 = new JPanel();
l = new JTextField(10);
l.addActionListener(new Thread1());
m = new Test12(l);
m1.add(l, BorderLayout.SOUTH);
this.add(m1);
}

class Thread1 implements ActionListener {

public void actionPerformed(ActionEvent e) {
m = new Test12(l);
m.start();
}
}
}

public class ThreadTest {
public static void main(String[] args) {
ThreadTest1 l = new ThreadTest1();
l.setVisible(true);
}
}
------------------------------------------------------------------------
import javax.swing.JTextField;

public class Test12 extends Thread {
private JTextField m;
private static int count=0;
public Test12(JTextField k)
{
this.m=k;
}
public void run()
{
try{
m.setText(Integer.toString(count));
count = count + 1;
Thread.sleep(3000);

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
...全文
48 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
quansheng3510 2008-03-30
  • 打赏
  • 举报
回复
我改了下,终于可以了,也不知道是那里的问题,但是原始程序并没有错,不过还是谢谢了

import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class ThreadTest1 extends JFrame {
JTextField l;

Button m2;

private static int count = 0;

private Test12 m;

public ThreadTest1() {
super();
this.setSize(300, 400);
this.setLayout(new BorderLayout());
m2 = new Button("Start");
JPanel m1 = new JPanel();
l = new JTextField(10);
m2.addActionListener(new Thread1());
m1.add(l, BorderLayout.SOUTH);
m1.add(m2, FlowLayout.LEFT);
this.add(m1);
}

class Thread1 implements ActionListener {

public void actionPerformed(ActionEvent e) {
m = new Test12(m2, l);
m.start();
}
}
}

public class ThreadTest {
public static void main(String[] args) {
ThreadTest1 l = new ThreadTest1();
l.setVisible(true);
}
}
import javax.swing.JFrame;

public class ThreadTest {
public static void main(String[] args) {
ThreadTest1 l = new ThreadTest1();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setVisible(true);
}
}




import javax.swing.JTextField;

public class Test12 extends Thread {
private JTextField m;
private static int count=0;
public Test12(JTextField k)
{
this.m=k;
}
public void run()
{
try{
while(true)
{
m.setText(Integer.toString(count));
count = count + 1;
Thread.sleep(3000);
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
}



awusoft 2008-03-30
  • 打赏
  • 举报
回复
按多次回车的话会报错.
awusoft 2008-03-30
  • 打赏
  • 举报
回复

import java.awt.BorderLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class ThreadTest1 extends JFrame {
JTextField l;

private static int count = 0;

private Test12 m;

public ThreadTest1() {
super();
this.setSize(300, 400);
this.setLayout(new BorderLayout());
JPanel m1 = new JPanel();
l = new JTextField(10);
l.addActionListener(new Thread1());
m = new Test12(l);
m1.add(l, BorderLayout.SOUTH);
this.add(m1);
}

class Thread1 implements ActionListener {

public void actionPerformed(ActionEvent e) {
if(m!=null)
{
//m = new Test12(l);
m.start();
}else
{
m = new Test12(l);
m.start();
}
}
}
}




import javax.swing.JFrame;

public class ThreadTest {
public static void main(String[] args) {
ThreadTest1 l = new ThreadTest1();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setVisible(true);
}
}




import javax.swing.JTextField;

public class Test12 extends Thread {
private JTextField m;
private static int count=0;
public Test12(JTextField k)
{
this.m=k;
}
public void run()
{
try{
while(true)
{
m.setText(Integer.toString(count));
count = count + 1;
Thread.sleep(3000);
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
quansheng3510 2008-03-30
  • 打赏
  • 举报
回复
不可以哦,应该不是这里的问题,
awusoft 2008-03-30
  • 打赏
  • 举报
回复
public void run()
{

try{
while(true)
{

m.setText(Integer.toString(count));
count = count + 1;
Thread.sleep(3000);
}
}
catch(Exception e)
{
e.printStackTrace();
}

58,446

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧