- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在为我的 java 游戏构建一个菜单,当我单击按钮时,我需要能够切换到我的游戏 JPanel。这是我所拥有的
public class Menu extends JPanel implements ActionListener {
protected JFrame frame;
JButton btnStart = new JButton();
public Menu(JFrame j) {
super();
frame = j;
//creating and styling button
btnStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
StartGame();
}
});
}
private void StartGame(){
frame.getContentPane().removeAll();
frame.getContentPane().add(new Main(frame));
frame.repaint();
}
`公共(public)类 Main 扩展 Universe 实现 ActionListener {
Timer timer = new Timer(75, this);
JFrame frame;
//@Override
public void actionPerformed(ActionEvent e) {
repaint();;
if(isLoss()){
timer.stop();
}
//update cretures
}
public Main(JFrame frame) {
super();
frame.setLayout(null);
frame.setLayout(null);
//frame.getContentPane().setLayout(null);
this.setBackground(Color.BLACK);
this.map = new Map();
StartUpHeight = 100;
this.Size = 30;
//this.playerCol = new Collision(map, pacman, Size);
pacman = new Player(1*Size, 4*Size, Size / 5, map, this.Size);
red = new RedGhost(9*Size, 8*Size, Size / 6, map, pacman, this.Size);
pinky = new PinkGhost(8*Size, 10*Size, Size / 6, map, pacman, this.Size);
inky = new BlueGhost(9*Size, 10*Size, Size / 6, map, pacman, this.Size, red);
clyde = new YellowGhost(10*Size, 10*Size, Size/6, map, pacman, this.Size);
timer.start();
this.frame = frame;
this.frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
pacman.keyPressed(e, Size);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
pacman.keyReleased(e);
}
});
setUpPanel();
}
@Override
public void paint(Graphics g) {
//drawing map
//drawing pacman
//drawing ghosts
}
`
到目前为止,我尝试了在互联网上找到的所有内容。 Main 继承自 jpanel,并像 Menu 一样实现操作监听器。执行此代码后,我的 jframe 就会清除。我尝试重新绘制它没有帮助。任何帮助表示赞赏。谢谢
最佳答案
当你调用 repaint() 方法时,你还应该调用 revalidate() 方法。另外,你能展示一下你的主类代码吗?您是否已将按钮添加到框架中?
关于java - 在实现actionListener的jpanels之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53768340/
在另一个 ActionListener 中嵌入一个 ActionListener 更好,还是将它们作为单独的 ActionListener 更好?我有 JComboBox1、JComboBox2 和
我为我的程序制作了一个面板。它仅由 RadioButtons 组成。选择单选按钮后,我想在其他代码中设置一个 boolean 值。此面板将用作更大面板或框架的组件,该面板或框架也应该能够监听此面板内发
我想知道(如果可能的话)如何更新 JButton 的 ActionListener 执行的操作。我需要在整个程序中保留一个按钮,但根据当前情况更改它的功能。 最佳答案 首先使用removeAction
我的程序的先前版本旨在显示多个系列的魔术图像,所有这些图像都使用相同类型的 ActionListener。例如,在显示每幅图像之间设置时间的所有技巧时,我通过使用扩展 ActionListener 的
也许我做错了。让我知道使用 Swing 和 AWT,我在框架上设置了几个按钮,每个按钮都有一个与其特定功能 I.E 相对应的 ActionListener。 JButton foo_button =
我想把第一个ActionListener(About)改成第二个ActionListener(About2)如果不将第一个复制到第二个,有什么办法可以做到这一点吗? About.addActionLi
我想在单击按钮 b 后检查选中了哪些复选框,但是我的复选框是在我单击按钮 b1 之后声明的 我的意思是,我应该全局声明 checkbox[] 吗?我该怎么做? b1.addActionListener
我有一个这样的方法,它被赋予一个 JButton 数组,并在它们被按下时返回它们的文本: public static String foo(JButton[] buttons) { for (
这个问题在这里已经有了答案: Detect enter press in JTextField (10 个答案) 关闭 7 年前。 我有一个具有三个功能的程序;读取文件、写入文件以及在文件中搜索特定
我有一个 actionlistener,当我单击一个按钮时触发,让我们称之为框,我有另一个 actionlistener 用于另一个按钮调用它重新启动。我想要做的是,当我单击一个框按钮时,除了在该 a
我正在想办法如何让一个类(class)听另一个类(class)的课。这就是我们的想法。 我有一个 MainFrame 类,它只是一个容器类,即 JFrame 容器,它采用 JPanel 类型的参数。基
实现 java.awt.event.ActionListener 接口(interface)的最佳方法是什么? 让您的类实现 ActionListener 并将其添加为 ActionListener:
我有一个框架(此处名为“MainApplication”),它主要有一个 JPanel 来根据上下文显示信息。 启动时,MainApplication 有一个空的 JPanel。 然后它创建一个“Lo
我发现已经有人提出了一些关于这个主题的问题,但我还没有找到答案。我正在编写一个代码,其中用户在 JTextField 中键入内容,然后单击按钮后,他的单词将被替换为与他的单词具有相同数量的字符数的星号
我将稍微解释一下我的代码: 我有一个带有项目列表的 JComboBox当按下 JButton“Select”时,它会注册 JComboBox 中最后一个选定项目的最后一个索引。 现在我需要访问主目录中
我收到错误,“AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于此代码的参数 (new ActionListener(){})”:
哪种方式实现 ActionListener 更正确?性能上有什么重大差异吗? 在类中实现 ActionListener: public class MainFrame implements Actio
在 Java 中,我对 JButton 数组使用 ActionListener。我希望 ActionListener 的较早部分将新的 ImageIcon 设置为 JButton,该更改将立即显示,然
这里有新海报;如果我违反了任何规则/搞砸了任何事情,请告诉我,我会删除该帖子。 我有一个 MainMenu JFrame,里面有一个 JPanel (mainMenu)。 mainMenu 有一个按钮
我在一个主类中有两个 Action Listener 内部类。每一个都对应于它自己的按钮。其中一个 Action 监听器被编码为生成数组列表。另一个只是将该数组列表写入文本字段。 我的问题是如何从其他
我是一名优秀的程序员,十分优秀!