我正在尝试为我正在上课的游戏制作一个介绍页面。我设法在JFrames中显示图像,但我不知道如何使按钮工作。下面是代码:
package nba_shootout_vol_two;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
public class Runner{
public static void main(String[] args) {
JFrame f=new JFrame("NBA Shootout Vol.2");
try{
f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img/title.jpg")))));
} catch(IOException e) {
System.out.println("Image doesn't exitst");
}
f.setResizable(false);
f.setSize(1280,720);
f.setVisible(true);
}
}
所以会弹出这个:
如何使按钮可以点击并且能够实际执行操作?
答案 0 :(得分:-1)
在我看来,你正在重新发明按钮,我认为这是一个好主意。那么,如何检查它们是否被点击:
示例EventHandler:
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
import java.awt.event.*;
public class EventHandler implements MouseListener {
public JFrame pane;
public int mouse_x;
public int mouse_y;
public int[] mouse=new int[5];
@Override
public void mousePressed(MouseEvent m) {
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=1;
case (MouseEvent.BUTTON2) :
mouse[3]=1;
case (MouseEvent.BUTTON3) :
mouse[4]=1;
}
}
@Override
public void mouseClicked(MouseEvent m) {
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=2;
case (MouseEvent.BUTTON2) :
mouse[3]=2;
case (MouseEvent.BUTTON3) :
mouse[4]=2;
}
}
@Override
public void mouseReleased(MouseEvent m) {
switch (m.getButton()) {
case (MouseEvent.BUTTON1) :
mouse[2]=3;
case (MouseEvent.BUTTON2) :
mouse[3]=3;
case (MouseEvent.BUTTON3) :
mouse[4]=3;
}
}
@Override
public void mouseEntered(MouseEvent m) {
}
@Override
public void mouseExited(MouseEvent m) {
}
public void getMouse() {
Component[] rel;
rel = pane.getComponents();
mouse_x=mouse[0]=MouseInfo.getPointerInfo().getLocation().x-rel[0].getLocationOnScreen().x;
mouse_y=mouse[1]=MouseInfo.getPointerInfo().getLocation().y-rel[0].getLocationOnScreen().y;
}
public void updateMouse() {
for (int i=0; i < mouse.length; i++) {
mouse[i]=0;
}
}
public void work() {
getMouse();
pane.requestFocus();
}
public void reset() {
updateMouse();
}
public EventHandler(Frame plane) {
this.pane=plane;
pane.requestFocus();
pane.addMouseListener(this);
}
}
使用它如下:
EventHandler events=new EventHandler(f);
带循环和rect_collide的游戏:
package nba_shootout_vol_two;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Button;
public class Runner{
public static EventHandler events;
public static boolean rect_collide(int bx, int by, int bw, int bh) {
return (events.mouse_x > bx && events.mouse_x < bx+bw && events.mouse_y > by && events.mouse_y < by+bh);
}
public static void main(String[] args) {
JFrame f=new JFrame("NBA Shootout Vol.2");
try{
f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("img/title.jpg")))));
} catch(IOException e) {
System.out.println("Image doesn't exitst");
}
f.setResizable(false);
f.setSize(1280,720);
f.setVisible(true);
events=new EventHandler(f);
while (true) { //Game loop
events.work();
if (events.mouse[2]) { //Left mouse button down
if (rect_collide(0,0,20,20)) { //Collision with button at x=0 y=0 w=20 h=20
//Do your stuff
}
}
f.repaint(); //Update the display since you are going to have changements after pressing the button
events.reset();
}
}
}
希望它对你有所帮助。如果您有任何疑问,请随时发表评论。