这个程序中的"Hello world"显示不出来,不知道为什么。import java.awt.*;
import javax.swing.*;
public class hello extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void run()
{
JLabel l=new JLabel("Hello world");
l.setBackground(Color.pink);
l.setSize(100, 100);
l.setLocation(100,100);
add(l);
}
public static void main(String []args)
{
hello h=new hello();
h.setSize(200,200);
h.setVisible(true);
}
}
import javax.swing.*;
public class hello extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void run()
{
JLabel l=new JLabel("Hello world");
l.setBackground(Color.pink);
l.setSize(100, 100);
l.setLocation(100,100);
add(l);
}
public static void main(String []args)
{
hello h=new hello();
h.setSize(200,200);
h.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class hello extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void run()
{
JLabel l=new JLabel("Hello world");
l.setBackground(Color.pink);
l.setSize(100, 100);
l.setLocation(100,100);
add(l);
}
public static void main(String []args)
{
hello h=new hello();
h.run();//你写的run函数要运行
h.setSize(200,200);
h.setVisible(true);
}
}
感觉还是有点不对,为什么我在书上看到JFrame上是无法直接加组件的,但这段代码JFrame直接加组件却是正确的,不知道是为什么?
import javax.swing.*;
public class hello extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void run()
{
JLabel l=new JLabel("Hello world");
l.setBackground(Color.pink);
l.setSize(100, 100);
l.setLocation(100,100);
JPanel p = new JPanel();
p.add(l);
}
public static void main(String []args)
{
hello h=new hello();
h.setSize(200,200);
h.setVisible(true);
h.run();
}
}
把run() 去掉