我正在尝试创建一个简单的菜单(黑色背景),屏幕中间只有一个简单的按钮,显示“Play!”,然后当用户点击该按钮时,它将启动游戏。现在我不打算复制并粘贴我的所有游戏代码,但是我将给出扩展我正在使用的applet的起始部分。这是代码:
public class Game extends JApplet implements Runnable, KeyListener
{
AnimateSprite user;
AnimateSprite monster, troll;
Coins ten, twenty;
Thread thread;
Random r;
public void init()
{
r = new Random();
user = new AnimateSprite();
user.setImage(getImage(getCodeBase(), "player.gif"));
user.setLocation(0, 0);
monster = new AnimateSprite();
monster.setImage(getImage(getCodeBase(), "monster.gif"));
monster.setLocation(100, 100);
troll = new AnimateSprite();
troll.setImage(getImage(getCodeBase(), "monster.gif"));
troll.setLocation(350, 350);
setupCoins();
setFocusable(true);
addKeyListener(this);
thread = new Thread(this);
thread.start();
}
public void setupCoins()
{
ten = new Coins(10);
twenty = new Coins(20);
ten.setLocation(400, 350);
twenty.setLocation(450, 50);
ten.setImage(getImage(getCodeBase(), "coins.gif"));
twenty.setImage(getImage(getCodeBase(), "coins.gif"));
}
我要做的就是为用户使用/创建一个简单的界面,他们所做的就是点击“播放!”按钮然后它将启动游戏就像我将单独运行applet一样。我的问题是如何做到这一点的最佳方式。任何帮助是极大的赞赏!如果需要,我还会为此添加更多代码。
答案 0 :(得分:0)
试试这个
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Joption {
public static void main(String[] args) {
final JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)screenSize.getWidth();
int height = (int)screenSize.getHeight();
f.setLocation((int)(width/4.5), (int)(height/3.5));
JButton play=new JButton("play");
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print("your code/start meathod goes here");
f.dispose();
}});
f.add(play);
f.setVisible(true);
}
}
我不太了解你的代码,所以我不能做得更好,也许我可以让按钮看起来更好,但你明白了。
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Jap extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void init(){
// r = new Random();
// user = new AnimateSprite();
// user.setImage(getImage(getCodeBase(), "player.gif"));
// user.setLocation(0, 0);
// monster = new AnimateSprite();
// monster.setImage(getImage(getCodeBase(), "monster.gif"));
// monster.setLocation(100, 100);
// troll = new AnimateSprite();
//troll.setImage(getImage(getCodeBase(), "monster.gif"));
//troll.setLocation(350, 350);
//setupCoins();
setFocusable(true);
//addKeyListener(this);
//thread = new Thread(this);
final JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)screenSize.getWidth();
int height = (int)screenSize.getHeight();
f.setLocation((int)(width/4.5), (int)(height/3.5));
JButton play=new JButton("play");
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.print("your code/start meathod goes here");
//thread.start(); thread will have to be final to be called here or rename it to something that is final
f.dispose();
}});
f.add(play);
f.setVisible(true);
}
void Start(){
}
}
像这样。
答案 1 :(得分:0)
你可以在init方法中添加播放按钮,并让它调用一个方法,你不能使用start方法,只是启动线程。
例如:
public void init(){
//whatever you want here
t1 = new Thread(this);
play = new JButton("Play");
//setLocation, etc, etc.
add(play);
play.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(play);
repaint();
test();
}
});
}
public void test(){
t1.start();
}
public void run(){
//whatever you want here
}
所以这里发生的是你有一个按钮,当按下它时,将自己从屏幕上移开,调用paint
(通过重绘)来绘制屏幕,然后调用一个方法(不能是{{1 }})启动线程,这将启动你的动画。