2013-07-25 49 views
0

我对Java有点新手,我在Swing制作的游戏中遇到问题。我有一个类guiPulUp,它有按钮和文本字段。其中一个按钮是“Magic”。我试图做到这一点,如果你点击魔术按钮,然后一个名为guiPullUpMagic的JFrame出现,并带有预设的“拼写”按钮。 这甚至可能吗?或者还有一种方法可以使用.setVisible使单击GUI时可见的GUI GUI? 请帮忙!我无法在互联网上找到与此相关的任何主题。谢谢! (如果还有其他错误,请告诉我) 另外,我非常困惑于如何让一个类中的变量在另一个类中工作,比如让我的JButton“buttonMAGIC”在我的guiPullUpMagic类中工作。JFrame和ActionListener

仅供参考,这里是我的代码(这是相当大的!):


import javax.swing.*; 

import java.lang.Math.*; 

import java.awt.event.*; 

/** 
* main GUI class 
*/ 

public class guiPullUp extends JFrame 
{ 

    public static void main(String[] args) 
    { 
     new guiPullUp(); 
    } 

    //declaring buttons for main GUI 

    private JButton buttonOK; 
    private JButton buttonUP; 
    private JButton buttonDOWN; 
    private JButton buttonRIGHT; 
    private JButton buttonLEFT; 
    private JButton buttonMAGIC; 
    private JButton buttonRUN; 
    private JTextField userINPUT; 
    private JLabel healthDisplay; 

    public guiPullUp() 
    { 
     this.setSize(600,500); 
     this.setTitle("Xenix V_1"); 
     this.setDefaultCloseOperation(
      JFrame.EXIT_ON_CLOSE); 

     //button names to appear on panel1* 
     //*button coordinates needed 

     JButton buttonOK = new JButton ("OK"); 
     JButton buttonUP = new JButton ("Up"); 
     JButton buttonDOWN = new JButton ("Down"); 
     JButton buttonRIGHT = new JButton ("Right"); 
     JButton buttonLEFT = new JButton ("Left"); 
     JButton buttonMAGIC = new JButton ("Magic"); 
     JButton buttonRUN = new JButton ("RUN"); 
     JTextField userINPUT = new JTextField(30); 
     JLabel healthDisplay = new JLabel("Health: "); 
     JPanel panel1 = new JPanel(); 

     ButtonListener bl = new ButtonListener(); 

     buttonOK.addActionListener(bl); 
     buttonUP.addActionListener(bl); 
     buttonDOWN.addActionListener(bl); 
     buttonRIGHT.addActionListener(bl); 
     buttonLEFT.addActionListener(bl); 
     buttonMAGIC.addActionListener(bl); 
     buttonRUN.addActionListener(bl); 


     panel1.add(buttonOK); 
     panel1.add(buttonDOWN); 
     panel1.add(buttonRIGHT); 
     panel1.add(buttonLEFT); 
     panel1.add(buttonMAGIC); 
     panel1.add(buttonRUN); 

     panel1.add(healthDisplay); 

     panel1.add(userINPUT); 

     this.add(panel1); 

     this.setVisible(true); 
     }  


    //declaring variables "counters" for magic count, 
    //health count, etc. 

    int healthMAX = 100; 
    int healthMINIMUM = 0; 
    int magicMAX = 100; 
    int magicMINIMUM = 0; 
    int walletSizeMAX = 9999; 
    int walletSizeMINIMUM = 0; 

    /** 
    * class used to create events 
    * based on button sources 
    */ 


    public class ButtonListener implements 
     ActionListener 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      //Haven't made these codes yet but some WILL need to bring up a GUI 
       } 

     } 
    } 
} 

的guiPullUpMagic类:


import javax.swing.*; 

import java.awt.event.*; 

import java.lang.Math.*; 

/** 
* class used to pull up the "spells" GUI 
*/ 

public class guiPullUpMagic extends JFrame 
{ 
    public void magic(String[] args) 
    { 
     new guiPullUpMagic(); 
    } 

    private JButton buttonFIREMAGIC; 
    private JButton buttonICEMAGIC; 
    private JButton buttonHEALMAGIC; 
    private JButton buttonSHOCKMAGIC; 

    public guiPullUpMagic() 
    { 
     this.setSize(400,400); 
     this.setTitle("Spells"); 
     this.setDefaultCloseOperation(
      JFrame.EXIT_ON_CLOSE); 

     //button names to appear on panel2* 
     //*button coordinates needed 

     JButton buttonFIREMAGIC = new JButton ("FireBall"); 
     JButton buttonICEMAGIC = new JButton ("Ice Flurry"); 
     JButton buttonSHOCKMAGIC = new JButton ("Spark"); 
     JButton buttonHEALMAGIC = new JButton ("Heal Minor Wounds"); 
     JPanel panel2 = new JPanel(); 

     ButtonListener bl = new ButtonListener(); 

     buttonFIREMAGIC.addActionListener(bl); 
     buttonICEMAGIC.addActionListener(bl); 
     buttonSHOCKMAGIC.addActionListener(bl); 
     buttonHEALMAGIC.addActionListener(bl); 

     panel2.add(buttonFIREMAGIC); 
     panel2.add(buttonICEMAGIC); 
     panel2.add(buttonSHOCKMAGIC); 
     panel2.add(buttonHEALMAGIC); 

     this.add(panel2); 

     this.setVisible(false);  
    } 

