【问题标题】:JPanel FlowLayout Force Component WrapJPanel FlowLayout 强制组件包装
【发布时间】:2014-01-10 23:37:48
【问题描述】:

我有一个JPanel,默认使用FlowLayout 管理器。我喜欢 FlowLayout 文档样式的优势,我一次添加一个组件并自动换行,但希望组件强制选择新行。

如果我使用BoxLayout,我可以插入一种component return key 并强制组件从新行开始。我需要关于我的决定以及哪种方法更好的指导。

我有一个 JLabelJTextField 在一行上,并希望将 JTextArea 包裹在下面的 JScrollPane 中。

【问题讨论】:

    标签: jpanel jcomponent flowlayout boxlayout


    【解决方案1】:
    • 结合使用FlowLayout BorderLayout。嵌套布局以获得所需的结果是个好主意。
    • JLabelJTextField 将合并为一个 JPanelFlowLayout
    • 然后另一个带有BorderLayoutJPanel 将在NORTH 位置保持前一个面板,在CENTER 位置保持JTextAreaJScrollPane

      JPanel topPanel = new JPanel();
      JLabel label = new JLabel("Text Field Label");
      JTextField jtf = new JTextField(20);
      topPanel.add(label);
      topPanel.add(jtf);
      
      JPanel bothPanel = new JPanel(new BorderLayout());
      JTextArea jta = new JTextArea(20, 40);
      bothPanel.add(topPanel, BorderLayout.NORTH);
      bothPanel.add(new JScrollPane(jta));
      

    看看Laying Out Components Within a Container

    import java.awt.BorderLayout;
    import java.awt.Color;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FlowBorderDemo {
    
        public FlowBorderDemo() {
            JPanel topPanel = new JPanel();
            JLabel label = new JLabel("Text Field Label");
            label.setForeground(Color.white);
            JTextField jtf = new JTextField(20);
            topPanel.add(label);
            topPanel.add(jtf);
            topPanel.setBackground(Color.black);
    
    
            JPanel bothPanel = new JPanel(new BorderLayout());
            JTextArea jta = new JTextArea(20, 40);
            JScrollPane scrollPane = new JScrollPane(jta);
            scrollPane.setBorder(BorderFactory.createMatteBorder(3, 0, 0, 0, Color.GRAY));
            bothPanel.add(topPanel, BorderLayout.NORTH);
            bothPanel.add(scrollPane);
            bothPanel.setBorder(BorderFactory.createMatteBorder(3, 8, 3, 8, Color.GRAY));
    
            JLabel copyLabel = new JLabel("<html>&copy;2014 peeskillet</html>");
            copyLabel.setBackground(Color.LIGHT_GRAY);
            copyLabel.setHorizontalAlignment(JLabel.CENTER);
            bothPanel.add(copyLabel, BorderLayout.PAGE_END);
    
    
            JFrame frame = new JFrame();
            frame.add(bothPanel);
            frame.pack();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager
                                .getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException
                            | IllegalAccessException
                            | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    new FlowBorderDemo();
                }
            });
        }
    }
    

    【讨论】:

    • 谢谢@peeskillet。我从来没有考虑过仅仅增加面板的数量并使用布局管理器。但是,我将如何强制将组件包装在同一个布局管理器中?
    • @Mushy 你到底想wrap什么?
    • 我想保留在FlowLayout 中,并将JScrollPane 包裹在JLabelJTextField 下方,而无需使用额外的面板和布局管理器。
    • @Mushy 您可以嵌套许多面板和布局,以获得所需的结果。包装可能不会一直给你想要的结果。例如,如果您想在带有文本字段的面板下方添加一些组件,您可以使用BoxLayout 创建另一个面板,并将文本字段面板和另一个面板添加到BoxLayout,然后将该BoxLayout 面板放在@987654345 @ 上面代码中的位置。只需继续嵌套面板即可获得所需的结果。
    猜你喜欢
    • 1970-01-01
    • 2015-08-26
    • 2016-04-19
    • 2015-04-11
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    相关资源
    最近更新 更多