Frame不会为空并使用if语句重绘

时间:2016-05-17 19:04:00

标签: java

我有一个if语句来检查一个真或假,enterGame,它会将东西添加到JFrame中,有一个按钮可以将布尔值更改为真,并尝试在清空后替换JFrame中的内容。一旦我删除了所有内容,它就不会重新绘制我的框架。水晶是我的老师,我们都很难过。

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;

public class GUIDemo implements ActionListener
{
int mapX, mapY;

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JPanel mainPanel = new JPanel();
private JPanel logonPanel = new JPanel();
private JLabel msglabel;
private JButton logButton;
boolean enterGame = false;
JLabel userLabel = new JLabel("Username:", JLabel.CENTER);
JLabel passLabel = new JLabel("Password:", JLabel.CENTER);

JLabel[][] maplabels = new JLabel[3][3];

public String[][] placeNames = { {"Forest", "Desert", "Jungle"}, 
{"Ice Cave", "Village", "Wizard's Tower"}, 
{"Lake", "Moutain", "Ocean"}};

public GUIDemo()
{
prepareGUI();
}

public static void main(String[] args)
{
GUIDemo swingDemo = new GUIDemo(); 
swingDemo.showDemo(); 
}

private void prepareGUI()
{
mainFrame = new JFrame("Swing Demo");
mainFrame.setSize(350,200);
mainFrame.setLayout(new BorderLayout());

mainPanel.setLayout(new BorderLayout());
logonPanel.setLayout(new GridLayout(3, 1));

//mainFrame.setLayout(null);

headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER); 

mainFrame.addWindowListener(new WindowAdapter() 
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit(0);
} 
}); 
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());

mainFrame.add(logonPanel, BorderLayout.CENTER);
//mainFrame.add(mainPanel, BorderLayout.CENTER);
//mainFrame.add(statusLabel);
mainPanel.setVisible(false); 
logonPanel.setVisible(true); 

mainFrame.setVisible(true); 

}

private void showDemo()
{ 
if (enterGame == true)
{
//logonPanel.setVisible(false); 

//mainFrame.setVisible(false); 
mainFrame.remove(logonPanel);
mainFrame.validate();
mainFrame.repaint();
//mainFrame.removeAll();
headerLabel.setText("Location: " + placeNames[mapX][mapY]);

JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
panel.setSize(300,300);
panel2.setSize(300,300);
GridLayout compassLayout = new GridLayout(3,3);

JButton button1 = new JButton("");
JButton button2 = new JButton("North");
JButton button3 = new JButton("");
JButton button4 = new JButton("West");
JButton button5 = new JButton("");
JButton button6 = new JButton("East");
JButton button7 = new JButton("");
JButton button8 = new JButton("South");
JButton button9 = new JButton("");
button1.setVisible (false);
button2.addActionListener(this);
button3.setVisible (false);
button4.addActionListener(this);
button5.setVisible (false);
button6.addActionListener(this);
button7.setVisible (false);
button8.addActionListener(this);
button9.setVisible (false);


GridLayout MapLayout = new GridLayout(3,3);

maplabels[0][0] = new JLabel("F", JLabel.CENTER);
maplabels[0][1] = new JLabel("D", JLabel.CENTER);
maplabels[0][2] = new JLabel("J", JLabel.CENTER);
maplabels[1][0] = new JLabel("I", JLabel.CENTER);
maplabels[1][1] = new JLabel("V", JLabel.CENTER);
maplabels[1][2] = new JLabel("W", JLabel.CENTER);
maplabels[2][0] = new JLabel("L", JLabel.CENTER);
maplabels[2][1] = new JLabel("M", JLabel.CENTER);
maplabels[2][2] = new JLabel("O", JLabel.CENTER);

panel.setBorder(BorderFactory.createLineBorder(Color.black));
panel.setLayout(compassLayout); 
panel.add(button1);
panel.add(button2); 
panel.add(button3); 
panel.add(button4); 
panel.add(button5); 
panel.add(button6); 
panel.add(button7); 
panel.add(button8); 
panel.add(button9); 

panel2.setBorder(BorderFactory.createLineBorder(Color.black));
panel2.setLayout(MapLayout); 
panel2.add(maplabels[0][0]);
panel2.add(maplabels[0][1]); 
panel2.add(maplabels[0][2]); 
panel2.add(maplabels[1][0]); 
panel2.add(maplabels[1][1]); 
panel2.add(maplabels[1][2]); 
panel2.add(maplabels[2][0]); 
panel2.add(maplabels[2][1]); 
panel2.add(maplabels[2][2]); 

mainPanel.add(headerLabel, BorderLayout.PAGE_START);
mainPanel.add(panel, BorderLayout.LINE_START);
mainPanel.add(panel2, BorderLayout.CENTER);
mainFrame.add(mainPanel, BorderLayout.CENTER);
}
else
{
//mainFrame.removeAll();
BorderLayout borderUser = new BorderLayout();
BorderLayout borderPass = new BorderLayout();
JPanel userPanel = new JPanel(borderUser);
JPanel passPanel = new JPanel(borderPass);
logButton = new JButton("Log On");
logButton.addActionListener(this);


JTextField userField = new JTextField();
JTextField passField = new JTextField();

userPanel.add(userLabel, BorderLayout.CENTER);
userPanel.add(userField, BorderLayout.PAGE_END);

passPanel.add(passLabel, BorderLayout.CENTER);
passPanel.add(passField, BorderLayout.PAGE_END);

logonPanel.add(userPanel);
logonPanel.add(passPanel);
logonPanel.add(new JPanel());
logonPanel.add(logButton); 
}

mainFrame.validate();
mainFrame.repaint();
mainFrame.setVisible(true); 
}

