2015-10-16 110 views
1

我正在写我的第一个Swing应用程序,并且在代码中堆叠标签时遇到了一些问题。如何在Swing中堆叠标签?

我见现在以下

First Swing app

我想“进入回购的名称和的名字”是上面“是回购搜索开放性问题的所有者。”所以窗户不那么宽。

这里是我的代码:

public class MainFrame extends JFrame { 

    private Boolean submitted = false; 

    public MainFrame(String title) { 
     super(title); 


     // Set layout manager 
     setLayout(new BorderLayout()); 

     // Create components 
     JPanel panOuter = new JPanel(new BorderLayout()); 
     JPanel panLeft = new JPanel(new BorderLayout()); 

     panLeft.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JPanel panRight = new JPanel(new BorderLayout()); 
     panRight.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JPanel panBottom = new JPanel(); 
     panBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JPanel panTop = new JPanel(); 
     panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JPanel panTopTop = new JPanel(); 
     panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     JPanel panTopBottom = new JPanel(); 
     panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     // Add components to content panel 
     panOuter.add(panLeft, BorderLayout.WEST); 
     panOuter.add(panRight, BorderLayout.EAST); 
     panOuter.add(panBottom, BorderLayout.SOUTH); 
     panOuter.add(panTop, BorderLayout.NORTH); 


     JLabel lblTop1 = new JLabel("Enter the name of the repo and the name of the\n", JLabel.CENTER); 
     JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER); 
     JLabel lblLeft = new JLabel("Repo", JLabel.CENTER); 
     JLabel lblRight = new JLabel("Owner", JLabel.CENTER); 

     JTextField txtLeft = new JTextField("Hello", 10); 
     JTextField txtRight = new JTextField("World", 10); 

     JButton btnBottom = new JButton("Submit!"); 

     panLeft.add(lblLeft, BorderLayout.NORTH); 
     panLeft.add(txtLeft, BorderLayout.CENTER); 

     panRight.add(lblRight, BorderLayout.NORTH); 
     panRight.add(txtRight, BorderLayout.CENTER); 

     panBottom.add(btnBottom); 
     panTopTop.add(lblTop1); 
     panTopBottom.add(lblTop2); 
     panTop.add(panTopTop, BorderLayout.NORTH); 
     panTop.add(panTopBottom, BorderLayout.SOUTH); 

     this.setContentPane(panOuter); 
     this.pack(); 

     btnBottom.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if(!submitted) 
        btnBottom.setText(txtLeft.getText()); 
       else 
        btnBottom.setText(txtRight.getText()); 
       submitted = !submitted; 
      } 

     }); 
    } 

} 

我试图让有标签的NORTHSOUTH分量的面板,但没有奏效。

有没有人有建议?

感谢, erip

+0

你应该尝试的GridLayout() –

+1

或者使用'GridBagLayout'如果你不”要求所有组件具有相同的宽度/高度 – MadProgrammer

回答

4

你可以尝试使用GridBagLayout ...

JPanel panTop = new JPanel(new GridBagLayout()); 
panTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
//JPanel panTopTop = new JPanel(); 
//panTopTop.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
//JPanel panTopBottom = new JPanel(); 
//panTopBottom.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

//... 

//panTopTop.add(lblTop1); 
//panTopBottom.add(lblTop2); 
GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridwidth = GridBagConstraints.REMAINDER; 
panTop.add(lblTop1, gbc); 
panTop.add(lblTop2, gbc); 
//panTop.add(panTopBottom, BorderLayout.SOUTH); 

GridBagLayout

有关详细信息,请参见

现在 How to Use GridBagLayout

,你可以得到真正的卑鄙,使用JTextArea ...

TextArea

JTextArea ta = new JTextArea(1, 20); 
ta.setText("Enter the name of the repo and the name of the owner of that repo to search for open issues."); 
ta.setWrapStyleWord(true); 
ta.setLineWrap(true); 
ta.setBorder(null); 
ta.setFont(UIManager.getFont("Label.font")); 
ta.setOpaque(false); 
ta.setFocusable(false); 
ta.setEditable(false); 

//JLabel lblTop1 = new JLabel("<html>Enter the name of the repo and the name of the owner of that repo to search for open issues", JLabel.CENTER); 
//JLabel lblTop2 = new JLabel("owner of that repo to search for open issues.\n", JLabel.CENTER); 
//... 
//panTopTop.add(lblTop1); 
//panTopBottom.add(lblTop2); 
GridBagConstraints gbc = new GridBagConstraints(); 
gbc.gridwidth = GridBagConstraints.REMAINDER; 
panTop.add(ta, gbc); 

甚至只使用Swing的HTML支持...

HTML layout

JLabel lblTop1 = new JLabel("<html><p align='center'>Enter the name of the repo and the name of the owner of that repo to search for open issues</p>", JLabel.CENTER); 
panOuter.add(lblTop1, BorderLayout.NORTH); 
+0

https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html这解释了这一切。 –

+0

@DavidPulse除了'JTextArea'和'HTML'技巧;) – MadProgrammer

+0

嘻嘻好秀 –