2015-10-16 43 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

嘻嘻好秀 –