[英]JPanels not showing up in Eclipse IDE?
我是一个新手程序员,最近一直在尝试使用GUI。 我一直在研究学校计算机(Windows XP,TextPad)上的应用程序,它们可以在它们上编译并正常运行。 但是,当我在家用计算机(Mac OS Mountain Lion,Eclipse)上运行完全相同的代码时,JPanel似乎没有正确添加到JFrame中。 我有以下课程。
Main.java:
public class Main {
public static void main( String[] args ) {
new Frm();
} // end of method main()
} // end of class Main
Frm.java:
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class Frm extends JFrame {
private final int HEIGHT = 400;
private final int WIDTH = 600;
public Frm() {
setSize(WIDTH, HEIGHT);
setTitle("SHREK");
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new Pnl());
} // end of constructor
} // end of class Frm
Pnl.java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
@SuppressWarnings("serial")
public class Pnl extends JPanel {
public Pnl() {
BorderFactory.createLineBorder(Color.BLACK, 5);
setBackground(Color.BLACK);
} // end of constructor
} // end of class Pnl
尝试致电
setVisible(true);
后
getContentPane().add(new Pnl());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.