GUI Box layout Y AXIS not inline
GUI Box layout Y AXIS not inline
我正在设计这个 GUI,整个框架使用 BorderLayout,然后有 3 个面板,boggle Board 面板使用 GridLayout,Enter Words Found 面板(由 JTextArea、Time left JLabel 和 Shake Dice 组成JButton) 正在使用带 Y 轴的 BoxLayout,当前 Word 面板(由当前 Word JLabel、提交 Word JButton 和 Score JLabel 组成)正在使用 FlowLayout。
如您所见,JTextArea 并未沿 Y 轴与 Time Left JLabel 和 Shake Dice JButton 内联,我不知道为什么会这样。如果有人能阐明他们为什么不排队,那就太好了。
这是我的 Enter Words Found JPanel 代码:
// Adding words found JPanel
wordFoundPanel = new JPanel();
wordFoundPanel.setLayout(new BoxLayout(wordFoundPanel, BoxLayout.Y_AXIS));
wordFoundPanel.setBorder(new TitledBorder("Enter Words Found"));
wordArea = new JTextArea();
wordArea.setLineWrap(true);
wordArea.setWrapStyleWord(rootPaneCheckingEnabled);
// Adding JScrollPane to the wordArea
scroll = new JScrollPane(wordArea);
scroll.setMinimumSize(new Dimension(200, 180));
scroll.setPreferredSize(new Dimension(200, 180));
scroll.setMaximumSize(new Dimension(200, 180));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
wordFoundPanel.add(scroll);
// Adding Clock Label
clockLabel = new JLabel("3:00", CENTER);
clockLabel.setMinimumSize(new Dimension(200, 100));
clockLabel.setPreferredSize(new Dimension(200, 100));
clockLabel.setMaximumSize(new Dimension(200, 100));
clockLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
clockLabel.setBorder(new TitledBorder("Time Left"));
wordFoundPanel.add(clockLabel);
// Adding Shake Dice button
shakeDiceButton = new JButton("Shake Dice");
shakeDiceButton.setMinimumSize(new Dimension(200, 100));
shakeDiceButton.setPreferredSize(new Dimension(200, 100));
shakeDiceButton.setMaximumSize(new Dimension(200, 100));
wordFoundPanel.add(shakeDiceButton);
frame.add(wordFoundPanel, BorderLayout.EAST);
在How to Use BoxLayout的修复对齐问题部分,我们可以阅读:
In general, all the components controlled by a top-to-bottom BoxLayout
object should have the same X alignment
所以只需在您的三个组件上调用例如 setAlignmentX(Component.RIGHT_ALIGNMENT);
,它就会修复这个奇怪的显示。
我正在设计这个 GUI,整个框架使用 BorderLayout,然后有 3 个面板,boggle Board 面板使用 GridLayout,Enter Words Found 面板(由 JTextArea、Time left JLabel 和 Shake Dice 组成JButton) 正在使用带 Y 轴的 BoxLayout,当前 Word 面板(由当前 Word JLabel、提交 Word JButton 和 Score JLabel 组成)正在使用 FlowLayout。
如您所见,JTextArea 并未沿 Y 轴与 Time Left JLabel 和 Shake Dice JButton 内联,我不知道为什么会这样。如果有人能阐明他们为什么不排队,那就太好了。
这是我的 Enter Words Found JPanel 代码:
// Adding words found JPanel
wordFoundPanel = new JPanel();
wordFoundPanel.setLayout(new BoxLayout(wordFoundPanel, BoxLayout.Y_AXIS));
wordFoundPanel.setBorder(new TitledBorder("Enter Words Found"));
wordArea = new JTextArea();
wordArea.setLineWrap(true);
wordArea.setWrapStyleWord(rootPaneCheckingEnabled);
// Adding JScrollPane to the wordArea
scroll = new JScrollPane(wordArea);
scroll.setMinimumSize(new Dimension(200, 180));
scroll.setPreferredSize(new Dimension(200, 180));
scroll.setMaximumSize(new Dimension(200, 180));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
wordFoundPanel.add(scroll);
// Adding Clock Label
clockLabel = new JLabel("3:00", CENTER);
clockLabel.setMinimumSize(new Dimension(200, 100));
clockLabel.setPreferredSize(new Dimension(200, 100));
clockLabel.setMaximumSize(new Dimension(200, 100));
clockLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
clockLabel.setBorder(new TitledBorder("Time Left"));
wordFoundPanel.add(clockLabel);
// Adding Shake Dice button
shakeDiceButton = new JButton("Shake Dice");
shakeDiceButton.setMinimumSize(new Dimension(200, 100));
shakeDiceButton.setPreferredSize(new Dimension(200, 100));
shakeDiceButton.setMaximumSize(new Dimension(200, 100));
wordFoundPanel.add(shakeDiceButton);
frame.add(wordFoundPanel, BorderLayout.EAST);
在How to Use BoxLayout的修复对齐问题部分,我们可以阅读:
In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment
所以只需在您的三个组件上调用例如 setAlignmentX(Component.RIGHT_ALIGNMENT);
,它就会修复这个奇怪的显示。