gpt4 book ai didi

java - 从子类调用 awt Frame 方法

转载 作者:IT老高 更新时间:2023-10-28 20:52:07 24 4
gpt4 key购买 nike

这个问题是关于框架、Java 和 Processing 的。 .

这个问题听起来很复杂,但实际上并非如此。我会尽量把它保持在一个简单的最低限度。我正在迷宫游戏中创建一个小球,让我的头脑了解物理和渲染。到目前为止,这是一次很好的体验,但我遇到了一些障碍。
我决定的总体布局是在 AWT Frame 中包含 PApplets 并关闭 Frame。这样做的原因是因为有人告诉我,您一次只能拥有一个 Papplet 实例。

PAppletProcessing 中的 Applet 类,一个渲染库。

我这里有 3 节课,包括主课

public class Menu extends PApplet
{
//images and buttons
PImage background, playbtn1, playbtn2, hsbtn1, hsbtn2, abbtn1, abbtn2, exbtn1, exbtn2;
FBox pBtn, hBtn, eBtn;

FWorld menu;

//simple constructor
public Menu()
{

}

public void setup()
{
size(600, 400);
smooth();
Fisica.init(this);
menu = new FWorld();

//loading and placing images
background = loadImage("MenuAlt.jpg");
System.out.println(background);
playbtn1 = loadImage("play1.gif");
playbtn2 = loadImage("play2.gif");
hsbtn1 = loadImage("high1.gif");
hsbtn2 = loadImage("high2.gif");
exbtn1 = loadImage("exit1.gif");
exbtn2 = loadImage("exit2.gif");

//loading and placing buttons
pBtn = new FBox(120, 150);
pBtn.setPosition(135, 215);
pBtn.setDrawable(false);
hBtn = new FBox(120, 150);
hBtn.setPosition(295, 215);
hBtn.setDrawable(false);
eBtn = new FBox(120, 150);
eBtn.setPosition(455, 215);
eBtn.setDrawable(false);

//add item to world
menu.add(pBtn);
menu.add(hBtn);
menu.add(eBtn);
}

public void draw()
{
image(background, 0, 0);
image(playbtn1, 80, 140);
image(hsbtn1, 237, 135);
image(exbtn1, 400, 140);

mouseOver();
menu.draw();
}

//close this frame an open a new level, high score or exit
//depending on what the use clicks
public void mousePressed()
{
FBody pressed = menu.getBody(mouseX, mouseY);
if (pressed == pBtn)
{
System.out.println("play game");
this.getParent().getParent().getParent().getParent().setVisible(false);

ExampleFrame x = new ExampleFrame(new Level("level1.txt"));
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == hBtn)
{
System.out.println("high scores");
this.getParent().getParent().getParent().getParent().setVisible(false);

/* these are just for finding the parent
System.out.println(this.getName());
System.out.println(this.getParent().getName());
System.out.println(this.getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getName());
System.out.println(this.getParent().getParent().getParent().getParent().getName());
*/
ExampleFrame x = new ExampleFrame(new HighScores()); //for testing, you can change this to new menu()
x.setLocation(this.getParent().getParent().getParent().getParent().getLocation());
}
if (pressed == eBtn)
{
System.out.println("exit");
System.exit(0);
}
}

exampleFrame 类

public class ExampleFrame extends JFrame
{
PApplet app;

public ExampleFrame(PApplet emApp)
{
super("Ball Maze Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(200, 200);

app = emApp;
setSize(615,438);
setVisible(true);

setLayout(new BorderLayout());

add(app, BorderLayout.CENTER);
app.init();
}
}

主要

public class Main
{
public static void main(String[] args)
{
ExampleFrame x = new ExampleFrame(new Menu());
}
}

当 mousePressed == ebtn 时需要发生的事情是框架中的所有内容都将被删除并加载高分屏幕。高分几乎与菜单相同。不需要贴代码,这里就够了。

第二类是作为框架并持有 PApplet 的类

底线,有谁知道如何从 PApplet 调用 Frame 方法或以其他方式删除所有 PApplet 内容并加载另一个 PApplet?

最佳答案

What needs to happen when mousePressed == ebtn is all the stuff in the Frame will be removed and a Highscores Screen will be loaded

演示。嵌套 CardLayout 的下方添加 ActionListener而不是 MouseListener .它对鼠标键盘输入都有反应。

还有许多其他方法可以在同一屏幕空间中包含多个 GUI 元素。在我的脑海中,JTabbedPane , JSplitPane , JDesktopPane/JInternalFrame ,在 JDialog 中弹出高分或 JOptionPane ..

截图

Game view High Scores view

CardLayoutDemo.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CardLayoutDemo {

public static void main(String[] args) {

Runnable r = new Runnable () {
public void run() {
final JRadioButton game = new JRadioButton("Game", true);
JRadioButton highScores = new JRadioButton("High Scores");

ButtonGroup bg = new ButtonGroup();
bg.add( game );
bg.add( highScores );

JPanel buttons = new JPanel(new
FlowLayout(FlowLayout.CENTER, 5, 5));
buttons.add( game );
buttons.add( highScores );

JPanel gui = new JPanel(new BorderLayout(5,5));
gui.add(buttons, BorderLayout.SOUTH);

final CardLayout cl = new CardLayout();
final JPanel cards = new JPanel(cl);
gui.add(cards);
cards.add(new JLabel("Level 1"), "game");
cards.add(new JLabel("High Scores"), "scores");

ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if (game.isSelected()) {
cl.show(cards, "game");
} else {
cl.show(cards, "scores");
}
}
};
game.addActionListener(al);
highScores.addActionListener(al);

JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 从子类调用 awt Frame 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5665156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com