分层多JPanels并增加他们在飞行
题
我基于主题医院,这是一个很老的游戏开发SIM游戏。 我做了很多的底层运作进展,但我现在来到GUI元素,我还没有很多的做过。我还是比较新到Java。 我想创建的效果就像这里显示...
http://www.tubechop.com/watch/18438
点击一个按钮,打开了选项卡的面板从不同的选择来选择,然后点击一个按钮来建房。我相信,在“标签”我可以使用卡片布局?对于房的实际建设,我非常排序。主要的问题我现在所拥有的,是越来越面板开辟一个按钮的点击。
目前电流I具有1的JFrame和2个JPanels ontop的,主游戏面板和几个按钮控制面板。
谁能告诉我,我会怎么做这样的事了一些简单的例子吗?我知道它可能是很简单的,我敢打赌,你们中的一些甚至可以编写代码了你的头顶,但我是新来的Java,并已教了关于编程的逻辑元素到目前为止,而不是如何建立一个更复杂的多层GUI例如游戏中的必需的。
我知道这是一个雄心勃勃的项目,但我已经走过了漫长的道路,甚至已经实现的自定义路径中使用A *,这是我很高兴发现(这要归功于你的人在这里的StackOverflow!)
感谢您提前对您有所帮助。
解决方案
JDialogs会的工作,但他们会在你的游戏窗口,弹出新的顶级窗口。你可以实现你的游戏主显示屏和控制面板作为的JDesktopPane(延伸的JLayeredPane)的背景,可以使弹出窗口JInternalFrames。
做作(但工作)示例:
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);
}
}
其他提示
我现在的主要问题,是 让面板打开的 点击按钮。
您不用打开另一个面板。你可能会使用包含另一个小组一个JDialog。然后,您可以使用CardLayout或者你可以在对话框中使用JTabbedPane的。本设计选择由你。
我建议您通过阅读 Swing指南启动所有组件的例子。我真的不能提供该程序的图形任何建议。为此你需要开始问的具体问题。但是建立对话,与面板和部件是直线前进。