2014-01-17 61 views
-3

我們正在製作Bananagrams遊戲。我們想使用一個mouseListener(我們不知道如何編寫)來點擊一個JButton,然後在GUI中打開一個新窗口。我們應該怎麼做?如何在GUI中創建Jbutton以打開新的GUI窗口?

代碼:這是我的Bananagramz代碼!作爲

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.JPanel; 
public class Gui extends JFrame implements ActionListener { 
    //private JPanel pane; 
    private Container pane; 
    private JButton start; 
    private JRadioButton five, fifteen, ten; 
    private JLabel welcome,instructions, ins, inst, instr; 
    private int i; 
    public Gui() { 
     this.setTitle("BANANAGRAMZ"); 
     this.setSize(500,500); 
     this.setLocation(100,100); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    pane = this.getContentPane(); 
    pane.setLayout(new FlowLayout()); 
    five = new JRadioButton("Check here if you want 5 minutes to play!"); 
    ten = new JRadioButton("Check here if you want 10 minutes to play!"); 
    fifteen = new JRadioButton("Check here if you want 15 minutes to play!"); 

    welcome = new JLabel("Welcome to Bananagramz!"); 
    instructions = new JLabel("You will be given 50 tiles randomly chosen from a pot of 144."); 
    ins = new JLabel ("Your goal is to make a grid of (real) English words,"); 
    inst = new JLabel ("which will be checked (for realness) at the end, within a time limit."); 
    instr = new JLabel("Think hard, and beat the clock!"); 
    JButton start = new JButton("Go!"); 

    pane.setForeground(Color.YELLOW); 
    pane.setBackground(Color.YELLOW); 

    pane.add(welcome); 
    pane.add(five); 
    pane.add(ten); 
    pane.add(fifteen); 


    pane.add(instructions); 
    pane.add(ins); 
    pane.add(inst); 
    pane.add(instr); 

    pane.add(start); 
    } 
    public void actionPerformed(ActionEvent e){ 
    i++; 
    System.out.println(i); 
    } 
    public void mouseClicked(MouseEvent e){ 
    System.out.println("mouse cliekd"); 
    } 


    public static void main(String[] args) { 
     Gui g = new Gui(); 
     g.setVisible(true); 
    } 
} 
+0

您是否嘗試過查看Oracle提供的任何教程?檢查出來[這裏](http://docs.oracle.com/javase/tutorial/)。 – csmckelvey

+0

我已經嘗試了所有關於Oracle的東西;我不能讓任何actionListener做任何事情! – magz

+0

@magz向我們顯示您的代碼。 – Vinay

回答

2

的問題,我看到的是你宣佈

private JButton start; 

,但在你的構造你做

JButton start = new JButton("Go!"); 

應該

start = new JButton("Go!"); 

並註冊一個監聽器下面的按鈕

start.addActionListener(this);