2013-10-31 5 views
1
import java.awt.Color; 
import java.awt.Dimension; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Ships { 

    public static JPanel init(JPanel radioPanel){ 
     radioPanel.add(addShips(2)); 
     radioPanel.add(addShips(3)); 
     radioPanel.add(addShips(4)); 
     radioPanel.add(addShips(5)); 

     return radioPanel; 
    } 

    public static JButton addShips(int size){ 
     JButton but = new JButton(); 
     but.setPreferredSize(new Dimension((40*size),40)); 
     but.setBackground(Color.BLACK); 
     return but; 
    } 

    public static void main(String[] args){ 
     JFrame frame = new JFrame(); 
     frame.setVisible(true); 
     JPanel radioPanel = new JPanel(); 
     radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); \\line 4 
     init(radioPanel); 
     frame.getContentPane().add(radioPanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
    } 

} 

단추가 한 줄로 표시되어 있습니다. BoxLayout.Y_AXIS에 따라 다른 단추를 차례로 배치해야합니다. // 4 행을 제거하면 FlowLayout에 따라 올바르게 작성됩니다.Y 축에 임의의 크기의 버튼을 만드는 방법

답변

2

이 시도 : 구성 요소의

import java.awt.Color; 
import java.awt.Dimension; 

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class Ships { 

    public static JPanel init(JPanel radioPanel){ 
     radioPanel.add(addShips(2)); 
     radioPanel.add(addShips(3)); 
     radioPanel.add(addShips(4)); 
     radioPanel.add(addShips(5)); 

     return radioPanel; 
    } 

    public static JButton addShips(int size){ 
     JButton but = new JButton(); 
     Dimension d = new Dimension((40*size),40); 
     but.setPreferredSize(d); 
     but.setMinimumSize(d); 
     but.setMaximumSize(d); 
     but.setBackground(Color.BLACK); 
     return but; 
    } 

    public static void main(String[] args){ 
     JFrame frame = new JFrame(); 
     JPanel radioPanel = new JPanel(); 
     radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); //line 4 
     init(radioPanel); 
     frame.getContentPane().add(radioPanel); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

당신이 JFrame의 구성 요소를 포장 할 때, 레이아웃 매니저가 선호 존중하지 않을 수 있습니다 크기가 너무 최소 및 최대 크기를 설정하려고합니다.

세로 레이아웃 (y 축)에서 BoxLayout은 모든 구성 요소를 가장 넓은 구성 요소로 넓게 만듭니다. 모든 버튼에는 텍스트 나 아이콘이 없으므로 버튼이 기본 크기로 축소되며 모두 동일한 너비를 갖습니다. 따라서 최대 및 최소 크기를 사용하여 특정 크기의 상자 레이아웃을 지정하십시오.

2

LayoutManager를 GridBagLayout으로 변경했으며 정상적으로 작동합니다. 그것은 당신에게 적합한가요? :

public class Ships { 

public static JPanel init(JPanel radioPanel){ 
    GridBagConstraints c = new GridBagConstraints(); 
    c.anchor = GridBagConstraints.WEST; 
    radioPanel.add(addShips(2),c); 
    c.gridy = 1; 
    radioPanel.add(addShips(6),c); 
    c.gridy = 2; 
    radioPanel.add(addShips(4),c); 
    c.gridy = 3; 
    radioPanel.add(addShips(5),c); 

    return radioPanel; 
} 

public static JButton addShips(int size){ 
    JButton but = new JButton(); 
    but.setPreferredSize(new Dimension((40*size),40)); 
    but.setBackground(Color.BLACK); 
    return but; 
} 

public static void main(String[] args){ 
    JFrame frame = new JFrame(); 
    frame.setVisible(true); 
    JPanel radioPanel = new JPanel(); 
    radioPanel.setLayout(new GridBagLayout()); 
    init(radioPanel); 
    frame.getContentPane().add(radioPanel); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
} 

} 

편집 : 가로에서 세로로 정렬합니다. 결과 :

enter image description here

+0

그게 내가 원하는 걸! 나는 내 질문을 조금 바꿨다. – Mercenary

+0

tou는 BoxLayout을 사용해야합니까? – alex2410

관련 문제