2017-12-08 9 views
0

私のJButtonとJTextAreaを、コードの真ん中に並べて整列しようとしています。私はいくつかのチュートリアルに続き、この時点まで来ました。私のプログラムを実行すると、テキスト領域とボタンはまだ上に並んでいます。助けて!ボタン、ラベル、およびテキストフィールドを整列するJava Swing

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 


public class Main extends JFrame implements ActionListener{ 

JPanel panel = new JPanel(); 
JButton button = new JButton("Confirm!"); 
JTextArea text = new JTextArea(1, 20); 
public Main() { 
    super("Battleship!"); 
    setLayout(new FlowLayout()); 
    button.addActionListener(this); 
    setSize(600, 500); 
    setResizable(true); 
    button.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    text.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    panel.add(text, BorderLayout.SOUTH); 
    panel.add(button, BorderLayout.SOUTH); 
    add(panel); 

    setVisible(true); 
} 
public static void main(String[] args) { 
    new Main(); 
} 
@Override 
public void actionPerformed(ActionEvent e) { 
    button.setText(text.getText()); 
} 

} 
+2

'BorderLayout'でしか管理できません利用可能な5つのポジションのいずれかに1つのコンポーネント。より良い解決策は、 'GridBagLayout'のようなより柔軟なレイアウトマネージャを使うことです。 – MadProgrammer

+0

もう一つの素晴らしい選択肢はJavaFXライブラリです。あなたの問題を修正するのではなく、JavaFXライブラリが大好きです。 @ JacobB。 –

+0

。 * "しかし、私はJavaFXライブラリが大好きです" *これは明確にトピック外のAPIを推薦するための言い訳ではありません。 –

答えて

1

コードコメントの注記を参照してください。

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

public class MainLayout extends JFrame { 

    // A panel defaults to flow layout. Use the default 
    JPanel panel = new JPanel(); 
    JButton button = new JButton("Confirm!"); 
    JTextArea text = new JTextArea(1, 20); 

    public MainLayout() { 
     super("Battleship!"); 
     //setLayout(new FlowLayout()); // use the default border layout 
     setSize(600, 500); // should be packed after components added. 
     //setResizable(true); // this is the default 
     /* Don't set layouts on things like buttons or text areas 
     This is only useful for containers to which we add other 
     components, and it is rarely, if ever, useful to add components 
     to these types of GUI elements. */ 
     //button.setLayout(new FlowLayout(FlowLayout.CENTER)); 
     // text.setLayout(new FlowLayout(FlowLayout.CENTER)); 

     /* No need for layout constraints when adding these to 
     a flow layout */ 
     //panel.add(text, BorderLayout.SOUTH); 
     panel.add(text); 
     //panel.add(button, BorderLayout.SOUTH); 
     panel.add(button); 

     // NOW we need the constraint! 
     add(panel, BorderLayout.PAGE_END); 

     setVisible(true); 
    } 

    public static void main(String[] args) { 
     /* Swing/AWT GUIs should be started on the EDT. 
     Left as an exercise for the reader */ 
     new MainLayout(); 
    } 
} 
+0

問題を解決するのに役立ちましたら[回答を受け入れてください](http://meta.stackexchange.com/a/5235/155831)してください。 –

0
パネルを持っているので、中心 にコンポーネントを配置し、フレームがデフォルトで持っているだろう「FlowLayoutのセンター」はデフォルトで中央に配置するためのボタンやテキストへのフローレイアウトを設定する必要はありません

「BorderLayoutの」
ボタンとテキストエリアを下部に配置するには、既に両方のパネルをパネルに追加してから、パネルをBorderLayout.SOUTHとしてフレームに追加する必要があります。 それ以下の変更されたコードは、必要があります

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
public class Main extends JFrame implements ActionListener 
{ 
     JPanel panel = new JPanel(); 
     JButton button = new JButton("Confirm!"); 
     JTextArea text = new JTextArea(1, 20); 
     public Main() 
     { 
      super("Battleship!"); 
      button.addActionListener(this); 
      setSize(600, 500); 
      setResizable(true); 
      panel.add(text); 
      panel.add(button); 
      add(panel, BorderLayout.SOUTH); 
      setVisible(true); 
     } 
     public static void main(String[] args) 
     { 
      new Main(); 
     } 
     @Override public void actionPerformed(ActionEvent e) 
     { 
      button.setText(text.getText()); 
     } 
} 
関連する問題