public void actionPerformed(ActionEvent e) 
{
String buttonName = ((JButton) e.getSource()).getText(); 
//userLabel.setText(buttonName);

if(buttonName == "Log On")
{
//userLabel.setText("HI");
enterGame = true;
this.showDemo(); 

}
else
{
maplabels[mapX][mapY].setBorder(BorderFactory.createEmptyBorder());

if(buttonName == "North")
{
mapX = (3 + mapX - 1)% 3;
}
if(buttonName == "South")
{
mapX = (3 + mapX + 1)% 3;
}
if(buttonName == "West")
{
mapY = (3 + mapY - 1)% 3;
}
if(buttonName == "East")
{
mapY = (3 + mapY + 1)% 3;
} 

maplabels[mapX][mapY].setBorder(BorderFactory.createLineBorder(Color.blue));
headerLabel.setText("Location: " + placeNames[mapX][mapY]);
} 
}
}

1 个答案:

答案 0 :(得分:0)

您的代码中有两个错误。首先,您将字符串与双等号进行比较,即(==)。这永远不会做你的工作。在java中,如果您要比较的东西不是原始类型(String不是),那么它将进行引用比较。因此,您的比较buttonName == 某事永远不会返回true。

其次,登录后会出现空白窗口,因为您尚未将mainPanel的可见性设置为true。以下是有效的更正代码:

public class GUIDemo implements ActionListener
{
    int mapX, mapY;

    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;
    private JPanel mainPanel = new JPanel();
    private JPanel logonPanel = new JPanel();
    private JLabel msglabel;
    private JButton logButton;
    boolean enterGame = false;
    JLabel userLabel = new JLabel("Username:", JLabel.CENTER);
    JLabel passLabel = new JLabel("Password:", JLabel.CENTER);

    JLabel[][] maplabels = new JLabel[3][3];

    public String[][] placeNames = { {"Forest", "Desert", "Jungle"}, 
                                     {"Ice Cave", "Village", "Wizard's Tower"}, 
                                     {"Lake", "Moutain", "Ocean"}};

    public GUIDemo()
    {
        prepareGUI();
    }

    public static void main(String[] args)
    {
        GUIDemo swingDemo = new GUIDemo(); 
        swingDemo.showDemo(); 
    }

    private void prepareGUI()
    {
        mainFrame = new JFrame("Swing Demo");
        mainFrame.setSize(350,200);
        mainFrame.setLayout(new BorderLayout());

        mainPanel.setLayout(new BorderLayout());
        logonPanel.setLayout(new GridLayout(3, 1));

        //mainFrame.setLayout(null);

        headerLabel = new JLabel("",JLabel.CENTER );
        statusLabel = new JLabel("",JLabel.CENTER); 

        mainFrame.addWindowListener(new WindowAdapter() 
        {
        public void windowClosing(WindowEvent windowEvent)
        {
        System.exit(0);
        } 
        }); 
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(logonPanel, BorderLayout.CENTER);
        //mainFrame.add(mainPanel, BorderLayout.CENTER);
        //mainFrame.add(statusLabel);
        mainPanel.setVisible(false); 
        logonPanel.setVisible(true); 

        mainFrame.setVisible(true); 

    }

