有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java两个组件使用MigLayout相互重叠

我试图得到两个组件

    frame.getContentPane().setLayout(new MigLayout("", "[53px,grow][57px][grow]", "[23px][][][]"));

    JTextPane itemTitle = new JTextPane();
    frame.getContentPane().add(itemTitle,"cell 0 4,alignx left,aligny top");
    itemTitle.setVisible(true);
    itemTitle.setMinimumSize(new Dimension(50, 50));

    List choices = new List();
    frame.add(choices, "cell 0 4,alignx left,aligny top");
    choices.setVisible(true);

在同一个地方,但发生的一切是:

https://i.imgur.com/0O9A8tk.png

itemTitle和choices中突出显示的两个组件。 我的目标是让上面的按钮将一个“setVisible”设置为true,另一个设置为false。他们永远不会都是真的。如何在一个单元格中同时获得两个组件?它也把我上面的按钮放错了位置,我不太清楚为什么。我把这两个组件的重要代码放在上面,如果您要求,我可以把完整的GUI代码放在上面

我发现:Fill Entire Cell With Two Components Using MigLayout但是它已经过时了,老实说,我不明白解决方法。 我在不断学习,我以前从未使用过MigLayout。我应该使用不同的布局吗

谢谢你的帮助


共 (2) 个答案

  1. # 1 楼答案

    Should I be using a different layout?

    不,坚持使用MigLayout。您选择了正确的布局管理器。 我建议你花点时间学习这位经理;创造夫妇 在较小的实际示例中,了解此 经理提供

    It also puts my above buttons out of place and I'm not too sure why.

    MigLayout是一个基于网格的布局管理器。(其最重要的模式是。) 新按钮和打开按钮之间的间隙是由于 高亮显示的组件和新按钮构成一列。列宽 由最大单元格的宽度确定。要绕过这一点,我们可以 使用split约束。(通常与span约束结合使用。) 在我的示例中,我使用此技术将两个按钮居中放置在 当前可见的标签。 如果我们对布局中的某些内容不确定,可以使用 debug绘制网格线和边界的布局约束 组件的名称

    frame.getContentPane().setLayout(new MigLayout("", "[53px,grow][57px][grow]", "[23px][][][]"));
    

    不要以像素为单位设置边界。你没有利用其中一个 此管理者的最重要优势是独立于决议和 新闻部。像素边界不可移植。在较小的屏幕上,按钮之间的3倍间隙看起来可以,但在较大的屏幕上则不可接受。 MigLayout提供了多个选项供选择,包括相关和不相关的间隙、逻辑像素、点或厘米

    itemTitle.setMinimumSize(new Dimension(50, 50));
    

    通常不需要设置零部件的最小尺寸。但如果我们需要 这样做,我们就有了wminwmax约束。这应该由政府来完成 布局管理器,而不是它之外的代码。集合(最小|最大|首选)大小 应该避免使用这种方法。(然而,对于较差的管理者,我们不能没有他们。) 同样,以像素为单位设置尺寸并不是最优的

    现在我们来解决问题MigLayout具有hidemode处理约束 符合你的要求。有四种隐藏模式。我想我们需要 hidemode 3,其中所有不可见组件都不参与 布局

    package com.zetcode;
    
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.util.ArrayList;
    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import net.miginfocom.swing.MigLayout;
    
    
    public class HidingComponentsEx extends JFrame {
    
        private ArrayList<JLabel> lbls;
        private int itemVisible = 0;
    
        public HidingComponentsEx() {
    
            initUI();
        }
    
        private void initUI() {
    
            createLabels();
    
            JButton prevBtn = new JButton("Previous");
            prevBtn.addActionListener(new PrevAction()); 
    
            JButton nextBtn = new JButton("Next");
            nextBtn.addActionListener(new NextAction());        
    
            JPanel pnl = new JPanel(new MigLayout("ins dialog"));
    
            pnl.add(prevBtn, "split 2, center");
            pnl.add(nextBtn, "wrap");
    
            for (JLabel lbl : lbls) {
                pnl.add(lbl, "cell 0 1, w 250lp, h 100lp, hidemode 3");
            }
    
            add(pnl);
            pack();
    
            setTitle("MigLayout example");
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        }
    
        private void createLabels() {
    
            lbls = new ArrayList<>();
            lbls.add(createLabel("North Sea"));
            lbls.add(createLabel("Ionian Sea"));
            lbls.add(createLabel("Norwegian Sea"));
            lbls.add(createLabel("Bering Sea"));
            lbls.add(createLabel("Dead Sea"));  
            lbls.get(itemVisible).setVisible(true);
        }
    
        private class NextAction extends AbstractAction {
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
                lbls.get(itemVisible).setVisible(false);
                itemVisible++;
    
                if (itemVisible > 4) {
                    itemVisible = 0;
                }
    
                lbls.get(itemVisible).setVisible(true);
            }
        }
    
        private class PrevAction extends AbstractAction {
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
                lbls.get(itemVisible).setVisible(false);
                itemVisible ;
    
                if (itemVisible < 0) {
                    itemVisible = 4;
                }
    
                lbls.get(itemVisible).setVisible(true);
            }
        }    
    
        private JLabel createLabel(String text) {
    
            JLabel lbl = new JLabel(text, JLabel.CENTER);
            lbl.setVisible(false);
            lbl.setBorder(BorderFactory.createEtchedBorder());
    
            return lbl;
        }
    
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    HidingComponentsEx ex = new HidingComponentsEx();
                    ex.setVisible(true);
                }
            });       
        }
    }
    

    我们的示例有两个按钮和五个标签。按钮会动态地更改其属性 能见度

    MigLayout example

  2. # 2 楼答案

    to be in the same place... My aim is to have the buttons above set one "setVisible" to true and the other to false. They would never both be true

    然后您应该使用包含这两个组件的JPanel。然后在该面板上使用Card Layout,并使用CardLayout确定在任何给定时间哪个组件是可见的。与任何其他组件一样,可以使用MigLayout将此面板添加到面板中

    List choices = new List();
    

    在我看来,您正在使用AWT组件。您应该对Swing应用程序使用JList