- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个射击游戏(FPS - 第一人称射击游戏)并且我正在使用 OpenGl (jogl 1.0)与 JFrame 。
我想向 JFrame 添加一个 ActionListener :
public class Main extends JDialog {
private static ActionListener action;
private static JFrame framePhaseOne;
private static JFrame framePhaseTwo;
...
...
action = new ActionListener() // this is for PHASE 2
{
public void actionPerformed(ActionEvent ae)
{
if (userPoints.getGamePhase()) // if F2 was clicked
{
framePhaseTwo = new JFrame(WorldName2);
framePhaseTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framePhaseTwo.setLocationByPlatform(true);
framePhaseTwo.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
Renderer_PhaseTwo myCanvas2 = new Renderer_PhaseTwo(userPoints);
final Animator animator2 = new Animator(myCanvas2);
framePhaseTwo.add(myCanvas2);
framePhaseTwo.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseTwo.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
@Override
public void run()
{
animator2.stop();
System.exit(0);
}
}.start();
}
});
framePhaseTwo.setVisible(true);
animator2.start();
myCanvas2.requestFocus();
myCanvas2.setFocusable(true);
}
}
};
我想将 action
添加到 framePhaseOne
中,如何在不使用 JPanel 和按钮的情况下做到这一点?
如果需要的话,这是主类的完整代码:
/**
* This is the main class that runs the First Person Java app
* using the OpenGL mechanism , with JOGL 1.0
* @author X2
*
*/
public class Main extends JDialog
{
// when true permission granted for starting the game
private static boolean start = false;
private static final long serialVersionUID = 1L;
protected static TimerThread timerThread;
static JStatusBar statusBar = new JStatusBar();
private static JFrame framePhaseOne;
private static JFrame framePhaseTwo;
private static ActionListener action;
/**
* framePhaseOne properties
*/
private static final int FRAME_LOCATION_X = 300;
private static final int FRAME_LOCATION_Y = 50;
private static final int FRAME_SIZE_X = 850; // animator's target frames per second
private static final int FRAME_SIZE_Y = 700; // animator's target frames per second
/**
* start button properties
*/
private static final int BUTTON_LOCATION_X = (FRAME_SIZE_X / 2) - 100;
private static final int BUTTON_LOCATION_Y = (FRAME_SIZE_Y / 2) - 50;
private static final int BUTTON_SIZE_X = 140; // animator's target frames per second
private static final int BUTTON_SIZE_Y = 50; // animator's target frames per second
/**
* timer & game title & arrow picture
*/
private static final String WorldName1 = "FPS 2013 CG Project - Phase 1";
private static final String WorldName2 = "FPS 2013 CG Project - Phase 2";
private static final String HARD_TARGET = "src/res/target.jpg";
private static final String runningOut = "Time is running out - you have : ";
static int interval;
static Timer timer1;
static JLabel changingLabel1 = null;
static Points userPoints = new Points();
/**
* Timer properties
*/
private static Timer timer;
private static int count = 60;
/**
* ActionListener for timer
*/
private static ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (start)
{
count--;
if (count == 0)
timer.stop();
changingLabel1.setText(runningOut + count + " seconds" + " , and your points are: "
+ userPoints.getPoints());
}
}
};
public static void exitProcedure() {
System.out.println();
timerThread.setRunning(false);
System.exit(0);
}
/**
* Clock timer1
* @author X2
*
*/
public static class TimerThread extends Thread
{
protected boolean isRunning;
protected JLabel dateLabel;
protected JLabel timeLabel;
protected SimpleDateFormat dateFormat =
new SimpleDateFormat("EEE, d MMM yyyy");
protected SimpleDateFormat timeFormat =
new SimpleDateFormat("h:mm a");
public TimerThread(JLabel dateLabel, JLabel timeLabel) {
this.dateLabel = dateLabel;
this.timeLabel = timeLabel;
this.isRunning = true;
}
@Override
public void run() {
while (isRunning) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Calendar currentCalendar = Calendar.getInstance();
Date currentTime = currentCalendar.getTime();
dateLabel.setText(dateFormat.format(currentTime));
timeLabel.setText(timeFormat.format(currentTime));
}
});
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
}
}
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
}
/**
*
* @param args
*/
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
framePhaseOne = new JFrame(WorldName1);
action = new ActionListener() // this is for PHASE 2
{
public void actionPerformed(ActionEvent ae)
{
if (userPoints.getGamePhase()) // if F2 was clicked
{
framePhaseTwo = new JFrame(WorldName2);
framePhaseTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
framePhaseTwo.setLocationByPlatform(true);
framePhaseTwo.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
Renderer_PhaseTwo myCanvas2 = new Renderer_PhaseTwo(userPoints);
final Animator animator2 = new Animator(myCanvas2);
framePhaseTwo.add(myCanvas2);
framePhaseTwo.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseTwo.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
@Override
public void run()
{
animator2.stop();
System.exit(0);
}
}.start();
}
});
framePhaseTwo.setVisible(true);
animator2.start();
myCanvas2.requestFocus();
myCanvas2.setFocusable(true);
}
}
};
final Container contentPane = framePhaseOne.getContentPane();
contentPane.setLayout(new BorderLayout());
/**
* the timer of the count-down
*/
timer = new Timer(1000, timerAction);
timer.start();
changingLabel1 = new JLabel("Game is offline , hit Start to continue !");
statusBar.setLeftComponent(changingLabel1);
final JLabel dateLabel = new JLabel();
dateLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(dateLabel);
final JLabel timeLabel = new JLabel();
timeLabel.setHorizontalAlignment(JLabel.CENTER);
statusBar.addRightComponent(timeLabel);
contentPane.add(statusBar, BorderLayout.SOUTH);
/**
* start button
*/
final JButton startButton = new JButton("Start the game !");
// startButton.setBounds(300, 50,140, 50 );
startButton.setBounds(BUTTON_LOCATION_X
, BUTTON_LOCATION_Y,
BUTTON_SIZE_X,
BUTTON_SIZE_Y );
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
start = true; // start the game
userPoints.startGame();
contentPane.remove(startButton);
contentPane.revalidate();
contentPane.repaint();
}
});
contentPane.add(startButton);
framePhaseOne.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
framePhaseOne.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
timerThread = new TimerThread(dateLabel, timeLabel);
timerThread.start();
Renderer_PhaseOne myCanvas = new Renderer_PhaseOne(userPoints);
final Animator animator = new Animator(myCanvas);
Toolkit t = Toolkit.getDefaultToolkit();
BufferedImage originalImage = null;
try
{
originalImage = ImageIO.read(new File(HARD_TARGET));
}
catch (Exception e1) {e1.printStackTrace();}
Cursor newCursor = t.createCustomCursor(originalImage, new Point(0, 0), "none");
framePhaseOne.setCursor(newCursor);
framePhaseOne.setLocation(FRAME_LOCATION_X, FRAME_LOCATION_Y);
framePhaseOne.add(myCanvas);
framePhaseOne.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);
framePhaseOne.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
new Thread()
{
@Override
public void run()
{
animator.stop();
System.exit(0);
}
}.start();
}
});
framePhaseOne.setVisible(true);
animator.start();
myCanvas.requestFocus();
myCanvas.setFocusable(true);
}
});
}
}
问候
最佳答案
您无法将 ActionListener
添加到 JFrame
,它的功能不像按钮,因此没有操作监听器。
您正在寻找的是 MouseListener 。它检测鼠标点击。您可能还对 MouseMotionListener 感兴趣它为您提供有关鼠标移动的信息。
这是一个例子:
framePhaseOne.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
System.out.println("Mouse was clicked on my frame!");
}
};
MouseAdapter是一个实现MouseListener的抽象类。它使您不必实现 MouseListener 接口(interface)所需的所有方法。
编辑:
在下面的评论中与您交谈后,您正在寻找的是 KeyListener 。再次,我推荐 KeyAdapter 的原因与 MouseAdapter 相同。这是一个例子:
framePhaseOne.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_F2){
//close frame one.
}
}
});
如果您希望它也关闭您的第一帧,也可以使用framePhaseTwo 执行此操作。
framePhaseTwo.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_F2){
//close frame one
}
}
});
请注意,框架需要焦点才能接收关键事件。
关于java - 如何在不使用按钮和面板的情况下将 ActionListener 添加到 JFrame ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18165800/
在另一个 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 监听器被编码为生成数组列表。另一个只是将该数组列表写入文本字段。 我的问题是如何从其他
我是一名优秀的程序员,十分优秀!