简体   繁体   English

JPanels没有在Eclipse IDE中显示?

[英]JPanels not showing up in Eclipse IDE?

I'm a novice programmer, and I've been experimenting with GUI's lately. 我是一个新手程序员,最近一直在尝试使用GUI。 I've been working on an application on my school's computers (Windows XP, TextPad), and they compile and run fine on them. 我一直在研究学校计算机(Windows XP,TextPad)上的应用程序,它们可以在它们上编译并正常运行。 However, when I run the exact same code on my home computer (Mac OS Mountain Lion, Eclipse), the JPanel seems to not be added to the JFrame properly. 但是,当我在家用计算机(Mac OS Mountain Lion,Eclipse)上运行完全相同的代码时,JPanel似乎没有正确添加到JFrame中。 I have the following classes. 我有以下课程。

Main.java: Main.java:

public class Main {
    public static void main( String[] args ) {
        new Frm();
    } // end of method main()
} // end of class Main

Frm.java: 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: 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

Try calling 尝试致电

setVisible(true);

after

getContentPane().add(new Pnl());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM