改变Jpanels

时间:2013-04-02 21:24:40

标签: java swing timer jpanel jbutton

我正在尝试让它显示一个JPanel并删除另一个依赖于java中哪个计时器关闭。这是代码的剪辑。 mainMenu和pongField是JPanels,它位于一个每毫秒起作用的计时器内;

if (SwingUtilities.getAncestorOfClass(JFrame.class, pongField) != null){
    if (!pongField.getTimer().isRunning())
    {
        mainMenu = new MainMenu(screenX, screenY, myColor, background, contentPane);
        contentPane.remove(pongField);
    }}
    if (SwingUtilities.getAncestorOfClass(JFrame.class, mainMenu) != null){
    if (!mainMenu.getTimer().isRunning())
    {
        switch(mainMenu.getButton())
        {
        case 1:
            pongField = new PongField(screenX, screenY, 0, moderator, player1UpControl, player2UpControl, player1DownControl, player2DownControl, myColor, background, contentPane, speed);
            mainMenu.setButton(0);
            contentPane.remove(mainMenu);
            break;
        case 2:

            break;
        case 3:

            break;
        case 4:

            break;
        }
    }}

让我更具体一点。 我正在尝试制作Pong,一个JPanel是实际游戏,而另一个是主菜单。如果游戏结束,计时器停止,如果我按下主菜单上的按钮,计时器停止并将getButton()设置为一个数字,具体取决于按下的数字。例如,如果按下单按钮按钮,它会创建游戏并摆脱当前的Jpanel。但如果游戏结束,它将摆脱当前的JPanel并显示主菜单。此外,每个JPanel在创建时都会设置自己的边界,背景颜色,并将自身添加到JFrame中。

问题是当我运行它并且游戏结束时它不会改变回来,除非我最小化它并将其恢复但是然后按钮将不起作用。这是MainMenu的代码。 PongField的代码太长了,但我想我总结了它的功能,但如果你愿意,我也可以发布它。

公共类MainMenu扩展了JPanel {     private static final long serialVersionUID = 1L;

//JButtons
private JButton singleJButton, multiJButton, settingsJButton, exitJButton;

//timer if game is still on
private Timer runningTimer;

//JLabel for title
private JLabel titleJLabel;

//screen size
int screenX, screenY;

//color of text and background
private Color myColor, background;

//Container of the JFrame
private Container contentPane;

//which button was clicked
private int buttonPressed;

public MainMenu(int x, int y, Color myC, Color backg, Container contentP)
{
    super();

    //Initialize all given variables
    screenX = x;
    screenY = y;
    myColor = myC;
    background = backg;
    contentPane = contentP;


    setBounds(0,0,screenX,screenY);
    setBackground(background);
    contentPane.add(this);
    setFocusable(true);
    requestFocusInWindow();

    singleJButton = new JButton();
    singleJButton.setBounds(100, 100, 100, 50);
    singleJButton.setText("Single Player");
    singleJButton.setFocusable(false);
    contentPane.add(singleJButton);
    singleJButton.addActionListener(new ActionListener()
    {public void actionPerformed( ActionEvent event )
    {singleJButtonAction(event);}});

    runningTimer = new Timer( 30, new ActionListener() 
    {public void actionPerformed( ActionEvent event )
    {}});

    runningTimer.start();
}

public Timer getTimer()
{
    return runningTimer;
}

public int getButton()
{
    return buttonPressed;
}

public void setButton(int val)
{
    buttonPressed = val;
}

private void singleJButtonAction(ActionEvent e)
{
    runningTimer.stop();
    buttonPressed = 1;
}

//make this panel have focus
public void focus()
{
    requestFocusInWindow();
}

2 个答案:

答案 0 :(得分:1)

什么,它不起作用?我只是猜测,因为你没有说。

如果你动态创建和删除窗格,我会看到第一件事。隐藏它们会更有效率。当然,我不知道你的布局是什么样的。另一种选择是在启动时使用所有其他GUI内容构建它们并继续添加和删除它们,但不要根据计时器重新创建它们。只需显示它们并根据需要隐藏它们。像这样:

mainMenu.setVisible( true );
mainMenu.setVisible( false );

每毫秒也很快。我看着棘手,说一秒钟左右。当然,这取决于你在做什么。

HTH

答案 1 :(得分:0)

我仍然无法理解您的问题是什么?

代码中似乎缺少的一件事是你添加你创建的mainMenu和pongField。您可以通过contentPane.remove(pongField);将其从contentPane中移除,contentPane.remove(mainMenu);是您的问题吗?

相关问题