2009-07-11 8 views
1

私はテーマ病院に基づいたsimゲームを開発しています。これはかなり古いゲームです。 私は根本的な作業について多くの進歩を遂げましたが、私はこれまでにやっていないGUI要素に着きます。私はまだJavaの方が新しいです。 ここに示したように、私が作成しようとしています効果がある...多くのJPanelをレイヤリングして、それらを即座に追加する

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

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

現在のところ、JFrameは1つ、JPanelsは2つあります。メインゲームパネルとコントロールパネルにはボタンがいくつかあります。

誰かが私にそのようなことをする方法の簡単な例を表示できますか?私は、おそらく本当にシンプルなことを知っていますが、あなたの頭の上からコードを書くことさえできる人もいるかもしれませんが、私はjavaを初めて使い、ビルド方法ではなくプログラミングの論理要素についてもっと教えられていますゲームで必要とされるより複雑なマルチレイヤGUI

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

ご協力いただきありがとうございます。

+0

私は助けることはできませんが、幸運を!どのような素晴らしいゲームだったか。これは商用リリースですか? – JoshJordan

+0

ありがとうございます。私の友人、フリーウェアのオープンソースの方法はまったくありません。私は、OTTDのようなものとして気づくほど大きなプロジェクトになることを願っています。 OTTDが私に多くの時間をもたらしたのに対し、私が演じた最初のシムゲームはTheme Hospitalでした。私は、THと人との共演、思い切ったひねり、エキサイティングな新しいゲームプレイの思い出を共有したいと思います! :) – Relequestual

+0

あなたはこれのために実行しているサイトがありますか?私は、私が貢献することに興味があるかもしれないと言っている。 – JoshJordan

答えて

2

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); 


} 
} 
+0

これは私が探しているものを正確に探します。ありがとう。 誰でもこれに関するコメントがありますか?それは良いか悪い練習か、個人の好みにちょうど良いですか? – Relequestual

+0

こんにちは。私は私のプロジェクトにそれを実装するいくつかの問題を抱えています。 私は恐らく間違ったやり方をしたことがあるかもしれませんが、今は約2〜3時間微調整していますので、もう一度掘り下げる前に尋ねる時間を考えました...ここでは、 ... http://snipt.org/kpJ ありがとうございました – Relequestual

+0

私の悪い、私はmainPaneを2回定義しました。進捗! :)今いくつかの他の面白い問題を抱えていますが、うまくいけば私はそれを得るでしょう – Relequestual

0

私が今持っている主な問題は、 パネルはボタンの クリックで開くことになっています。

別のパネルを開くことはできません。おそらく別のパネルを含むJDialogを使用します。 CardLayoutを使用するか、ダイアログボックスでJTabbedPaneを使用できます。デザインの選択肢はあなた次第です。

すべてのコンポーネントの例については、Swing tutorialを読むことから始めてください。私は本当にプログラムのグラフィックスについてアドバイスを提供することはできません。そのためには、具体的な質問をする必要があります。しかし、パネルやコンポーネントを使ってダイアログを構築するのは簡単です。

0

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

あなたが探しているパネルは、最上位のSwingコンテナに組み込まれているルートペインフレームワーク内にあります。 Here is a tutorial

ルートペインのフレームワークには、トップレベルコンポーネントのzオーダースタックの上部にある透明なペインであるガラスペインが含まれています。レイヤードペインもありますが、これはあなたのニーズにもう少し適しています。 tutorial for that as wellがあります。

関連する問題