当前位置: 技术问答>java相关
我写了异常简单的线程程序,大家看一看错在那里。谢谢
来源: 互联网 发布时间:2015-03-25
本文导语: 程序运行后,按钮和文本不见。为什么谢谢 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class ThreadTest{ public static void main(String[] args){ ThreadTest test=new ThreadTest(); JFrame f=new JFrame("t...
程序运行后,按钮和文本不见。为什么谢谢
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ThreadTest{
public static void main(String[] args){
ThreadTest test=new ThreadTest();
JFrame f=new JFrame("thread");
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MyThread t=test.new MyThread(f);
t.start();
}
class MyThread extends Thread{
private JFrame f;
MyThread(JFrame f){
super();
this.f=f;
}
public void run(){
JButton b=new JButton("press");
JLabel l=new JLabel("1");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
Container p=f.getContentPane();
p.add(b,BorderLayout.NORTH);
p.add(l,BorderLayout.SOUTH);
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class ThreadTest{
public static void main(String[] args){
ThreadTest test=new ThreadTest();
JFrame f=new JFrame("thread");
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MyThread t=test.new MyThread(f);
t.start();
}
class MyThread extends Thread{
private JFrame f;
MyThread(JFrame f){
super();
this.f=f;
}
public void run(){
JButton b=new JButton("press");
JLabel l=new JLabel("1");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
Container p=f.getContentPane();
p.add(b,BorderLayout.NORTH);
p.add(l,BorderLayout.SOUTH);
}
}
}
|
只要在p.add(b,BorderLayout.NORTH);
p.add(l,BorderLayout.SOUTH);
后加入f.setVisible(true);就可以了
否则按你的原程序显示的是一个空的fram只有在最大化一下或则切换一下界面后才能显示出来,因为显示的是super.f的句柄
别忘了给分呀!!!!!!!
p.add(l,BorderLayout.SOUTH);
后加入f.setVisible(true);就可以了
否则按你的原程序显示的是一个空的fram只有在最大化一下或则切换一下界面后才能显示出来,因为显示的是super.f的句柄
别忘了给分呀!!!!!!!
|
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class ThreadTest{
public static void main(String[] args){
ThreadTest test=new ThreadTest();
JFrame f=new JFrame("thread");
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MyThread t=test.new MyThread(f);
t.start();
}
class MyThread extends Thread{
private JFrame f;
MyThread(JFrame f){
super();
this.f=f;
}
public void run(){
JButton b=new JButton("press");
JLabel l=new JLabel("1");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
Container p=f.getContentPane();
p.add(b,BorderLayout.NORTH);
p.add(l,BorderLayout.SOUTH);
f.setVisible(true);//
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
class ThreadTest{
public static void main(String[] args){
ThreadTest test=new ThreadTest();
JFrame f=new JFrame("thread");
f.setSize(200,200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
MyThread t=test.new MyThread(f);
t.start();
}
class MyThread extends Thread{
private JFrame f;
MyThread(JFrame f){
super();
this.f=f;
}
public void run(){
JButton b=new JButton("press");
JLabel l=new JLabel("1");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
Container p=f.getContentPane();
p.add(b,BorderLayout.NORTH);
p.add(l,BorderLayout.SOUTH);
f.setVisible(true);//