从子类中调用awt Frame方法

| 这个问题是关于框架,Java和处理的。 这个问题听起来很复杂,但实际上并非如此。我将尝试将其保持在最低限度。我正在迷宫游戏中创建一个小球,以使我了解物理和渲染。到目前为止,这是一个很好的经验,但是我碰到了一些砖墙。 我决定的总体布局是在AWT框架中包含PApplets,并关闭框架。这样做的原因是因为有人告诉我您一次只能使用一个Papplet实例。
PApplet
是Processing(渲染库)中的
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方法,或以另一种方式删除所有PApplets内容并加载另一个PApplet的方法?     
已邀请:
为了回答如何从PApplet调用Frame方法?,我已将您的代码段修改为最低限度。在此修改的版本中,当用户单击鼠标按钮时,会触发“ѭ5”。 现在,您可以通过两种方式访问​​
Frame
对象。但是在此之前,我先说两点: 当您创建像“ 7”这样的PApplet并将其像这样“ 9”添加到您的“ 8”中时,就会创建一个复杂的窗口/面板层次结构。 像这样:
javax.swing.JPanel
    javax.swing.JLayeredPane
        javax.swing.JRootPane
            test.ExampleFrame
PApplet
提供用于设置和访问框架对象的公共字段。令人惊讶的是它被称为
frame
:)。您可以在致电
app.init();
之前进行设置 >>代码 **检出代码中的注释** 修改后的ExampleFrame.java
import java.awt.BorderLayout;    
import javax.swing.JFrame;    
import processing.core.PApplet;

public class ExampleFrame extends JFrame
{
    private static final long serialVersionUID = 4792534036194728580L;
    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);

        // Setting my frame object
        app.frame = this;       
        app.init();
    }

    // Sample Method
    public void sampleMethod(String msg)
    {
        System.out.println(\"I think \'\"+ msg +\"\' called me !!\");
    }
}
修改后的Menu.java
import java.awt.Container;

import processing.core.PApplet;
import processing.core.PImage;

public class Menu extends PApplet
{
    private static final long serialVersionUID = -6557167654705489372L;

    PImage background;
    static String tab = \"\";

    //simple constructor
    public Menu()
    {

    }

    public void setup()
    {
        size(600, 400);
        smooth();

        background = loadImage(\"C:/temp/background.jpg\");
    }

    public void draw()
    {
        image(background, 0, 0);
    }

    public void mousePressed()
    {
        Container p = getParent();
        tab = \"\";

        // FIRST WAY OF ACCESSING PARENT FRAME
        while(p != null)
        {
            //printParentTree(p);
            if(p instanceof ExampleFrame)
            {
                ExampleFrame myframe = (ExampleFrame)p;
                myframe.sampleMethod(\"First Way\");
                break;
            }
            p = p.getParent();
        }

        // SECOND WAY OF ACCESSING PARENT FRAME     
        if(frame != null && (frame instanceof ExampleFrame))
        {
            ExampleFrame myframe = (ExampleFrame)p;
            myframe.sampleMethod(\"Second Way\");
        }
    }

    void printParentTree(Container p) 
    {
        System.out.println(tab+p.getClass().getName());
        tab +=\'\\t\';
    }
}
签出
public void mousePressed()
方法。 为了完整起见,我还包括Main.java。
public class Main {
    public static void main(String[] args){
        new ExampleFrame(new Menu());
    }
}
现在回答删除所有PApplet内容并在其中加载另一个PApplet 好吧,我还没有测试过。但是您可以在
JApplet
上添加
JPanel
并在此上进行所有绘制,即创建子控件等。当需要重绘时,请调用
JPanel.removeAll()
。根据javadoc:   从中删除所有组件   容器。此方法还通知   布局管理器删除   该容器的组件   通过removeLayoutComponent进行布局   方法。 之后,在
JPanel
上呼叫
repaint
。试试看,它可能会起作用:)。     
  当mousePressed == ebtn会发生什么情况,那么框架中的所有内容都将被删除,并且将加载高分屏幕 演示。嵌套
CardLayout
的下面添加了
ActionListener
而不是
MouseListener
。它对鼠标和键盘输入均作出反应。 在同一个屏幕空间中,还有多种其他方式可以包含多个GUI元素。在我头顶上,
JTabbedPane
JSplitPane
JDesktopPane
/
JInternalFrame
,以the30ѭ或
JOptionPane
的高分出现。 屏幕截图    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);
    }
}
    

要回复问题请先登录注册