Java 小程序,label 放到 panel 里面,panel 放到 frame 里面,为什么 label 没能显示出来呢?
/***Filename:tsForJPanel.java*@author:Amitayus*Created:2013/10/3119:08:49*/importjava...
/**
* Filename: tsForJPanel.java
* @author: Amitayus
* Created : 2013/10/31 19:08:49
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class TestFor {
public static void main(String[] args) {
JFrame jFrame = new JFrame("test for jpanel");
jFrame.setResizable(true);
jFrame.setLayout(null);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 400) / 2, ((int)dimension.getHeight() - 300) / 2, 400, 300);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(200,150));
panel.setBackground(Color.CYAN);
JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
panel.add(label1);
jFrame.add(panel);
label1.setVisible(true);
jFrame.setVisible(true);
}
} 展开
* Filename: tsForJPanel.java
* @author: Amitayus
* Created : 2013/10/31 19:08:49
*/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class TestFor {
public static void main(String[] args) {
JFrame jFrame = new JFrame("test for jpanel");
jFrame.setResizable(true);
jFrame.setLayout(null);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
jFrame.setBounds(((int)dimension.getWidth() - 400) / 2, ((int)dimension.getHeight() - 300) / 2, 400, 300);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setPreferredSize(new Dimension(200,150));
panel.setBackground(Color.CYAN);
JLabel label1 = new JLabel("姓名");
label1.setBounds(10, 10, 100, 30);
panel.add(label1);
jFrame.add(panel);
label1.setVisible(true);
jFrame.setVisible(true);
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询