简体   繁体   English

Java.Swing BorderLayout问题

[英]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. 我认为它的borderLayout是愚蠢的。 I'm a noob at Java btw. 我是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. 当以这种方式使用BorderLayout时,您不应以这种方式使用它。 Besides, your example has several issues. 此外,您的示例还有几个问题。 For instance, you did not start the application on the EDT (Event Dispatch Tread). 例如,您没有在EDT(事件调度胎面)上启动应用程序。

Here is a working example that creates your inteded layout. 这是一个创建示例布局的工作示例。 It uses the powerful GroupLayout manager. 它使用了功能强大的GroupLayout管理器。

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; GroupLayout是功能强大的布局管理器; 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. GUI创建委托给initUI()方法。 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. 每个Swing应用程序都应放在EDT上。 Failing to do this can lead to hard to find bugs in the future. 否则,将来可能很难发现错误。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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