添加第二个JPanel时,Java CardLayout JPanel向上移动 [英] Java CardLayout JPanel moves up, when second JPanel added

查看:28
本文介绍了添加第二个JPanel时,Java CardLayout JPanel向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,主要是CardLayout.我只想切换由JPanels表示的窗口".我在CardLayout的某处读过这份工作.但是我的问题是,当添加chatPanel mainPanel (这是CardLayout之一)时,它会将 connectPanel 的内容移动到顶部几个像素,远离其居中位置.我是否跳过了代码 createChatPanel()应该位于的位置.

I am new to Java and mostly CardLayout. I want to simply switch "windows" represented by JPanels. I read somewhere that job for CardLayout. But my problem is, when add chatPanel to mainPanel (this is the CardLayout one), it shifts the content of connectPanel several pixels to the top, away from its centered position. Is I skip in my code createChatPanel(), its where it should be.

我有此代码:

package App;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import Validators.*;



public class GUI {

private JFrame mainFrame = null;
private JPanel mainPanel = null;
private CardLayout cl = new CardLayout();

public GUI(){


    try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         } 
         catch (UnsupportedLookAndFeelException e) {
         }
         catch (ClassNotFoundException e) {
         }
         catch (InstantiationException e) {
         }
         catch (IllegalAccessException e) {
         }

    mainFrame = new JFrame("MainChat");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(640,480);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setResizable(false);
    mainFrame.setLayout(new GridBagLayout());

    JMenuBar menuBar = new JMenuBar();

    JMenu menuFile = new JMenu("Soubor");
    JMenu menuHelp = new JMenu("Nápověda");

    menuBar.add(menuFile);
    menuBar.add(menuHelp);

    menuFile.add(new JMenuItem("Nové Připojení"));
    menuFile.add(new JSeparator());
    menuFile.add(new JMenuItem("Konec"));
    menuHelp.add(new JMenuItem("O programu"));
    mainFrame.setJMenuBar(menuBar);


    createMainPanel();
    createConnectPanel();
    createChatPanel();

    mainFrame.setVisible(true);
}

public void createMainPanel() {

    mainPanel = new JPanel(cl);
    mainFrame.add(mainPanel);

}

public void createConnectPanel() {

    JPanel connectPanel = new JPanel();
    mainPanel.add(connectPanel,"connectPanel");

    JTextField ip = new JTextField();
    ip.setDocument(new JTextFieldLimit(15));
    ip.setColumns(11);

    JLabel iplabel = new JLabel("IP:");
    connectPanel.add(iplabel);
    connectPanel.add(ip);

    JButton connect = new JButton("Connect");
    connect.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            cl.show(mainPanel,"chatPanel");

        }
    });
    connectPanel.add(connect);

}

public void createChatPanel(){

    JPanel chatPanel = new JPanel();
    mainPanel.add(chatPanel,"chatPanel");

    JTextArea chatbox = new JTextArea();
    chatbox.setPreferredSize(new Dimension(200,200));
    chatPanel.add(chatbox);

}
}

拜托,我搞砸了吗?谢谢.

Please, what I messed up? Thanks.

推荐答案

由于要在主JPanel中添加两个JPanel,这两个面板都需要放在主面板中.

Since you are adding two JPanels to your main JPanel, these two panels both need to fit within the main panel.

如果其中一个内面板比另一个内面板大得多,主面板将进行调整以适合较大的面板.

If one of the inner panels is much larger than the other one, the main panel will adjust to fit the larger one.

例如评论此行:

chatbox.setPreferredSize(new Dimension(200,200));

将导致您的文本字段保持原样.这是因为chatbox不会导致容器调整大小.

would cause your text field to stay put. This is because the chatbox would not cause the container to resize.

还请注意,主面板最初的尺寸与您的主框架,因为您尚未设置主面板的尺寸.

Also note that the main panel is not initially the same size as your main frame, since you have not set the size of the main panel.

如果将connectPanel的大小设置为相同的大小作为您的主机,connectPanel不会添加chatPanel时自动调整大小(作为调整mainPanel的结果)

If you would set the size of the connectPanel to the same size as your main frame, the connectPanel would not be automatically resized when adding the chatPanel (as a consequence of the mainPanel being resized)

所以您可以在中间添加一行:

So what you could do is add the middle line in:

JPanel connectPanel = new JPanel();
connectPanel.setSize(640, 480);
mainPanel.add(connectPanel, "connectPanel");

,可能会解决您的问题.

, which probably would solve your problem.

尽管这可以工作,但我绝对建议您使用 MIG布局您所有的GUI设计.如果您愿意,它将为您节省大量时间您需要花一个小时来学习它.这也将使您免于必须手动设置尺寸(从而节省您的时间)每次设计更改都必须重写一半的GUI代码.

Although this would work, I definitely recommend using MIG Layout for all your GUI designing. It will save you plenty of time if you take an hour to learn it. It will also save you from having to set sizes manually (and thereby saving you from having to rewrite half your GUI code with every design change).

这篇关于添加第二个JPanel时,Java CardLayout JPanel向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