我正在寫我的第一個Swing應用程序,並且在代碼中堆疊標籤時遇到了一些問題。如何在Swing中堆疊標籤?
我見現在以下
我想「進入回購的名稱和的名字」是上面「是回購搜索開放性問題的所有者。」所以窗戶不那麼寬。
這裏是我的代碼:
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;
}
});
}
}
我試圖讓有標籤的NORTH
和SOUTH
分量的面板,但沒有奏效。
有沒有人有建議?
感謝, erip
你應該嘗試的GridLayout() –
或者使用'GridBagLayout'如果你不」要求所有組件具有相同的寬度/高度 – MadProgrammer