简体   繁体   English

JPanel / JFrame自动调整大小。 JAVA

[英]JPanel/JFrame Automatically resizing. JAVA

I so have made a GUI for a Chat that im currently working on. 因此,我为当前正在使用的聊天程序制作了一个GUI。 Its aaaaalmost done but the only thing is the automatically resizing when dragging the window. 它几乎全部完成,但是唯一的事情就是拖动窗口时自动调整大小。 I can't find out why its happend and this was my last chance. 我不知道为什么会发生,这是我最后的机会。 So I really need help from you guys! 所以我真的需要你们的帮助! Im kinda out of idea 我有点想法不对

The code is here: 代码在这里:

ClientGUI(String host, int port) {

    super("Chat Client");
    defaultPort = port;
    defaultHost = host;



    // The CenterPanel which is the chat room
    MessageText = new JTextArea("Welcome to the Chat room\n");
    JPanel centerPanel = new JPanel();
    MessageText.setWrapStyleWord(true);
    MessageText.setLineWrap(true);

    centerPanel.setLayout(null);
    JScrollPane scrollPane = new JScrollPane(MessageText);
    scrollPane.setBounds(0, 0, 584, 486);
    getContentPane().add(scrollPane);
    MessageText.setEditable(false);
    getContentPane().add(centerPanel, BorderLayout.CENTER);
    WriteMessage = new JTextField("Write your username here!");
    WriteMessage.setBounds(0, 492, 584, 35);
    centerPanel.add(WriteMessage);
    WriteMessage.setColumns(234);
    WriteMessage.setBackground(Color.WHITE);

    // the 3 buttons
    Login = new JButton("Login");
    Login.setBounds(594, 338, 125, 35);
    centerPanel.add(Login);
    Logout = new JButton("Logout");
    Logout.setBounds(594, 469, 125, 35);
    centerPanel.add(Logout);
    Logout.addActionListener(this);
    Logout.setEnabled(false);       // you have to login before being able to logout
    Online = new JButton("Online");
    Online.setBounds(594, 403, 125, 35);
    centerPanel.add(Online);
    Online.addActionListener(this);
    Online.setEnabled(false);       // you have to login before being able to Who is in
    JLabel PortNumberText = new JLabel("Port Number:  ");
    PortNumberText.setBounds(594, 83, 144, 20);
    centerPanel.add(PortNumberText);
    PortNumber = new JTextField("" + port);
    PortNumber.setBounds(594, 114, 129, 20);
    centerPanel.add(PortNumber);
    PortNumber.setHorizontalAlignment(SwingConstants.RIGHT);

    JLabel ServerText = new JLabel("Server Address:  ");
    ServerText.setBounds(594, 21, 240, 20);
    centerPanel.add(ServerText);
    // the two JTextField with default value for server address and port number
    ServerAddress = new JTextField(host);
    ServerAddress.setBounds(594, 52, 129, 20);
    centerPanel.add(ServerAddress);
    Login.addActionListener(this);
    WriteMessage.requestFocus();

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(760, 570);
    setVisible(true);




}

http://i.gyazo.com/8d751c832f07980f884bb408d91d65d5.gif

A picture what I mean 图片我的意思

Don't use a null layout. 不要使用空布局。 Don't use setBounds(). 不要使用setBounds()。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。

Resizing of components can only be done when you use a layout manager. 仅当使用布局管理器时才能调整组件的大小。 Read the section from the Swing tutorial on Layout Manager for more information and examples. 阅读有关布局管理器的Swing教程中的部分,以获取更多信息和示例。 You can always use multiple panels each with a different layout manager to get your desired results. 您始终可以将多个面板与不同的布局管理器一起使用,以获得所需的结果。

Also follow Java naming conventions. 还要遵循Java命名约定。 Variable names should NOT start with an upper case character. 变量名称不应以大写字母开头。 Half the time your names are correct and half the time they are not. 您的名字正确的时间有一半是正确的,一半是错误的。 Be consistent! 始终如一!

You are using a Null-Layout (absolute positioning), and this means you have to take care of everything related to layout yourself. 您正在使用Null-Layout (绝对定位),这意味着您必须自己处理与布局有关的所有事情。 Better use a LayoutManager . 最好使用LayoutManager

Alright so I just made a border layout all from start so I guess its all good, Thanks everyone for helping! 好吧,所以我只是从一开始就进行了边框布局,所以我想一切都很好,谢谢大家的帮助! Doesnt look that professional. 看起来不那么专业。 So yeah, Might be some more updates later on :) 是的,以后可能还会有更多更新:)

在此处输入图片说明

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

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