저는 도박 게임을 시뮬레이트하기로되어있는 작은 프로젝트에서 일해 왔습니다. 불행히도 BoxLayout
으로 작업하면서 이상한 문제가 발생했습니다. 내가 아는 한 LayoutManager
은 보통 컴포넌트의 선호 크기를 존중합니다. 그러나 아래 코드에서 BoxLayout
은 그렇지 않습니다.BoxLayout은 JButton의 선호 크기를 기리는 것을 거부합니다
import java.awt.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Suit-Up");
frame.setContentPane(makeGUI());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900,450);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public static JPanel makeGUI()
{
JPanel main = new JPanel();
main.setMinimumSize(new Dimension(900,450));
main.setBackground(Color.red);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS));
infoPanel.setPreferredSize(new Dimension(900,60));
infoPanel.setBackground(Color.green);
main.add(infoPanel);
JPanel infoText = new JPanel();
infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS));
infoPanel.add(infoText);
JPanel moneyText = new JPanel();
moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS));
infoText.add(moneyText);
JPanel lastGameText = new JPanel();
lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS));
infoText.add(lastGameText);
JButton playAgain = new JButton("Play Again ($20)");
playAgain.setPreferredSize(new Dimension(200,60));
infoPanel.add(playAgain);
JButton finish = new JButton("End Session");
finish.setPreferredSize(new Dimension(200,60));
infoPanel.add(finish);
JPanel cardPanel = new JPanel();
cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS));
main.add(cardPanel);
return main;
}
}
모두 JButton
의 선호 크기를 지정에도 불구하고, 그들의 크기를 변경하지 마십시오 :
setMaximumSize()
과
setMinimumSize()
도 시도했지만 어느 것도 영향을 미치지 않았습니다.
나는 명백한 것을 간과하고 있습니까? 아니면이 한계가 BoxLayout
입니까?
항상 BoxLayout을 사용할 수 있지만 구성 요소를 중첩합니다 ... – mre
미안하지만 중첩 의미가 확실하지 않습니다. 나는 비교적 레이아웃 관리자가 처음이다. – Thrfoot
단추를 패널에 배치하고 단추 대신 컨테이너에 직접 패널을 추가하면 단추 대신 패널의 크기가 조정됩니다. – mre