将组件添加到超类中的JPanel

时间:2015-03-25 14:12:18

标签: java jframe jpanel super jcomponent

我有三节课......

屏幕:

private JPanel screenPanel;

public Screen()
{
    screenPanel = new JPanel();
    screenPanel.setSize(new Dimension(1000, 1000));
}

public JPanel getPanel()
{
    JFrame test = new JFrame();
    test.setSize(new Dimension(1000, 1000));
    test.setTitle("Hello test");
    test.add(screenPanel);
    test.setVisible(true);
    return screenPanel;
}

public void add(JComponent toAdd)
{       
    screenPanel.add(toAdd);
}

NewTurnScreen(扩展屏幕):

private JLabel newTurnLabel, newPlayerName;
private JButton startTurnButton;

public NewTurnScreen()
{
    super();
    //initiate JComponents.
    newTurnLabel = new JLabel();
    newPlayerName = new JLabel();
    startTurnButton = new JButton();
    //set the text of the JComponents.
    newTurnLabel.setText("New Turn");
    newPlayerName.setText("New Player: " + EEG.getCurrentPlayer());
    startTurnButton.setText("Go");
    //set the layout of panel the JComponents will be added into.
    super.getPanel().setLayout(new BoxLayout(super.getPanel(), BoxLayout.Y_AXIS));
    //add the JComponents to the panel
    super.add(newTurnLabel);
    super.add(newPlayerName);
    super.add(startTurnButton); 
}

&安培;屏幕管理器:

public ScreenManager()
{
    frame = new JFrame();
    frame.setSize(new Dimension(1000, 1000));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //each screen has a JPanel
    newTurnPanel = new JPanel();        
    newTurn = new NewTurnScreen();  
}

public void setPanel(int panel)
{
    switch(panel)
    {
    case 1: 
        newTurnPanel = newTurn.getPanel();
        frame.removeAll();
        frame.add(newTurnPanel);            
        break;
    ...
   }
}

因此,取决于传入的整数值取决于ScreenManager中JFrame上显示的屏幕(或面板)。

我的问题是显示的JFrame是空的。即使屏幕类中的JFrame(测试)也没有显示添加的任何组件。

0 个答案:

没有答案