将jpanel定位在底部

时间:2014-04-28 12:36:44

标签: java swing jpanel layout-manager

我想在JTextArea(白色区域)的底部放置包含发送按钮和文本字段(现在正在使用flowlayout)的JPanel。 我怎样才能做到这一点?

public GUI()
{
    mainWindow = new JFrame("Chat GUI");
    lowerPanel = new JPanel(new FlowLayout());
    usersPanel = new JPanel(new GridLayout(GRIDLAYOUT_ROWS, GRIDLAYOUT_COLS));
    users = new JList(data);
    usersPanel.add(users);

    sendButton = new JButton("Send");           
    textField = new JTextField(TEXTFIELD_WIDTH);
    textArea = new JTextArea(TEXTAREA_HEIGHT, TEXTAREA_WIDTH);
    textArea.setEditable(false);
}

private void addButtonListener(JButton b) {
    b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
}

public void createGUI() {

    addButtonListener(sendButton);

    lowerPanel.add(sendButton);
    lowerPanel.add(textField);

    mainWindow.add(users);
    mainWindow.add(textArea);
    mainWindow.add(lowerPanel);

    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.setVisible(true);
    mainWindow.setLayout(new FlowLayout());
    mainWindow.pack();
}

enter image description here

1 个答案:

答案 0 :(得分:1)

你将有布局:

this.add(buttonPanel,BorderLayout.SOUTH);

请参阅this answer