java从子类调用awt框架方法

b91juud3  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(323)

这个问题是关于框架、java和处理的。
这个问题听起来很复杂,但实际上不是。我会尽量把这个控制在最低限度。我在一个迷宫游戏中创造了一个小球,让我的头脑围绕物理和渲染。到目前为止这是一次很好的经历,但我遇到了一些麻烦。
我决定的总体布局是在一个awt框架内包含papplets,并将框架关闭。这样做的原因是因为我被告知你一次只能有一个丘疹。 PAppletApplet 类中的一个渲染库。
我这里有三节课,包括主课

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时需要做的是,框架中的所有内容都将被删除,并加载一个highscores屏幕。高分和菜单差不多。没有必要发布代码,因为这里已经足够了。
第二类是作为一个框架,并持有丘疹
总之,有人知道如何从papplet调用frame方法,或者用其他方法删除所有papplet内容并加载另一个papplet吗?

qlzsbp2j

qlzsbp2j1#

为了回答如何从papplet?调用frame方法,我将您的代码片段修改为最小值。在这个修改过的版本中,当用户点击鼠标按钮时 System.out 被解雇了。
现在有两种方法可以访问 Frame 对象。但在此之前,我要说两点:
当你创造了一个丘疹像 new ExampleFrame(new Menu()); 把它加到你的 JFrame 这样地 add(app, BorderLayout.CENTER); 然后创建一个复杂的窗口/面板层次结构。
这样地:

javax.swing.JPanel
    javax.swing.JLayeredPane
        javax.swing.JRootPane
            test.ExampleFrame
``` `PApplet` 提供用于设置和访问框架对象的公共字段。令人惊奇的是它被称为 `frame` :). 你可以在打电话前设置 `app.init();` >>代码

**checkout 代码中的注解**

修改的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 !!");
}

}

修改菜单.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';
}

}

 checkout  `public void mousePressed()` 方法。
为了完整起见,我还包括main.java。

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

现在要回答,请删除所有papplets内容并加载另一个papplets
我还没试过呢。但是你可以加一个 `JPanel` 给你的 `JApplet` 做你所有的绘图,即创建子控件等,当你想重画然后打电话 `JPanel.removeAll()` . 根据javadoc:
从此容器中删除所有组件。此方法还通知布局管理器通过removelayoutcomponent方法从此容器的布局中删除组件。
在这通电话之后 `repaint` 上 `JPanel` . 试试看,可能有用:)。
tuwxkamq

tuwxkamq2#

当mousepressed==ebtn时需要做的是,框架中的所有内容都将被移除,并加载一个highscores屏幕
演示。在嵌套的 CardLayout 添加 ActionListener 而不是 MouseListener . 它对鼠标和键盘输入都有React。
在同一屏幕空间中包含多个gui元素还有许多其他方法。从我的头顶上, JTabbedPane , JSplitPane , JDesktopPane / JInternalFrame ,在一个 JDialog 或者 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);
    }
}

相关问题