2014-01-10 45 views
0

我有一个JPanel,默认使用FlowLayout管理器。我喜欢文档样式FlowLayout的优点,在该文档中我使用自动包装一次添加一个组件,但希望组件强制选择新的一行。JPanel FlowLayout Force Component Wrap

我读了如果我使用了BoxLayout我可以插入一种component return key并强制组件在新行上启动。我需要关于我的决定的指导,哪一个是更好的方法。

我在一条线上有一个JLabelJTextField,并且想要将一个JTextArea包装在下面的JScrollPane内。

回答

2
  • 使用的FlowLayoutBorderLayout的组合。嵌套布局以获得您想要的结果是一个不错的主意。
  • JLabelJTextField会去一个JPanelFlowLayout
  • 然后又JPanelBorderLayout将保持前面板的NORTH位置,JTextAreaJScrollPaneCENTER位置。

    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

enter image description here

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(); 
      } 
     }); 
    } 
} 
+0

谢谢@peeskillet。我从来没有考虑过将面板数量增加到布局管理器。但是,我将如何强制在同一个布局管理器中封装组件? – Mushy

+0

@穆什你到底想要什么_wrap_? –

+0

我想留在'FlowLayout'中,并且在'JLabel'和'JTextField'下面包装'JScrollPane',而不用额外的面板和布局管理器。 – Mushy