请参阅下面的SSCCE:
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.Box; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.UIManager; import javax.swing.border.EtchedBorder; @SuppressWarnings("serial") public class GlueTest extends JComponent { private JFrame frame; private JToolBar toolbar; private JLabel label_1; private JTextField textField_1; private JLabel label_2; private JTextField textField_2; public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { GlueTest window = new GlueTest(); window.frame.setVisible(true); window.frame.requestFocusInWindow(); } catch (Exception e) { e.printStackTrace(); } } }); } public GlueTest() { initialize(); } private void initialize() { frame = new JFrame("Test"); frame.setBounds(0,0,800,600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); toolbar = new JToolBar(); toolbar.setFloatable(false); toolbar.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); toolbar.setMaximumSize(new Dimension(frame.getWidth(), frame.getHeight())); frame.getContentPane().add(toolbar, BorderLayout.NORTH); Component horizontalGlue = Box.createHorizontalGlue(); toolbar.add(horizontalGlue); label_1 = new JLabel(" 1 "); toolbar.add(label_1); textField_1 = new JTextField(); textField_1.setColumns(9); textField_1.setMaximumSize(textField_1.getPreferredSize()); toolbar.add(textField_1); label_2 = new JLabel(" 2 "); toolbar.add(label_2); textField_2 = new JTextField(); textField_2.setColumns(9); textField_2.setMaximumSize(textField_2.getPreferredSize()); toolbar.add(textField_2); } }
注意我正在使用“系统”的外观和感觉。 在Windows 7中,它显示完美。 文本字段会自动调整为最大尺寸,左侧的水平粘贴会尽可能增大,从而迫使它们右alignment。
但是,在Linux(Ubuntu 12.04,如果它很重要),它结束了方式不同。 textfields滚动框架! 水平胶水本身太大。
我已经尝试了其他几种不同的方法来让它在两个版本中都能正常显示,没有运气。 实际上,在Linux下正确显示的唯一方法是删除文本字段的所有样式(最大宽度,列等),然后成功不滚动框架,但它是无用的,因为它只有几个像素的宽度。
作为一个旁注,我甚至没有机会尝试在Mac OS上,但我很好奇,结果如何。
问题(不是*** nix用户),你有这个代码相同的问题
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import javax.swing.Box; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.border.EtchedBorder; @SuppressWarnings("serial") public class GlueTest extends JComponent { private JFrame frame; private JToolBar toolbar; private JLabel label_1; private JTextField textField_1; private JLabel label_2; private JTextField textField_2; public GlueTest() { toolbar = new JToolBar(); toolbar.setFloatable(false); toolbar.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); toolbar.setAlignmentY(TOP_ALIGNMENT); toolbar.add(Box.createHorizontalGlue()); label_1 = new JLabel(" 1 "); toolbar.add(label_1); toolbar.add(Box.createRigidArea(new Dimension(10,1))); textField_1 = new JTextField(); textField_1.setColumns(9); textField_1.setMaximumSize(textField_1.getPreferredSize()); toolbar.add(textField_1); toolbar.add(Box.createHorizontalStrut(10)); label_2 = new JLabel(" 2 "); toolbar.add(label_2); toolbar.add(Box.createRigidArea(new Dimension(10,1))); textField_2 = new JTextField(); textField_2.setColumns(9); textField_2.setMaximumSize(textField_2.getPreferredSize()); toolbar.add(textField_2); toolbar.add(Box.createHorizontalStrut(10)); frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(toolbar, BorderLayout.NORTH); frame.add(new JTextArea(10, 40)); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { GlueTest window = new GlueTest(); } }); } }
将built-in BoxLayout切换到BorderLayout
import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.border.EtchedBorder; @SuppressWarnings("serial") public class GlueTest extends JComponent { private JFrame frame; private JToolBar toolbar; private JLabel label_1; private JTextField textField_1; private JLabel label_2; private JTextField textField_2; public GlueTest() { toolbar = new JToolBar(); toolbar.setFloatable(false); toolbar.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); toolbar.setLayout(new BorderLayout(10, 10)); JPanel panel = new JPanel(); panel.setOpaque(false); label_1 = new JLabel("1"); panel.add(label_1); textField_1 = new JTextField(); textField_1.setColumns(9); panel.add(textField_1); label_2 = new JLabel("2"); panel.add(label_2); textField_2 = new JTextField(); textField_2.setColumns(9); panel.add(textField_2); toolbar.add(panel, BorderLayout.EAST); frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(toolbar, BorderLayout.NORTH); frame.add(new JTextArea(10, 40)); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { GlueTest window = new GlueTest(); } }); } }
作为替代方案,考虑将JToolBar
布局更改为FlowLayout.RIGHT
。
import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.FlowLayout; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JToolBar; import javax.swing.border.EtchedBorder; public class Test extends JComponent { public Test() { JToolBar toolbar = new JToolBar(); toolbar.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null)); toolbar.setLayout(new FlowLayout(FlowLayout.RIGHT)); toolbar.add(new JLabel("1")); toolbar.add(new JTextField(9)); toolbar.add(new JLabel("2")); toolbar.add(new JTextField(9)); JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(toolbar, BorderLayout.NORTH); frame.add(new JTextArea(10, 40)); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { Test window = new Test(); } }); } }