我们正在制作Bananagrams游戏。我们想要使用一个mouseListener(我们无法弄清楚如何编写)来点击一个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 :(得分:2)
我看到的问题是你宣布了
private JButton start;
但是在你的构造函数中你做了
JButton start = new JButton("Go!");
应该是
start = new JButton("Go!");
并将监听器注册到按钮,如下所示
start.addActionListener(this);