    private void showDemo()
    { 
        if (enterGame == true)
        {

            //logonPanel.setVisible(false); 

            //mainFrame.setVisible(false); 
            mainFrame.remove(logonPanel);
            mainFrame.revalidate();
            mainFrame.repaint();
            //mainFrame.removeAll();
            headerLabel.setText("Location: " + placeNames[mapX][mapY]);

            JPanel panel = new JPanel();
            JPanel panel2 = new JPanel();
            panel.setSize(300,300);
            panel2.setSize(300,300);
            GridLayout compassLayout = new GridLayout(3,3);

            JButton button1 = new JButton("");
            JButton button2 = new JButton("North");
            JButton button3 = new JButton("");
            JButton button4 = new JButton("West");
            JButton button5 = new JButton("");
            JButton button6 = new JButton("East");
            JButton button7 = new JButton("");
            JButton button8 = new JButton("South");
            JButton button9 = new JButton("");
            button1.setVisible (false);
            button2.addActionListener(this);
            button3.setVisible (false);
            button4.addActionListener(this);
            button5.setVisible (false);
            button6.addActionListener(this);
            button7.setVisible (false);
            button8.addActionListener(this);
            button9.setVisible (false);


            GridLayout MapLayout = new GridLayout(3,3);

            maplabels[0][0] = new JLabel("F", JLabel.CENTER);
            maplabels[0][1] = new JLabel("D", JLabel.CENTER);
            maplabels[0][2] = new JLabel("J", JLabel.CENTER);
            maplabels[1][0] = new JLabel("I", JLabel.CENTER);
            maplabels[1][1] = new JLabel("V", JLabel.CENTER);
            maplabels[1][2] = new JLabel("W", JLabel.CENTER);
            maplabels[2][0] = new JLabel("L", JLabel.CENTER);
            maplabels[2][1] = new JLabel("M", JLabel.CENTER);
            maplabels[2][2] = new JLabel("O", JLabel.CENTER);

            panel.setBorder(BorderFactory.createLineBorder(Color.black));
            panel.setLayout(compassLayout); 
            panel.add(button1);
            panel.add(button2); 
            panel.add(button3); 
            panel.add(button4); 
            panel.add(button5); 
            panel.add(button6); 
            panel.add(button7); 
            panel.add(button8); 
            panel.add(button9); 

            panel2.setBorder(BorderFactory.createLineBorder(Color.black));
            panel2.setLayout(MapLayout); 
            panel2.add(maplabels[0][0]);
            panel2.add(maplabels[0][1]); 
            panel2.add(maplabels[0][2]); 
            panel2.add(maplabels[1][0]); 
            panel2.add(maplabels[1][1]); 
            panel2.add(maplabels[1][2]); 
            panel2.add(maplabels[2][0]); 
            panel2.add(maplabels[2][1]); 
            panel2.add(maplabels[2][2]); 

            mainPanel.add(headerLabel, BorderLayout.PAGE_START);
            mainPanel.add(panel, BorderLayout.LINE_START);
            mainPanel.add(panel2, BorderLayout.CENTER);
            mainFrame.add(mainPanel, BorderLayout.CENTER);
            mainPanel.setVisible(true);

        }
        else
        {
            //mainFrame.removeAll();
            BorderLayout borderUser = new BorderLayout();
            BorderLayout borderPass = new BorderLayout();
            JPanel userPanel = new JPanel(borderUser);
            JPanel passPanel = new JPanel(borderPass);
            logButton = new JButton("Log On");
            logButton.addActionListener(this);


            JTextField userField = new JTextField();
            JTextField passField = new JTextField();

            userPanel.add(userLabel, BorderLayout.CENTER);
            userPanel.add(userField, BorderLayout.PAGE_END);

            passPanel.add(passLabel, BorderLayout.CENTER);
            passPanel.add(passField, BorderLayout.PAGE_END);

            logonPanel.add(userPanel);
            logonPanel.add(passPanel);
            logonPanel.add(new JPanel());
            logonPanel.add(logButton); 
        }

        //mainFrame.getContentPane().validate();
        //mainFrame.getContentPane().repaint();
        mainFrame.setVisible(true); 
    }

    public void actionPerformed(ActionEvent e) 
    {
        String buttonName = ((JButton) e.getSource()).getText(); 


        if(buttonName.equals("Log On"))
        {

            this.enterGame = true;
            this.showDemo(); 

        }
        else
        {
            maplabels[mapX][mapY].setBorder(BorderFactory.createEmptyBorder());

            if(buttonName.equals("North"))
            {
                mapX = (3 + mapX - 1)% 3;
            }
            if(buttonName.equals("South"))
            {
                mapX = (3 + mapX + 1)% 3;
            }
            if(buttonName.equals("West"))
            {
                mapY = (3 + mapY - 1)% 3;
            }
            if(buttonName.equals("East"))
            {
                mapY = (3 + mapY + 1)% 3;
            } 

            maplabels[mapX][mapY].setBorder(BorderFactory.createLineBorder(Color.blue));
            headerLabel.setText("Location: " + placeNames[mapX][mapY]);
        } 
    }
}