简体   繁体   English

Java组件替换JPanel

[英]Java components replacement JPanel

I'm trying to create an application in 3 parts : 3 labels and 3 gridlayouts. 我试图创建一个由3部分组成的应用程序:3个标签和3个gridlayouts。 When we click on a label, the corresponding gridlayout disappear and the frame replace automatically the components at the right place. 当我们单击标签时,相应的网格布局消失,框架自动在正确的位置替换组件。 I created a simple snippet : 我创建了一个简单的代码段:

import java.awt.Button;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestLayout extends JFrame{
    private JPanel content;
    private JLabel[] lbl;
    private JPanel[] pnl;
    private Boolean[] ih;

    public TestLayout(){
        setTitle("Test");
        setSize(new Dimension(300, 400));
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        lbl = new JLabel[3];
        pnl = new JPanel[3];
        ih  = new Boolean[3];

        content = new JPanel(new GridLayout(6, 1));
        for(int i=0; i<3; i++){
            lbl[i] = new JLabel("Label" + i);
            lbl[i].addMouseListener(new MouseAdapter()  
            {  
                public void mouseClicked(MouseEvent e)  
                {  
                    for(int i=0; i<3; i++){
                        if(e.getSource() == lbl[i]){
                            //pnl[i].setVisible(!pnl[i].isVisible());
                            if(ih[i]) content.remove(pnl[i]);
                            else content.add(pnl[i]);
                            ih[i] = !ih[i];
                        }
                    }

                }  
            }); 
            pnl[i] = new JPanel(new GridLayout(3, 3));
        }

        for(int i=0; i<9; i++){
            pnl[0].add(new Button("" + (i+1)));
            pnl[1].add(new Button("" + (i+10)));
            pnl[2].add(new Button("" + (i+19)));
        }

        for(int i=0; i<3; i++){
            content.add(lbl[i]);
            content.add(pnl[i]);
            ih[i] = true;
        }

        add(content);
        setVisible(true);
    }
    public static void main(String[] args){
        new TestLayout();
    }   
}

The first problem is the use of a global gridlayout which resize all the components at the same size, but I think it would be better if labels could be smaller than the grilayouts. 第一个问题是使用全局网格布局,该网格布局将所有组件的大小调整为相同大小,但是我认为如果标签小于网格布局会更好。

The second problem is that even if the gridlayout is removed or setVisible(false), it still take a blank place in the global container. 第二个问题是,即使删除了gridlayout或setVisible(false),它在全局容器中仍然占据空白。

What I get : 我得到的是:

在此处输入图片说明

What I was expecting : 我期待的是:

在此处输入图片说明

The only thing I don't wanna use is a GridBagLayout. 我唯一不想使用的是GridBagLayout。

I was thinking about create a method init() which one remove all components of the global container then re add all the labels and all the panels, then create another method which do the exact same as the init() method but take a number as parameter (for example 2) then re add all the components excepting the second gridlayout. 我正在考虑创建一个方法init(),该方法将删除全局容器的所有组件,然后重新添加所有标签和所有面板,然后创建另一个方法,该方法与init()方法完全相同,但取一个数字作为参数(例如2),然后重新添加除第二个gridlayout之外的所有组件。 But I think it's a dirty way to do that because the container will content an empty case at the end and I think there is a better way than removing and re adding all the components (which basically doesn't solve the first problem of label's size) 但是我认为这样做是一种肮脏的方式,因为容器最后将容纳一个空的容器,而且我认为有比删除并重新添加所有组件更好的方法(这基本上不能解决标签尺寸的第一个问题)

How can I avoid theses problems ? 如何避免这些问题?

Try using a vertical BoxLayout . 尝试使用垂直的BoxLayout

Read the section from the Swing tutorial on How to Use BoxLayout for more information and working examples. 阅读Swing教程中有关如何使用BoxLayout的部分, 获取更多信息和工作示例。

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

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