简体   繁体   中英

JPanels not showing up in Eclipse IDE?

I'm a novice programmer, and I've been experimenting with GUI's lately. I've been working on an application on my school's computers (Windows XP, TextPad), and they compile and run fine on them. 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. I have the following classes.

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

Try calling

setVisible(true);

after

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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