Getter和Setter不为Jbutton工作
我正在创建一个迷宫,可以使用箭头键移动玩家。启动程序后,创建了一个框架,其中包含两个Jpanels菜单和Mazepanel。菜单上有一个按钮“一步一步”,该按钮可以撤消最后一步。
此按钮应在第一步之前使用“设定(false)”和单击一次后使用“设置(false)”。单击它后,如果玩家再次移动,应再次启用它。
播放器类包含布尔值clickable = false,并在每一步之后将其设置为true。
这就是创建帧的方式。请注意,Mazepanel将菜单实例作为参数。
public class Main extends JFrame {
public Main(){
frame = new JFrame();
menu = new Menu();
frame.add(menu);
mazepanel = new MazePanel(frame, menu);
frame.add(mazepanel);
}
}
创建该按钮并禁用菜单类的构造函数,并创建一个getter和setter。
public class Menu extends JPanel {
private JButton one_step_back;
public Menu() {
one_step_back = new JButton("One step back");
one_step_back.setEnabled(false);
}
public JButton getOne_step_back() {
return one_step_back;
}
public void setOne_step_back(JButton one_step_back) {
this.one_step_back = one_step_back;
}
}
Mazepanel班级看起来像这样:
public class MazePanel extends JPanel {
private JFrame frame;
private JPanel menu;
public MazePanel(JFrame frame, JPanel menu) {
this.frame = frame;
this.menu = menu;
play = new Player();
}
public JPanel getMenu() {
return menu;
}
public void setMenu(JPanel menu) {
this.menu = menu;
}
}
和玩家班: 每次按键都会由钥匙单元调用功能移动。我只是在使用 在这种情况下,一个关键是要简短。
每当进行移动时,我都试图从播放器类中禁用菜单中的按钮。 现在。目前,我只是想在移动后激活该按钮,因此不必担心单击按钮后停用该按钮。
public class Player implements KeyListener{
boolean clickable = false;
private JPanel menu;
public Player(){
menu = panel.getMenu();
}
public void move() {
clickable = true;
menu.setOne_step_back(getOne_step_back().setEnable(clickable));
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
move();
}
}
}
但是,玩家类中的这一行将
menu.setOne_step_back(getOne_step_back().setEnable(clickable));
获得以下问题:对于类型播放器,方法getOne_step_back()未定义。
如果我只是使用以下内容,
menu.getOne_step_back();
则会得到以下内容:
对于JPANEL类型,方法One_step_back()是未定义的。
我需要更改以使getter和setter起作用并启用按钮?
I am creating a maze, where a player can be moved using the arrow keys. When the program is started, a frame is created which contains two JPanels Menu and MazePanel. The menu has a button "One step back" which allows the player to undo the last move.
This button should be disabled using "setEnable(false)" before the first move and after it has been clicked once. After it has been clicked, it should be enabled again if the player moves again.
The Player class contains the boolean clickable = false and sets it to true after every move.
This is how the frame is created. Note that the mazepanel gets the menu instance as a parameter.
public class Main extends JFrame {
public Main(){
frame = new JFrame();
menu = new Menu();
frame.add(menu);
mazepanel = new MazePanel(frame, menu);
frame.add(mazepanel);
}
}
The Button is created and disabledin the constructor of the Menu class and a getter and setter are created.
public class Menu extends JPanel {
private JButton one_step_back;
public Menu() {
one_step_back = new JButton("One step back");
one_step_back.setEnabled(false);
}
public JButton getOne_step_back() {
return one_step_back;
}
public void setOne_step_back(JButton one_step_back) {
this.one_step_back = one_step_back;
}
}
The MazePanel class looks like this:
public class MazePanel extends JPanel {
private JFrame frame;
private JPanel menu;
public MazePanel(JFrame frame, JPanel menu) {
this.frame = frame;
this.menu = menu;
play = new Player();
}
public JPanel getMenu() {
return menu;
}
public void setMenu(JPanel menu) {
this.menu = menu;
}
}
And the Player class:
The function move gets called by a Keylistener every time a key is pressed. I'm just using
one key in this case to keep it short.
I am trying to disable the button in menu from the Player class whenever a move is made.
Right now. For now, I am just trying to activate the Button after a move, so don't worry about deactivating the button after it is clicked.
public class Player implements KeyListener{
boolean clickable = false;
private JPanel menu;
public Player(){
menu = panel.getMenu();
}
public void move() {
clickable = true;
menu.setOne_step_back(getOne_step_back().setEnable(clickable));
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
move();
}
}
}
However, this line in the Player class
menu.setOne_step_back(getOne_step_back().setEnable(clickable));
gets the following issue: The method getOne_step_back() is undefined for the type Player.
If I just use instead
menu.getOne_step_back();
I get the following:
The method One_step_back() is undefined for the type JPanel.
What do I have to change in order make the getter and setter work and enable the button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您在评论中的帮助。实际上,一个问题是必须是
第二部分,即可禁用按钮,就足以使用下面的代码,因此无需使用设置器。
Thanks for your help in the comments. One issue was in fact that it has to be
For the second part, to disable the button, it is enough to use the code below, so there's no need to use a setter.