我有一个简单的问题,因为我不太熟悉Java GUI。我试图让JLable在下面的代码中可见,因为我发现很难理解这个概念。但仍然看不到标签,但框架确实在运行时打开。
public class Sample extends JPanel {
public void Sample() {
JPanel p = new JPanel();
JLabel lab1 = new JLabel("User Name", JLabel.LEFT);
p.setLayout(new FlowLayout());
p.add(lab1 = new JLabel("add JLabel"));
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new Sample());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}发布于 2011-05-13 13:36:27
示例是Jpanel。
Sample扩展了JPanel意味着你继承了JPanel。
去掉JPanel p和你所有的“p”
https://stackoverflow.com/questions/5987600
复制相似问题