    public class ButtonListener implements 
     ActionListener 
    { 

     public void actionPerformed (ActionEvent e) 
     { 
      if (e.getSource() == buttonMAGIC); //THIS is where I'll need 
                  //the buttonMAGIC variable 
                  //from guiPullUp 
      { 
        //code to bring up the guiPullUpMagic GUI 
      } 
     } 
    } 
} 
+0

嗯,我会在一个时尚的JFrame实现ActionListener: 公共类guiPullUp扩展JFrame中实现的ActionListener { @覆盖 公共无效的actionPerformed(ActionEvent的五){ 如果 (e.getSource()等于(buttonFIREMAGIC。 )) { //火魔 } } – KernelPanic

回答

0

注意的变化

  1. ActionListener在类中实现GuiPullUp
  2. GuiPullUpMagic类是JDialog
  3. 检查方法actionPerformed

类GuiPullUp:

public class GuiPullUp extends JFrame implements ActionListener { 

//declaring buttons for main GUI 
private JButton buttonOK; 
private JButton buttonUP; 
private JButton buttonDOWN; 
private JButton buttonRIGHT; 
private JButton buttonLEFT; 
private JButton buttonMAGIC; 
private JButton buttonRUN; 
private JTextField userINPUT; 
private JLabel healthDisplay; 

public GuiPullUp() { 
    this.setSize(600, 500); 
    this.setTitle("Xenix V_1"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    buttonOK = new JButton("OK"); 
    buttonUP = new JButton("Up"); 
    buttonDOWN = new JButton("Down"); 
    buttonRIGHT = new JButton("Right"); 
    buttonLEFT = new JButton("Left"); 
    buttonMAGIC = new JButton("Magic"); 
    buttonRUN = new JButton("RUN"); 
    userINPUT = new JTextField(30); 
    healthDisplay = new JLabel("Health: "); 
    JPanel panel1 = new JPanel(); 


    buttonOK.addActionListener(this); 
    buttonUP.addActionListener(this); 
    buttonDOWN.addActionListener(this); 
    buttonRIGHT.addActionListener(this); 
    buttonLEFT.addActionListener(this); 
    buttonMAGIC.addActionListener(this); 
    buttonRUN.addActionListener(this); 


    panel1.add(buttonOK); 
    panel1.add(buttonDOWN); 
    panel1.add(buttonRIGHT); 
    panel1.add(buttonLEFT); 
    panel1.add(buttonMAGIC); 
    panel1.add(buttonRUN); 

    panel1.add(healthDisplay); 

    panel1.add(userINPUT); 

    this.add(panel1); 


} 
//declaring variables "counters" for magic count, 
//health count, etc. 
int healthMAX = 100; 
int healthMINIMUM = 0; 
int magicMAX = 100; 
int magicMINIMUM = 0; 
int walletSizeMAX = 9999; 
int walletSizeMINIMUM = 0; 

@Override 
public void actionPerformed(ActionEvent e) { 
    if(e.getActionCommand().equals(buttonMAGIC.getActionCommand())) { 
     new GuiPullUpMagic(this).setVisible(true); 
    } 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      GuiPullUp guiPullUp = new GuiPullUp(); 
      guiPullUp.setVisible(true); 
     } 
    }); 
} 
} 

类GuiPullUpMagic

public class GuiPullUpMagic extends JDialog implements ActionListener { 

    private JButton buttonFIREMAGIC; 
    private JButton buttonICEMAGIC; 
    private JButton buttonHEALMAGIC; 
    private JButton buttonSHOCKMAGIC; 

    public GuiPullUpMagic(JFrame parent) { 
     super(parent); 
     this.setSize(400, 400); 
     this.setTitle("Spells"); 

     buttonFIREMAGIC = new JButton("FireBall"); 
     buttonICEMAGIC = new JButton("Ice Flurry"); 
     buttonSHOCKMAGIC = new JButton("Spark"); 
     buttonHEALMAGIC = new JButton("Heal Minor Wounds"); 
     JPanel panel2 = new JPanel(); 



     buttonFIREMAGIC.addActionListener(this); 
     buttonICEMAGIC.addActionListener(this); 
     buttonSHOCKMAGIC.addActionListener(this); 
     buttonHEALMAGIC.addActionListener(this); 

     panel2.add(buttonFIREMAGIC); 
     panel2.add(buttonICEMAGIC); 
     panel2.add(buttonSHOCKMAGIC); 
     panel2.add(buttonHEALMAGIC); 

     this.add(panel2); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 

    } 
} 
+0

好吧我已经做了更改,但是如何一起运行多个类文件? –

+0

先跑1,然后点击魔术 – vels4j

+0

我不能。当它编译它说它不识别符号“guiPullUpMagic”,所以我需要一起运行它们。 –

3

如果你想一个JFrame的领域传递到另一个JFrame中,你可以一个ComponentEvent发送到JFrame中与控制

例如,你可以定义一个ComponentEvent(例如ShowPopUpEvent)用于显示的JFrame A和A会听为了那个原因。 每当你想展示一个JButton并将其传递给你时,你就可以派发该事件。