简体   繁体   中英

Java.Swing BorderLayout issue

So I'm trying to get the button on the bottom right and the text field taking up the bottom left but it keeps switching around for some reason. I think its borderLayout being stupid. I'm a noob at Java btw. Here's my code:

package textchat;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class window extends JFrame{
    public static void main(String[] args)
    {
        new window();
    }

    public window()
    {

        //Window Config
        //JFrame frame = new JFrame();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dm = tk.getScreenSize();
        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("CALI V1");
        this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        //this.setLayout(null);
        int Width = this.getWidth();


        //Panel(s)
        JPanel Panel = new JPanel();
        Panel.setLayout(new BorderLayout());
        JPanel PanelSouth = new JPanel();
        JPanel PanelEast = new JPanel();
        JPanel PanelWest = new JPanel();
        //button
        JButton btn = new JButton("SEND");

        //Text Area
        JTextArea txt = new JTextArea(100 , 100);
        txt.setText("TEXT IS HERE");

        //Text Field
        JTextField fld = new JTextField("Type Here",15);

        //Adding to the panel
        Panel.add(txt);
        PanelSouth.add(PanelEast, BorderLayout.EAST);
        PanelSouth.add(PanelWest, BorderLayout.WEST);
        PanelEast.add(btn);
        PanelWest.add(fld);
        //adding to frame
        this.add(Panel);
        this.add(PanelSouth , BorderLayout.SOUTH);



        this.setVisible(true);



    }


}

While using BorderLayout in such a way kind of works, you should not use it in such a way. Besides, your example has several issues. For instance, you did not start the application on the EDT (Event Dispatch Tread).

Here is a working example that creates your inteded layout. It uses the powerful GroupLayout manager.

package com.zetcode;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TextChatEx extends JFrame {

    public TextChatEx() {

        initUI();
    }

    private void initUI() {

        JTextArea area = new JTextArea(15, 15);
        JTextField field = new JTextField(15);
        JButton sendButton = new JButton("Send");

        createLayout(area, field, sendButton);

        setTitle("Text chat");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(arg[0])
                .addGroup(gl.createSequentialGroup()
                        .addComponent(arg[1])
                        .addComponent(arg[2]))
                );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(arg[0])
                .addGroup(gl.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(arg[1])
                        .addComponent(arg[2]))                
        );

        pack();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> {
            TextChatEx ex = new TextChatEx();
            ex.setVisible(true);
        });
    }
}

GroupLayout is a powerful layout manager; with this manager, you don't have to create a bunch of panels to create some basic layout.

    public TextChatEx() {

        initUI();
    }

The GUI creation is delegated to the initUI() method. Rather than placing all the code into the constructor, we use a specialized method.

    EventQueue.invokeLater(() -> {
        TextChatEx ex = new TextChatEx();
        ex.setVisible(true);
    });

Each Swing application should be placed on the EDT. Failing to do this can lead to hard to find bugs in the future.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM