2017-12-08 5 views
0

내 JButton 및 JTextArea를 내 코드의 아래쪽 중간에 나란히 정렬하려고합니다. 나는 약간의 튜토리얼을 따라이 시점으로왔다. 프로그램을 실행하면 텍스트 영역과 버튼이 여전히 상단에 정렬됩니다. 도움!버튼, 레이블 및 텍스트 필드를 정렬하는 자바 스윙

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 가지 위치 중 하나에서 단일 구성 요소. 더 나은 해결책은'GridBagLayout'와 같이보다 유연한 레이아웃 관리자를 사용하는 것입니다. – MadProgrammer

+0

또 다른 훌륭한 옵션은 JavaFX 라이브러리입니다. 당신의 문제를 해결하지 못했지만 JavaFX 라이브러리를 좋아합니다. –

+0

@ JacobB. * "하지만 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()); 
     } 
} 
관련 문제