2016-04-27 7 views
1

以下のコードでは、JFrameのボタンコンポーネントに位置とテキストを割り当てたいだけですが、常にそれらを画面の高さに割り当て、表示せずに左に押しつぶします私が割り当てたテキスト助けを気にしない?JComponentが位置やテキストを設定していない

import javax.swing.*; 
import java.awt.*; 

class Main 
{ 
    public static final String SIMULATOR_NAME = "Flocking Simulator 2K16"; //Simulator window name. 
    private static final String SPARROW_GRAPHIC = "Bird.png"; //Graphic image for sparrows. 
    public static final int MAXIMUM_WIDTH = 800; //Simulator window width. 
    public static final int MAXIMUM_HEIGHT = 600; //Simulator window height. 

    private static final int DEFAULT_BIRDS = 50; //Default number of birds on program launch. 
    private static final int MINIMUM_BIRDS = 5; //Minimum quantity of birds. 
    private static final int MAXIMUM_BIRDS = 100; //Maximum quantity of birds. 
    private static final double DEFAULT_VELOCITY = 0.25; //Move at 0.25 pixels per 1ms. 
    private static final int MOVEMENT_DELAY = 5; //Update movement every 5ms. 
    private static final int MOVEMENTS_UNTIL_NEXT_UPDATE = 5; //Update directions every 25ms. 

    private static JFrame frame; //Frame used from the JFrame class to draw graphics onto. 

    private static Flock sparrows; //The flock of sparrows used for the simulator. 

    private static JButton addBird; //The button object for adding a bird. 
    private static JButton removeBird; //The button object for adding a bird. 

    public static void main(String [ ] args) 
    { 
     Initialize(); 
     GameLoop(); 
    } 

    private static void Initialize() 
    { 
     frame = new JFrame(SIMULATOR_NAME + " - Number Of Birds: " + DEFAULT_BIRDS); 
     frame.setSize(MAXIMUM_WIDTH, MAXIMUM_HEIGHT); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout()); 
     LoadObjectGraphics(); 
     frame.setVisible(true); 
    } 

    private static void LoadObjectGraphics() 
    { 
     sparrows = new Flock(SPARROW_GRAPHIC, DEFAULT_BIRDS, new CartesianCoordinate(MAXIMUM_WIDTH, MAXIMUM_HEIGHT), DEFAULT_VELOCITY); 

     for (int i = 0; i < DEFAULT_BIRDS; i++) 
     { 
      frame.add(sparrows.GetBird(i).BirdGraphic()); 
     } 

     LoadUI(); 
    } 

    private static void LoadUI() 
    { 
     addBird = new JButton("+"); 
     addBird.setBounds(50, 50, 50, 50); 
     removeBird = new JButton("-"); 
     removeBird.setSize(48, 48); 
     removeBird.setBounds(50, 50, 50, 50); 
     frame.add(addBird); 
     frame.add(removeBird); 
    } 

    private static void UpdateUI() 
    { 
     frame.setTitle(SIMULATOR_NAME + " - Number Of Birds: " + sparrows.GetMaximumBirds()); 
    } 

    private static void GameLoop() 
    { 
     while (true) 
     { 
      //Change the directions of the birds 
      sparrows.UpdateDirections(); 

      //Move the birds before updating directions of the birds again 
      for (int i = 0; i < MOVEMENTS_UNTIL_NEXT_UPDATE; i++) 
      { 
       Utils.pause(MOVEMENT_DELAY); //Pause allowing program to not crash in an infinate loop 
       sparrows.ProcessMovement(MOVEMENT_DELAY); //Move the birds each interval 
       UpdateUI(); //Update the simulators UI 
      } 
     } 
    } 
} 

As you can see, the dimensions and location of the buttons are incorrect

+0

この[使用例](https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/uiswing/examples/layout)を使用できます。 /GridLayoutDemoProject/src/layout/GridLayoutDemo.java)をOracleから取得します。 GridLayout [here](https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html)の使い方をお読みください。あなたが作成したGridLayoutは役に立たない。あなたはグリッドのサイズを指定しませんでした。 – randominstanceOfLivingThing

答えて

0

のGridLayout(これを実行する最も簡単な方法)やたSpringLayoutを使用してください。

例:

GridLayout layout = new GridLayout(/*Number of rows*/ , /*Number of columns*/); 

はとのフレームでこのレイアウトを追加します。

myFrame.setlayout(layout); 

とのGridLayoutであなたのコンポーネントを追加します。

layout.add(myComponent); 
layout.add(mySecondComponent); 

等...

あり完全にあなたのためのGridLayoutのガイド:

How to use GridLayout

そしてたSpringLayoutのガイドは(たSpringLayoutを使用するのは難しいですが、試してください):

How to use SpringLayout

たSpringLayoutは、GridLayoutのよりも優れています!

関連する問題