Java 如何使GUI中的Jbutton打开新的GUI窗口?
我们正在做一个Bananagrams游戏。我们想使用一个鼠标侦听器,但我们不知道如何编写它来单击JButton,这将在GUI中打开一个新窗口。我们怎样才能做到这一点 代码:这是我的Bananagramz代码Java 如何使GUI中的Jbutton打开新的GUI窗口?,java,swing,user-interface,mouselistener,Java,Swing,User Interface,Mouselistener,我们正在做一个Bananagrams游戏。我们想使用一个鼠标侦听器,但我们不知道如何编写它来单击JButton,这将在GUI中打开一个新窗口。我们怎样才能做到这一点 代码:这是我的Bananagramz代码 import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JPanel; public class Gui extends JFrame implements ActionList
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);
}
}
我看到的问题是你
private JButton start;
但在你的构造器中你做到了
JButton start = new JButton("Go!");
应该是
start = new JButton("Go!");
并向下面的按钮注册一个侦听器
start.addActionListener(this);
您是否阅读过Oracle提供的任何教程?看看他们。我已经在甲骨文上试过了所有的东西;我不能让任何actionListener做任何事情@magz向我们展示您的代码。正如JJPA所说,如果您发布了您编写的代码,我们可以对其进行调整,以便您指出缺陷和错误所在,甚至可以为您解决问题。公共类Gui扩展JFrame实现ActionListener{