如何让我的程序根据用户输入打开一个窗口?

时间:2013-10-25 21:26:45

标签: java swing static non-static

我对编程很新,我一直在尝试编写一个非常简单的菜单,允许用户按下JRadioButton来选择摇滚,纸张,剪刀(1人或2人)的模式。我当前的代码侦听选择了哪个按钮然后将int设置为1或2.然后它接受该数字并使用它来确定在main方法中打开哪个窗口但我不知道我应该做什么,因为我可以不将静态字段引用到静态方法。

我的代码中的这一部分设置了模式,然后根据int确定要打开的窗口。

public void actionPerformed(ActionEvent e)
{
    if(p1.isSelected())
        mode = 1;
    else if(p2.isSelected())
        mode = 2;
}
public static void main(String args[])
{
    RPSMenu window = new RPSMenu();
    window.setBounds(300, 300, 400, 100);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
    if(mode == 1)
    {
    Rps window1 = new Rps();
    window1.setBounds(300, 300, 400, 160);
    window1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window1.setVisible(true);
    window.setVisible(false);
    }
    else if(mode == 2)
    {
    P2RPS window2 = new P2RPS();
    window2.setBounds(300, 300, 400, 150);
    window2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window2.setVisible(true);
    window.setVisible(false);
    }
}

如果看到我的完整代码有帮助,那就是:

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

public class RPSMenu extends JFrame
implements ActionListener
{
/**
 * 
 */
private static final long serialVersionUID = 1L;
JRadioButton p1, p2;
int mode;

public RPSMenu()
{
    p1 = new JRadioButton("   1 Player   ");
    p2 = new JRadioButton("   2 Player   ");

    ButtonGroup menu = new ButtonGroup();
    menu.add(p1);
    menu.add(p2);

    JButton go = new JButton("    Go!    ");
    go.addActionListener(this);

    Container m = getContentPane();
    m.setLayout( new FlowLayout());
    m.add(go);
    m.add(p1);
    m.add(p2);
}

public void actionPerformed(ActionEvent e)
{
    if(p1.isSelected())
        mode = 1;
    else if(p2.isSelected())
        mode = 2;
}
public static void main(String args[])
{
    RPSMenu window = new RPSMenu();
    window.setBounds(300, 300, 400, 100);
    window.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window.setVisible(true);
    if(mode == 1)
    {
    Rps window1 = new Rps();
    window1.setBounds(300, 300, 400, 160);
    window1.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window1.setVisible(true);
    window.setVisible(false);
    }
    else if(mode == 2)
    {
    P2RPS window2 = new P2RPS();
    window2.setBounds(300, 300, 400, 150);
    window2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    window2.setVisible(true);
    window.setVisible(false);
    }
}
}

2 个答案:

答案 0 :(得分:1)

您的问题是您尝试从静态main方法调用非静态代码。解决方法不是这样做,只使用main方法来设置程序并运行它,而是在类的代码本身中调用所有其他内容,无论是在构造函数中还是在init方法中。

例如,main可能看起来像:

public static void main(String[] args) {
  // to begin a Swing application in thread-safe mannter
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      Create your GUI class;
      MyGui myGui = new MyGui();
      myGui.init(); // initialize its fields, call your if blocks
      // etc...
    }
  });
}

然后在init()方法中,您可以显示一个对话框,让用户选择播放器数量,并以非静态方式初始化和显示GUI。

请注意,我不会像你正在做的那样交换窗口,而是创建并显示一个JFrame,然后使用一个交换 视图 (这里它们将是JPanels) CardLayout。

答案 1 :(得分:0)

试试这个:

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

public class RPSMenu extends JFrame
implements ActionListener
{
/**
 * 
 */
/* it's typically considered bad practice to allow other classes to access a 
 * classes' variables, they should be accessed though a method that returns them*/
private static final long serialVersionUID = 1L;
private JRadioButton p1, p2;
private int mode = 0;//you need to know if this has been set or if it is still at default value

public RPSMenu()
{
p1 = new JRadioButton("   1 Player   ");
p2 = new JRadioButton("   2 Player   ");

ButtonGroup menu = new ButtonGroup();
menu.add(p1);
menu.add(p2);

JButton go = new JButton("    Go!    ");
go.addActionListener(this);

Container m = getContentPane();
m.setLayout( new FlowLayout());
m.add(go);
m.add(p1);
m.add(p2);
}

public void actionPerformed(ActionEvent e)
{
if(p1.isSelected())
    mode = 1;
else if(p2.isSelected())
    mode = 2;
}
public int getMode()
{
return mode;
}
public static void main(String args[])
{
RPSMenu window = new RPSMenu();
window.setBounds(300, 300, 400, 100);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setVisible(true);
while(window.getMode() == 0){/*wait for someone to press a button*/}
/* the non static type of mode should be accessed only from the object in which it is used
 * however, you could also just make it static and the program would also work but that's not your only problem*/
Rps window1 = null;
P2RPS window2 = null;//initilize these here so you can use them later
if(window.getMode() == 1)
{
//I am assuming this class and the other class below are your windows
window1 = new Rps();
window1.setTitle("Single Player RPS");
window1.setBounds(300, 300, 400, 160);
window1.setDefaultCloseOperation(EXIT_ON_CLOSE);
window1.setVisible(true);
}
else if(window.getMode() == 2)
{
window2 = new P2RPS();
window2.setTitle("Two Player RPS");
window2.setBounds(300, 300, 400, 150);
window2.setDefaultCloseOperation(EXIT_ON_CLOSE);
window2.setVisible(true);
}
window.setVisible(false);
while(window1 != null && window1.isVisible()){}
while(window2 != null && window2.isVisible()){}
}
}

你好像有一个想法,你的程序会自动等待输入,但你必须想出一个方法来自己做,就像我上面做的那样,通过窗口获取输入并不像得到它从控制台。