【问题标题】:How do I get my program to open a window based of user input?如何让我的程序根据用户输入打开一个窗口?
【发布时间】:2013-10-25 21:26:45
【问题描述】:

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

【问题讨论】:

    标签: java swing static non-static


    【解决方案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,然后使用卡片布局。

    【讨论】:

      【解决方案2】:

      试试这个:

      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()){}
      }
      }
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        相关资源
        最近更新 更多