2013-05-29 32 views
0

嗨看看這段代碼: package arkanoid;Java JButton - 製作簡單的菜單

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import java.awt.Color; 
import java.awt.event.*; 

public class Arkanoid extends JFrame 
{ 
private static final long serialVersionUID = 6253310598075887445L; 
static JFrame frame; 


static class Action1 implements ActionListener {   
     public void actionPerformed (ActionEvent e) {  
     //frame = new JFrame("Arkanoid"); 
     frame.setLocationRelativeTo(null); 
     frame.setIgnoreRepaint(true); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     frame.setSize(500,400); 
     frame.add(new Gra()); 
     } 
    } 
    static class Action2 implements ActionListener {   
     public void actionPerformed (ActionEvent e) {  
      frame.dispose(); 
      System.exit(0); 
     } 
} 
public static void main(String[] args) 
{ 
    //new Arkanoid(); 
     frame = new JFrame("Arkanoid"); 
     frame.setSize(500,400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setTitle("Arkanoid BETA"); 
     frame.setLocationRelativeTo(null); 
     frame.setIgnoreRepaint(true); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     JPanel panel = new JPanel(); 
     frame.add(panel); 

     JButton button = new JButton("Nowa Gra"); 
     panel.add(button); 
     button.addActionListener (new Action1()); 

     JButton button2 = new JButton("Wyjscie"); 
     panel.add(button2); 
     button2.addActionListener (new Action2()); 
} 
} 

此代碼幾乎工作,我想打個button2一個退出按鈕,就像在右上框架的圖標和按鈕1 X按鈕的工作需要在同一個窗口中打開GRA()。當我這樣做這樣它不工作正常:/我需要在button1上點擊2次才能到Gra()和Gra()中的更多KeyListeners是不是工作:( 即時通訊新的按鈕,框架和麪板在Java所以請使用此代碼幫助。糾正它,請。

回答

3

有許多與你的代碼,其中最重要的是,爲什麼你button1需要點擊兩下根本問題。

但是,對於你的問題,你應該嘗試重新排列您的button1聽衆的順序,以便在將其設置爲可見之前,首先將您的Component添加到框架中。應該有效的示例:

static class Action1 implements ActionListener {   
    public void actionPerformed (ActionEvent e) { 
     frame.add(new Gra()); 
     frame.revalidate(); 
    } 
} 

請注意,您已經在main中設置了frame的大小,位置等,因此無需在每次點擊該按鈕時再次設置它們。

我強調代碼中存在比這個問題更重要的問題。您應該看看Java的修飾符類型(static在這裏似乎不適用)以及面向對象的概念(如繼承)(您定義Arkanoid類以擴展JFrame,但將JFrame對象作爲類變量)。

1

你的代碼有很多問題。我重構了一點。用下面的代碼& @ ricky116答案我認爲你應該得到他們所有人。

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

public class Arkanoid extends JFrame 
{ 
    public Arkanoid() { 
     super("Arkanoid"); 
     setSize(500,400); 
     setTitle("Arkanoid BETA"); 
     setLocationRelativeTo(null); 
     setResizable(false); 

     final JPanel panel = new JPanel(); 
     setContentPane(panel); 

     panel.add(new JButton(new AbstractAction("Nowa Gra") { 
      public void actionPerformed (ActionEvent e) {  
       panel.removeAll(); 
       panel.add(new Gra()); 
       panel.revalidate(); 
       panel.repaint(); 
      } 
     }); 

     panel.add(new JButton(new AbstractAction("Wyjscie") { 
      public void actionPerformed (ActionEvent e) {  
       Arkanoid.this.setVisible(false); 
      } 
     }); 
    } 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       Arkanoid frame = new Arkanoid(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 
2

我想打個button2一個退出按鈕工作像X按鈕位於頂部右側邊框的

您可以使用Closing an Application發現ExitAction類。

有關如何使用按鈕的其他示例,請參閱How to Use Buttons上的Swing教程。這是所有與Swing相關的問題的開始。