0

我对编程相当陌生,我一直在尝试编写一个非常简单的菜单,以允许用户按下 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);
    }
}
}
4

2 回答 2

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,然后使用 CardLayout交换视图(这里它们将是 JPanel)。

于 2013-10-25T21:30:34.480 回答
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()){}
}
}

您似乎认为您的程序会自动等待输入,但是您必须想出一种方法来自己完成此操作,就像我在上面所做的那样,通过窗口获取输入不像从控制台获取它.

于 2013-10-25T22:12:58.597 回答