单击-Java JButton后重命名按钮
我试图改变我的按钮,从说“开始”到“停止”,当我点击它。我的尝试如下,我查了一下,试图复制指南,但我不知道我做错了什么。我可能遗漏了一些“}”,因为我遗漏了很多不相关的代码。有人能看出我做错了什么吗单击-Java JButton后重命名按钮,java,swing,jframe,jbutton,actionlistener,Java,Swing,Jframe,Jbutton,Actionlistener,我试图改变我的按钮,从说“开始”到“停止”,当我点击它。我的尝试如下,我查了一下,试图复制指南,但我不知道我做错了什么。我可能遗漏了一些“}”,因为我遗漏了很多不相关的代码。有人能看出我做错了什么吗 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class PipeGameApp extends JFrame implements Acti
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PipeGameApp extends JFrame implements ActionListener {
private static int BOARD_SIZE = 11;
private PipeGame game; // The model
private PipeGameView view; // The view
// This constructor builds the window
public PipeGameApp(String title) {
super(title);
game = new PipeGame(BOARD_SIZE);
view = new PipeGameView(game);
//THE TOP BAR
JPanel topBar = new JPanel();
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
ButtonGroup bg1 = new ButtonGroup();
JRadioButton rb1 = new JRadioButton("2 minutes", true);
rb1.addActionListener(this);
JRadioButton rb2 = new JRadioButton("10 minutes", false);
JRadioButton rb3 = new JRadioButton("No Time Limit", false);
bg1.add(rb1);
bg1.add(rb2);
bg1.add(rb3);
topBar.add(startButton);
topBar.add(rb1);
topBar.add(rb2);
topBar.add(rb3);
//END OF TOP BAR
//THE BOTTOM BAR
JPanel bottomBar = new JPanel();
JLabel timeLeft = new JLabel("Time Left: ");
JProgressBar bar = new JProgressBar();
bottomBar.add(timeLeft);
bottomBar.add(bar);
bottomBar.setVisible(false);
//end of bottom
/*
//bottom 2
int fscore=10;
JPanel bottomBar2 = new JPanel();
JLabel score = new JLabel("Final score:" + fscore);
bottomBar2.add(score);
bottomBar2.setVisible(false);
*/
getContentPane().add(view); //CHANGE LOCATION OF BOARD GAME HERE BorderLayout.SOUTH
getContentPane().add(topBar, BorderLayout.NORTH);
getContentPane().add(bottomBar, BorderLayout.SOUTH);
//getContentPane().add(bottomBar2, BorderLayout.SOUTH);
// Add the listeners to the view's buttons
for (int r = 0; r < BOARD_SIZE; r++) {
for (int c = 0; c < BOARD_SIZE; c++) {
view.getButton(r, c).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handleTileSelection(e);
}
});
}
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setSize(446,466);
setSize(446, 530);
setResizable(false);
}
// Handle a Tile Selection. Just change the model and update the view.
private void handleTileSelection(ActionEvent e) {
// Find the row and column of the pressed button, then make the change
int r = 0, c = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (e.getSource() == view.getButton(i, j)) {
if (game.placePipe(i, j)) {
view.update();
}
return;
}
}
}
}
// This is where it all begins
public static void main(String[] args) {
new PipeGameApp("The Frantic Pipe Layer").setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
startButton.setText("asdf");
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.event.*;
公共类PipeGameApp扩展JFrame实现ActionListener{
专用静态int板_尺寸=11;
私有PipeGame游戏;//模型
私有PipeGameView视图;//视图
//此构造函数构建窗口
公共PipeGameApp(字符串标题){
超级(标题);
游戏=新游戏(棋盘大小);
视图=新游戏视图(游戏);
//顶栏
JPanel topBar=新的JPanel();
JButton startButton=新JButton(“开始”);
addActionListener(这个);
ButtonGroup bg1=新建ButtonGroup();
JRadioButton rb1=新的JRadioButton(“2分钟”,真);
rb1.addActionListener(本);
JRadioButton rb2=新的JRadioButton(“10分钟”,错误);
JRadioButton rb3=新的JRadioButton(“无时间限制”,false);
bg1.add(rb1);
bg1.add(rb2);
bg1.add(rb3);
添加(开始按钮);
顶栏。添加(rb1);
顶栏。添加(rb2);
顶栏。添加(rb3);
//顶杆末端
//底线
JPanel bottomBar=新的JPanel();
JLabel timeLeft=新JLabel(“剩余时间:”);
JProgressBar=新的JProgressBar();
底部栏。添加(timeLeft);
添加(条);
底部栏。设置可见(假);
//底端
/*
//底部2
int fscore=10;
JPanel bottomBar2=新的JPanel();
JLabel分数=新的JLabel(“最终分数:+fscore”);
2.添加(分数);
底部条形图2.setVisible(假);
*/
getContentPane().add(view);//在BorderLayout.SOUTH此处更改棋盘游戏的位置
getContentPane().add(顶栏,BorderLayout.NORTH);
getContentPane().add(底部栏,BorderLayout.SOUTH);
//getContentPane().add(bottomBar2,BorderLayout.SOUTH);
//将侦听器添加到视图的按钮中
用于(int r=0;r
您在main
方法之后保留了构造函数的端部支撑。这是正确的代码
public PipeGameApp(String title) {
super(title);
JPanel topBar = new JPanel();
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
}
public static void main(String[] args) {
new PipeGameApp("The Frantic Pipe Layer").setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
startButton.setText("asdf");
}
您的问题是变量范围有限。startButton变量正在构造函数中声明,因此仅在构造函数中可见。您需要在类中声明它,而不是在构造函数中重新声明它,从而使类的其余部分能够“看到”变量并使用它 i、 例如,改变这一点:
public class PipeGameApp extends JFrame implements ActionListener {
private static int BOARD_SIZE = 11;
private PipeGame game; // The model
private PipeGameView view; // The view
public PipeGameApp(String title) {
JButton startButton = new JButton("Start");
startButton.addActionListener(this);
// etc...
为此:
public class PipeGameApp extends JFrame implements ActionListener {
private static int BOARD_SIZE = 11;
private PipeGame game; // The model
private PipeGameView view; // The view
private JButton startButton; // *** note change ***
public PipeGameApp(String title) {
startButton = new JButton("Start"); // *** note change ***
startButton.addActionListener(this);
// etc...
或者:
- 使用JToggleButton
- 或者使用ActionEvent的
方法返回的对象,并根据其当前状态设置其新状态getSource()
@Override
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if (source instanceof JButton) {
if (ae.getText().equals("Start")) {
ae.setText("Stop");
// do other stuff
} else if (ae.getText().equals("Stop")) {
ae.setText("Start");
// do more stuff
}
}
}
关于
“我可能遗漏了一些”}”,因为我遗漏了很多不相关的代码。”
请投入努力,以免发生这种情况。丢失的“}”不应该丢失,它使我们更难理解您的代码并帮助您。如果你要求其他人在空闲时间尽力帮助你,那么不要发布垃圾代码并不是要求你太多。请参见编辑我的答案。您的问题不是变量阴影,而是变量范围。我很惊讶您的代码能够编译,因为您没有对startButton的类引用。或者它实际上没有编译?非常感谢,我会记住发布我所有的代码。我想我会使用getSource方法,因为我需要按钮来切换。@user1692517:很高兴你已经解决了这个问题。请理解,“我所有的代码”通常太多了。最重要的是,不要像最初那样发布垃圾代码。缺少大括号并非无关紧要,但事实上非常重要。这导致一个助手Ravindra浪费了他自己宝贵的空闲时间来帮助你解决一个对他不公平的问题。