質問

私はかなり古いゲームですテーマ病院、に基づいてシムゲームを開発しています。 しかし私は、今、私は私が前のたくさん行っていないGUI要素に来ています、根本的な仕組みの進捗状況をたくさん作りました。私はまだJavaへのかなり新しいです。 ここに示したように、私が作成しようとしています効果がある...

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

ボタンをクリックして、別の選択肢から選択してから、部屋を構築するために、ボタンをクリックし、タブ付きパネルを開きます。私は「タブ」のために私はカードレイアウトを使用することができると信じて?部屋の実際の建物のために、私はかなりソートされています。私が今持っている主な問題は、ボタンのクリックで開くためにパネルを取得されます。

現在では、私は岩下1のJFrameと2つのJPanel、ゲームのメインパネルといくつかのボタンとコントロールパネルを持っています。

誰も私に、私は、そのようなことをするだろうかのいくつかの簡単な例を示していることはできますか?私はそのおそらく本当に簡単知っている、と私はあなたのいくつかのも、あなたの頭の上からコードを書くことができますが、私は、Javaに新しいですし、これまでのところではなく構築する方法よりも、プログラミングの論理要素についての詳細を教えられてきた賭けますゲームに必要なような、より複雑な多層GUIます。

私はそれは野心的なプロジェクトだけど、私は長い道のりを歩んでいる、とさえ私はおよそ嬉しい*を使用して見つけるカスタムパスを実装している(すべてのおかげでここにStackOverflowであなたの人々に!)

あなたの助けのために事前にありがとうございます。

役に立ちましたか?

解決

を持たないJDialogは動作しますが、彼らはあなたのゲームのウィンドウの上に新しいトップレベルウィンドウをポップアップするつもりです。あなたは(のJLayeredPaneを拡張)JDesktopPaneの背景として、あなたのメインのゲーム画面とコントロールパネルを実装することができ、そしてポップのUPSのJInternalFrameを作ることができます。

不自然(しかし作業)例:

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のチュートリアルを読むことから始め示唆しますすべてのコンポーネントの例について。私は実際にプログラムのグラフィックス上の任意のアドバイスを提供することはできません。そのために、あなたは、特定の質問を開始する必要があります。しかし、パネルや部品を持つ建物のダイアログは、まっすぐである。

ボタン処理が非常に簡単です。あなたはクラスがActionListenerインターフェイスを実装持っている必要があり、それはあなたのボタンに登録する必要があります。あなたはここをかなり明確な例を見ることができます。

あなたが育てたいパネルの場合、それはあなたがトップレベルのSwingコンテナに組み込まれたルートペインの枠組みの中で見つけることができます探しているもののように見えます。 ここでチュートリアルのです。

ルート区画フレームワークは、トップレベルのコンポーネントのzオーダーのスタックの上に座っている明確なウィンドウガラスペインを含みます。あなたはまた、もう少し自分のニーズに合うかもしれない階層化区画を、持っています。それだけでなくのチュートリアルがあります。

scroll top