分层许多JPanels并即时添加它们 [英] Layering many JPanels and adding them on the fly

查看:100
本文介绍了分层许多JPanels并即时添加它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于主题医院的模拟游戏,这是一个相当古老的游戏. 我在基础工作上取得了很多进展,但是现在我要介绍GUI元素,这是我之前从未做过的.我对Java还是很陌生. 我尝试创建的效果如下所示……

I'm developing a sim game based on Theme Hospital, which is quite an old game. I've made lots of progress on the underlying workings, however now I am coming to the GUI elements, which I haven't done alot of before. I am still rather new to java. The effect I am trying to create is like shown here...

http://www.tubechop.com/watch/18438

单击一个按钮,打开一个带有选项卡的面板以从不同的选项中进行选择,然后单击一个按钮来建立一个房间.相信对于标签"我可以使用卡片布局吗?对于房间的实际建筑,我非常满意.我现在遇到的主要问题是,单击按钮即可打开面板.

Click on a button, opens up a panel with tabs to select from different selections, and then click a button to build a room. I believe for the "tabs" I can use a card layout? For the actual building of rooms, I am pretty much sorted. The main problem I have right now, is getting the panel to open up on the click of a button.

目前,我的顶部有1个JFrame和2个JPanels,主游戏面板和带有几个按钮的控制面板.

At current, I have 1 JFrame and 2 JPanels ontop, the main game panel and the control panel with a few buttons.

任何人都可以向我展示一些有关如何进行此操作的简单示例吗?我知道它可能真的很简单,我敢打赌,你们中的一些人甚至可以将代码写在脑海中,但是我是Java的新手,到目前为止,他们已经学习了更多有关编程逻辑元素的知识,而不是如何构建代码.更复杂的多层GUI,就像游戏中需要的一样.

Can anyone show me some simple example of how I would do such a thing? I know its probably really simple, and I bet some of you could even write the code off the top of your head, but I am new to java, and have been taught more about the logical elements of programming so far rather than how to build a more complex multi layered GUI like required in a game.

我知道这是一个雄心勃勃的项目,但是我走了很长一段路,甚至使用A *实现了自定义路径查找,对此我感到很高兴(感谢您在StackOverflow上的人们!)

I know it's an ambitious project, but I have come a long way, and have even implemented custom path finding using A*, which I'm happy about (All thanks to you people here at StackOverflow!)

预先感谢您的帮助.

推荐答案

JDialogs可以运行,但是它们将在游戏窗口上弹出新的顶级窗口.您可以将主游戏的显示和控制面板实现为JDesktopPane(扩展了JLayeredPane)的背景,并可以弹出JInternalFrames.

JDialogs would work, but they're going to pop up new top level windows over your game window. You could implement your main game display and control panel as the background of a JDesktopPane(which extends JLayeredPane), and could make the pop ups JInternalFrames.

人为(但有效)的示例:

Contrived (but working) example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

public class DesktopTest extends JFrame {

private JDesktopPane desktop;
private JPanel background;
private JInternalFrame firstFrame;
private JInternalFrame secondFrame;

public DesktopTest() {
    super("DesktopTest");

    desktop = new JDesktopPane();
    setContentPane(desktop);

    background = new JPanel(new BorderLayout());
    JToolBar toolbar = new JToolBar();
    toolbar.add(new AbstractAction("1") {

        public void actionPerformed(ActionEvent actionEvent) {
            firstFrame.setVisible(true);
        }
    });

    toolbar.add(new AbstractAction("2") {

        public void actionPerformed(ActionEvent actionEvent) {
            secondFrame.setVisible(true);
        }
    });
    AddPanel addPanel = new AddPanel();
    background.add(addPanel, BorderLayout.CENTER);
    background.add(toolbar, BorderLayout.SOUTH);
    addComponentListener(new ComponentAdapter() {

        public void componentResized(ComponentEvent componentEvent) {
            background.setSize(desktop.getSize());
            background.revalidate();
            background.repaint();
        }

        public void componentShown(ComponentEvent componentEvent) {
            background.setSize(desktop.getSize());
            background.revalidate();
            background.repaint();
        }
    });
    desktop.add(background);

    firstFrame = new TermFrame("First Term", "Update First Term: ", addPanel) {

        protected int getValue() {
            return addPanel.getFirst();
        }

        protected void update(int value) {
            addPanel.setFirst(value);
        }
    };
    firstFrame.pack();
    firstFrame.setBounds(10, 10, 200, 150);
    desktop.add(firstFrame);

    secondFrame = new TermFrame("Second Term", "Update Second Term: ", addPanel){

        protected int getValue() {
            return addPanel.getSecond();
        }

        protected void update(int value) {
            addPanel.setSecond(value);
        }
    };
    secondFrame.pack();
    secondFrame.setBounds(200, 200, 200, 150);
    desktop.add(secondFrame);

}

public static void main(String[] args) {
    JFrame f = new DesktopTest();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

static class AddPanel extends JPanel {
    private JLabel first;
    private JLabel second;
    private JLabel result;

    public AddPanel() {
        first = new JLabel("0");
        second = new JLabel("0");
        result = new JLabel("0");

        Box vertical = Box.createVerticalBox();
        vertical.add(Box.createVerticalGlue());
        Box horizontal = Box.createHorizontalBox();
        horizontal.add(Box.createHorizontalGlue());
        horizontal.add(first);
        horizontal.add(new JLabel("+"));
        horizontal.add(second);
        horizontal.add(new JLabel("="));
        horizontal.add(result);
        horizontal.add(Box.createHorizontalGlue());
        vertical.add(horizontal);
        vertical.add(Box.createVerticalGlue());

        setLayout(new BorderLayout());
        add(vertical, BorderLayout.CENTER);
    }

    public void setFirst(int i) {
        first.setText(Integer.toString(i));
        updateResult();
    }

    public int getFirst() {
        return Integer.parseInt(first.getText());
    }

    public void setSecond(int j) {
        second.setText(Integer.toString(j));
        updateResult();
    }

    public int getSecond() {
        return Integer.parseInt(second.getText());
    }

    private void updateResult() {
        int i = Integer.parseInt(first.getText());
        int j = Integer.parseInt(second.getText());
        result.setText(Integer.toString(i + j));
        revalidate();
    }
}

static abstract class TermFrame extends JInternalFrame {

    protected AddPanel addPanel;
    private JFormattedTextField termField;

    public TermFrame(String title, String message, AddPanel addPanel) {
        super(title, true, true, true);
        this.addPanel = addPanel;
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(0);
        termField = new JFormattedTextField(format);
        termField.setColumns(3);
        termField.setValue(getValue());

        JPanel content = new JPanel(new FlowLayout());
        content.add(new JLabel(message));
        content.add(termField);
        JButton apply = new JButton("apply");
        apply.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionEvent) {
                Integer value = Integer.parseInt(termField.getText());
                update(value);
            }
        });
        content.add(apply);
        setContentPane(content);

        setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
    }

    protected abstract int getValue();

    protected abstract void update(int value);


}
}

这篇关于分层许多JPanels并即时添加它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