2016-08-31 3 views
0

私はペアゲームを作成しようとしています。私は2つのJPanelを作成しました。最初はゲームマップ(100個のボタン)と2番目の統計情報です。問題は、私は左の写真のようにタイマーとカウンターの配置を持っているが、私は右の絵のように(私はペイントで編集)したいと思っている。私はどんなレイアウトを使うべきですか?私は多くの解決策を試しましたが、何も私のために働きました。また、私が設定されている場合はnullマネージャJPanelのは、あなたがお互いに「についての」あなたのオブジェクトを配置することを可能にするさまざまなレイアウトマネージャの全体の様々ながありJpanelのコンポーネントを設定する

public class PairGame extends JFrame implements ActionListener{ 

JLabel counterLabel, timerLabel; 
JMenuBar menuBar; 
JMenu file, help; 
JMenuItem fileNew, fileExit, helpAbout; 
JPanel gamePanel, statisticsPanel; 
JToggleButton buttons[]; 

ArrayList <Integer> values; 

int temp=0, counter=0; 

void createMenuBar() 
{   
    file = new JMenu("File"); 
     fileNew = new JMenuItem("New"); 
     fileNew.addActionListener(this); 
     fileNew.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); 
     fileExit = new JMenuItem("Exit"); 
     fileExit.addActionListener(this); 
     fileExit.setAccelerator(KeyStroke.getKeyStroke("ctrl Q")); 
     file.add(fileNew); 
     file.add(fileExit); 
     file.setMnemonic('f'); 

    help = new JMenu("Help"); 
     helpAbout = new JMenuItem("About"); 
     helpAbout.addActionListener(this); 
     helpAbout.setAccelerator(KeyStroke.getKeyStroke("ctrl H")); 
     help.add(helpAbout); 
     help.setMnemonic('h'); 

    menuBar = new JMenuBar(); 
     menuBar.add(file); 
     menuBar.add(help); 
     setJMenuBar(menuBar); 
} 

public void createGameMap() 
{  
    gamePanel = new JPanel(new GridLayout(10,10)); 

    values = new ArrayList<Integer>(); 
    for(int i=1; i<=50; i++) 
     values.add(i); 
    for(int i=51; i<=100; i++) 
     values.add(i-50); 

    Collections.shuffle(values);  

    buttons = new JToggleButton[100]; 
    for(int i=0; i<100; i++) 
    { 
     buttons[i] = new JToggleButton(""+(i+1)); 
     buttons[i].setName(String.valueOf(values.toArray()[i])); 
     buttons[i].addActionListener(this); 
     gamePanel.add(buttons[i]); 
    } 

    gamePanel.setBounds(0,0,550,400); 
    add(gamePanel); 
} 

public void createStatisticsPanel() 
{ 
    statisticsPanel = new JPanel(); 
    counterLabel = new JLabel("Counter: "+counter); 
    timerLabel = new JLabel("Timer: "); 
    statisticsPanel.add(counterLabel); 
    statisticsPanel.add(timerLabel); 
    statisticsPanel.setBounds(0,410,540,400); 
    add(statisticsPanel); 
} 


PairGame() 
{ 
    setTitle("Pair Game"); 
    setSize(565,600); 
    setLocation(400,100);   
    setLayout(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);  

    createMenuBar(); 
    createGameMap(); 
    createStatisticsPanel(); 

    setVisible(true); 
} 

public static void main(String[] args) 
{ 
    new PairGame(); 
} 

IMAGE

+1

'' BoxLayout'(https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html)、['GridLayout'](https://)を使用できる' statisticsPanel'については、 docs.oracle.com/javase/tutorial/uiswing/layout/grid.html)、['GridBagLayout'](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)または[ 'BorderLayout'](https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html)を参照してください。この場合私の個人的な好みによって決定される。 –

+0

投稿することを検討する[MCVE] – c0der

+0

http://stackoverflow.com/help/someone-answersを参照してください – c0der

答えて

2

を消えます。

オラクルのBoxLayoutの2番目の例を見てみると、そのパネルには「2つの要素」が表示されます。上側の要素は左揃えのラベルです。おそらくあなたが探しているものでしょう。あるいは、右のGridLayoutの最初の例でもラベルが整列しています。

+0

私はstatisticsPanelのBoxLayoutを設定できません。なぜなら、 "BoxLayoutは共有できません"というコンパイルエラーが発生するからです。また、GridLayoutは私に何が欲しいのかわからない。 私はちょうど左の境界から約30で、上部JPanelから30(ゲームマップで)であるいくつかの位置(今は2:カウンタとタイマー、そしてもう少し後で)を持つ1列が必要です。私はGridLayoutを1列と2行で使用しようとしましたが、動作しません.20列を設定すると動作します(それから私は1LOLを持ちます)。また私は私のJLabelsを設定することはできませんsetAligmentはちょうど動作しません(何も変更) – Kurczaksky

+0

a。 'BoxLayout'を正しく設定する必要があります。 b。異なるレイアウトマネージャを異なるパネルに適用することができます。私が掲示したコードを見てください。 – c0der

2

私の答えの目的は、GhostCatが提案したレイアウトマネージャを使用して解決策を示すことだけでなく、容易に助けて助けを得るためにMCVEのアイデアを示しました。

の説明のためのコメントを参照してください:

//post imports 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JToggleButton; 

public class PairGame extends JFrame { 

    //removed fields to keep code as short as possible 
    JLabel counterLabel, timerLabel; 
    JPanel gamePanel, statisticsPanel; 

    //menu bar removed to keep the code as short as possible 
    //void createMenuBar() 

    public void createGameMap() 
    { 
     //simplify as much as possible 
     gamePanel = new JPanel(new GridLayout(1,1)); 

     JToggleButton button = new JToggleButton("1"); 
     gamePanel.add(button); 

     //use layout managers is preferred practice over setting bounds 
     add(gamePanel, BorderLayout.CENTER); 
    } 

    public void createStatisticsPanel() 
    { 
     counterLabel = new JLabel("Counter: "); 
     timerLabel = new JLabel("Timer: "); 

     statisticsPanel = new JPanel(); 

     //use layout managers is preferred practice over setting bounds 
     //the layout you want can be achieved with various managers 
     //such as BorderLayout, BoxLayout, Gridlayout 

     //Implementation of BorderLayout 
     statisticsPanel.setLayout(new BorderLayout()); 
     statisticsPanel.add(counterLabel, BorderLayout.NORTH); 
     statisticsPanel.add(timerLabel, BorderLayout.SOUTH); 

     //to test box layout REPLACE the 3 statements above with 
     //Implementation of BoxLayout 
     //BoxLayout boxLayout = new BoxLayout(statisticsPanel,BoxLayout.Y_AXIS); 
     //statisticsPanel.setLayout(boxLayout); 
     //statisticsPanel.add(counterLabel); 
     //statisticsPanel.add(timerLabel); 

     add(statisticsPanel, BorderLayout.SOUTH); 
    } 


    PairGame() 
    { 
     setTitle("Pair Game"); 
     setSize(565,600); 
     setLocation(400,100); 

     //use layout managers is preferred practice over setting bounds 
     getContentPane().setLayout(new BorderLayout()); 

     setDefaultCloseOperation(EXIT_ON_CLOSE); 

     createGameMap(); 
     createStatisticsPanel(); 

     setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     new PairGame(); 
    } 
} 

は、必要に応じて明確化を求めることを躊躇しないでください。

関連する問題