2014-01-17 62 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);