0

おそらく単純な間違いですが、何が悪いのかわかりません。フレーム(MainFrame)を作成し、メソッドを使用してパネルを変更するクラスがあります。パネルが記述されている別のクラスがあります。しかし、なぜかパネルがなくフレームしか見えません。誰かここで私を助けてくれませんか? 私は MigLayout の初心者で、私の間違いを説明していただければ本当に助かります..

public class MainFrame extends JFrame
{
private JPanel panel;

//getting dimensions
public static  Dimension dim = Toolkit.getDefaultToolkit().getScreenSize() ;

public MainFrame()
{
    getContentPane().setLayout(new MigLayout());
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    this.setTitle("Title");
    this.setLocation((int)dim.getWidth()/3,(int)dim.getHeight()/4);
    this.setSize(500, 500);     

    setNewPanel(new MainWindowPanel());
    this.validate();
}

public final void setNewPanel(JPanel newPanel)
{
    //to change the panel, old one has to be deleted
    if (panel != null) remove(panel);

    getContentPane().setLayout(new MigLayout());
    add(newPanel);

    //pack();
    panel = newPanel;
    this.setVisible(true);  
}
}

私のパネルクラス

   public class MainWindowPanel  extends JPanel
   {
//Label
JLabel greeting = new JLabel("Welcome:");

//Buttons
JButton helpButton = new JButton("Help?");

public MainWindowPanel() 
{

    // the layout of the main screen
    JPanel p = new JPanel(new MigLayout("fill", "[center]"));

    p.setBackground(Color.lightGray);
    p.add(greeting,  "skip 1, gaptop 40, wrap");
    greeting.setFont(times20);

    p.add(helpButton, "bottom, span, tag help");

    }
}

ありがとうございました!!

4

1 に答える 1

2

MainWindowPanel のコンストラクターで、新しいパネルを作成し、それにボタン/ラベルを追加します。新しく作成したパネルを追加する必要はありません。次の行を追加します。

 add(p);

実際、深くネストされたパネルで何を達成したいのかよくわかりません。

 public MainWindowPanel() {
      setLayout(new MigLayout( ... contraints);
      add(greetings);
      add(button);
 }

そして、あなたがそれに取り組んでいる間: JPanelを拡張せずに使用することを検討してください:

 JComponent mainWindowPanel = new JPanel(new MigLayout(...));
 JLabel greetings = ... // create and configure
 mainWindowPanel.add(greetings); 
 JButton button = ... // create and configure
 mainWindowPanel.add(button);
于 2012-01-19T12:54:39.137 に答える