私は初心者のプログラマーで、最近 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
From.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