2011-10-19 10 views
0

私はSwingの手書きを学び、Sun Docのチュートリアルを使用しています。私はGridLayoutを使用しています。そして、最初に、すべてのボタンを使用可能なスペースに塗りつぶしました。私の2回目の行進では、上部にいくつかのコントロールを保持するために2番目のグリッドを実装しました。私はこれが本当に簡単なものだと確信しています。しかし、年齢のために読んで、演奏した後、私の最初の試みで行ったように、ボタンでいっぱいに2番目の下部ペインを得ることができません。ヘルプpls? invokeメソッドパックによってJavaスイングの2番目のJFrameレイアウトは、スペースを埋める?

package layout; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Game extends JFrame { 
    GridLayout gameLayout = new GridLayout(6,6); 
    JLabel score = new JLabel("Score:"); 

    public Game(String name) { 
     super(name); 
     setResizable(true); 
    } 

    public void addComponentsToPane(final Container pane) { 
     final JPanel mainGameWindow = new JPanel(); 
     mainGameWindow.setLayout(gameLayout); 
     JPanel controls = new JPanel(); 
     controls.setLayout(new GridLayout(2,4)); 

     //Add buttons to experiment with Grid Layout 
     mainGameWindow.add(new JButton("sqrOne1")); 
     mainGameWindow.add(new JButton("sqrOne2")); 
     mainGameWindow.add(new JButton("sqrOne3")); 
     mainGameWindow.add(new JButton("sqrOne4")); 
     mainGameWindow.add(new JButton("sqrOne5")); 
     mainGameWindow.add(new JButton("sqrOne6")); 
     mainGameWindow.add(new JButton("sqrTwo1")); 
     mainGameWindow.add(new JButton("sqrTwo2")); 
     mainGameWindow.add(new JButton("sqrTwo3")); 
     mainGameWindow.add(new JButton("sqrTwo4")); 
     mainGameWindow.add(new JButton("sqrTwo5")); 
     mainGameWindow.add(new JButton("sqrTwo6")); 
     mainGameWindow.add(new JButton("sqrThree1")); 
     mainGameWindow.add(new JButton("sqrThree2")); 
     mainGameWindow.add(new JButton("sqrThree3")); 
     mainGameWindow.add(new JButton("sqrThree4")); 
     mainGameWindow.add(new JButton("sqrThree5")); 
     mainGameWindow.add(new JButton("sqrThree6")); 
     mainGameWindow.add(new JButton("sqrFour1")); 
     mainGameWindow.add(new JButton("sqrFour2")); 
     mainGameWindow.add(new JButton("sqrFour3")); 
     mainGameWindow.add(new JButton("sqrFour4")); 
     mainGameWindow.add(new JButton("sqrFour5")); 
     mainGameWindow.add(new JButton("sqrFour6"));  
     mainGameWindow.add(new JButton("sqrFive1")); 
     mainGameWindow.add(new JButton("sqrFive2")); 
     mainGameWindow.add(new JButton("sqrFive3")); 
     mainGameWindow.add(new JButton("sqrFive4")); 
     mainGameWindow.add(new JButton("sqrFive5")); 
     mainGameWindow.add(new JButton("sqrFive6"));   
     mainGameWindow.add(new JButton("sqrSix1")); 
     mainGameWindow.add(new JButton("sqrSix2")); 
     mainGameWindow.add(new JButton("sqrSix3")); 
     mainGameWindow.add(new JButton("sqrSix4")); 
     mainGameWindow.add(new JButton("sqrSix5")); 
     mainGameWindow.add(new JButton("sqrSix6")); 

     //Add controls to set up horizontal and vertical gaps 
     controls.add(new Label("Match pairs of Promient Tech Heads!")); 
     controls.add(score); 
     controls.add(new JButton("Solve")); 
     controls.add(new JButton("Scrabble")); 

     //Process the Apply gaps button press  
     gameLayout.layoutContainer(mainGameWindow); 

     pane.add(controls, BorderLayout.NORTH); 
     pane.add(new JSeparator(), BorderLayout.CENTER);   
     pane.add(mainGameWindow, BorderLayout.SOUTH); 
    } 

    private void setLabelText(String text) 
    { 
     score.setText(text); 
     score.paintImmediately(score.getVisibleRect()); 
    } 


    private static void createAndShowGUI() { 
     //Create and set up the window. 
     Game frame = new Game("Game"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //Set up the content pane. 
     frame.addComponentsToPane(frame.getContentPane()); 
     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
     frame.setSize(600,600); 
     frame.setMinimumSize(new Dimension(400, 400));    
    } 

    public static void main(String[] args) { 
     /* Use an appropriate Look and Feel */ 
     try { 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 
     /* Turn off metal's use of bold fonts */ 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     //Schedule a job for the event dispatch thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 

答えて

1

する必要があり、ボタンのようにする必要があり

JButton btnOne1 = new JButton("btnOne1"); 
btnOne1.setPreferredSize(new Dimension(100, 100)); 

とは、

mainGameWindow.add(btnOne1); 

そして、これは、彼らがしているウィンドウを埋めることができます。

3

、あなたの質問、唯一のコードを読んで、このコードを実行するためにあなたが

GridBagLayoutDemo frame = new GridBagLayoutDemo("GridLayoutDemo"); 

Game frame = new Game("GridLayoutDemo"); 

にEDIT

1を変更する必要はありません) ();あなたは正しくJComponentsのサイズを設定します。

2)setVisible(true)に正しく設定します。

しかし

その後、setSize()作成SOUTH area上のギャップ、正しいに近い

のコードは、私は将来の読者のために問題を見つけたと思う

private static void createAndShowGUI() { 
    Game frame = new Game("Game"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.addComponentsToPane(frame.getContentPane()); 
    frame.setMinimumSize(new Dimension(400, 400)); 
    frame.pack(); 
    frame.setVisible(true); 
} 
+0

おかげで、今でソート。 –

+0

あなたは正しい答えを(現在)マークするつもりですか? –

+0

ああ、申し訳ありません、私はちょうど命名エラーがソートされたことを意味しました。 –