JPanel在Java中显示为空

时间:2015-02-19 18:04:14

标签: java jframe jpanel

我正在制作一个显示3个按钮的程序,当用户点击“单人游戏”时,它会激活创建新面板的鼠标监听器,并且应该用新创建的面板替换现有面板。但是我点击“单人游戏”后得到的是带有白色空背景的新窗口。这是代码:

public class UserInterface extends JFrame {
private JLabel singlePlayer,multiPlayer,quit;
private Container menu;
public JPanel mainPanel,fieldPanel;


private Field field;
private Snake snake1,snake2;
private Food food;


public UserInterface(){


    // Adjust window
    mainPanel=new JPanel();
    mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();


    mainPanel.setSize(700, 500);
    setSize(710, 510);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setResizable(false);
    setTitle("Snake");
    //add(mainPanel);
    setVisible(true);
    this.getContentPane().setBackground(Color.ORANGE);
    //mainPanel.setBackground(Color.GREEN);

    // Instantiate buttons
    singlePlayer=new JLabel();
    multiPlayer=new JLabel();
    quit=new JLabel();
    singlePlayer.setIcon(new ImageIcon("files/singleplayer.jpg"));
    multiPlayer.setIcon(new ImageIcon("files/multiplayer.jpg"));
    quit.setIcon(new ImageIcon("files/quit.jpg"));
    quit.addMouseListener(new Mouse());
    singlePlayer.addMouseListener(new Mouse());
    multiPlayer.addMouseListener(new Mouse());

    // Create menu panel
    menu=new JPanel(new GridLayout(3,1,0,0));
    menu.setBackground(Color.ORANGE);
    menu.add(singlePlayer);
    menu.add(multiPlayer);
    menu.add(quit);
    menu.setMaximumSize(new Dimension(164,150));

    // Insert menu to the center    
    mainPanel.add(menu, gbc);

    add(mainPanel);
}

public void singlePlayer(){
    field=new Field("single player");
    snake1=new Snake();
    food=new Food();

    setVisible(false);
    removeAll();
    mainPanel=field.getFieldPanel();
    add(mainPanel);
    //setContentPane(mainPanel);
    invalidate();
    validate();
    setVisible(true);


    Thread fieldThread=new Thread(field, "Field thread");
    //fieldThread.start();

    Thread snake1Thread=new Thread(snake1, "Snake thread");
    //snake1Thread.start();


}


private class Mouse implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource()==quit){
            System.exit(0);;
        } else if(e.getSource()==singlePlayer){
            singlePlayer();
        } else if(e.getSource()==multiPlayer){

        }
    }

这里是字段类:          public class Field extends JFrame实现了Runnable {

private JPanel fieldPanel;
private JPanel leftPanel;
private JPanel centralPanel;
private JPanel rightPanel;
private String type;

private JLabel leftScore,rightScore;
private int score;

private int foodX,foodY;
private JLabel [][] field;
private final int FIELD_WIDTH=50,FIELD_HEIGHT=50;
private static final long serialVersionUID = 1L;

public JPanel getFieldPanel(){return fieldPanel;}


public Field(String type){
    fieldPanel=new JPanel(new GridLayout(1,3,0,0));
    fieldPanel.setPreferredSize(new Dimension(700,500));
    this.type=type;

    // Adjust playground and score tables
    leftPanel=new JPanel();
    centralPanel=new JPanel();
    rightPanel=new JPanel();
    leftPanel.setMaximumSize(new Dimension(100,500));
    leftPanel.setPreferredSize(new Dimension(100,500));
    centralPanel.setMaximumSize(new Dimension(500,500));
    centralPanel.setPreferredSize(new Dimension(500,500));
    rightPanel.setMaximumSize(new Dimension(100,500));
    rightPanel.setPreferredSize(new Dimension(100,500));

    // Set leftPanel
    score=0;
    leftScore=new JLabel("Score:\n"+score);
    leftPanel.add(leftScore);

    // Adjust field
    field=new JLabel[FIELD_WIDTH][FIELD_HEIGHT];
    for(int i=0;i<FIELD_WIDTH;i++){
        for(int j=0;j<FIELD_HEIGHT;j++){
            field[i][j]=new JLabel("");
            field[i][j].setSize(new Dimension(10,10));
            field[i][j].setOpaque(true);
            field[i][j].setBackground(Color.LIGHT_GRAY);
        }
    }
    for(int i=0;i<FIELD_WIDTH;i++){
        for(int j=0;j<FIELD_HEIGHT;j++){
            if(i==0)
                field[i][j].setBackground(Color.BLACK);
            else if(j==0)
                field[i][j].setBackground(Color.BLACK);
            else if(i==FIELD_WIDTH-1)
                field[i][j].setBackground(Color.BLACK);
            else if(j==FIELD_HEIGHT-1)
                field[i][j].setBackground(Color.BLACK);
        }
    }

    // Adjust centralPanel
    centralPanel.setLayout(new GridLayout(50,50,0,0));
    for(int i=0;i<FIELD_WIDTH;i++){
        for(int j=0;j<FIELD_HEIGHT;j++){
            centralPanel.add(field[i][j]);
        }
    }

    // Adjust rightPanel
    rightPanel.add(new JLabel("Player 2 score: "));

    fieldPanel.setLayout(new BoxLayout(fieldPanel,BoxLayout.Y_AXIS));
    fieldPanel.add(leftPanel);
    fieldPanel.add(centralPanel);
    fieldPanel.add(rightPanel);



}

1 个答案:

答案 0 :(得分:0)

  

&#34;,它激活创建新面板的鼠标监听器

它不会解决您的问题,但不能使用MouseListener。 JButton旨在与ActionListener一起使用。

  

点击&#34;单人游戏&#34;是一个白色空背景的新窗口。

在创建旨在与多个面板共享相同空间的GUI时,您应该使用CardLayout来控制可见面板。阅读How to Use a CardLayout上Swing教程中的部分,了解更多信息和示例。

否则,如果您尝试手动交换面板,则基本代码应为:

panel.remove(...);
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint the components