【问题标题】:Swing - GridBagLayout button adding mistakeSwing - GridBagLayout 按钮添加错误
【发布时间】:2014-12-06 15:01:36
【问题描述】:

我使用这个函数来创建标签。当我想添加按钮“?”将打开向导,此按钮出现在此 JTable 的右侧。如何编辑?

comp1 - “1. Dp/Drho”,comp2 - “2. Df2/Drho”,comp3 - “3.SDSS”,comp4 - “4. 帮助”。 name1-4 - 此选项卡的名称。

static protected JPanel allocateUniPane(Component comp1,Component comp2,Component comp3, Component comp4,
                                        String name1, String name2, String name3, String name4){
    JPanel resultPane = new JPanel();
    GridBagLayout gridbagforUnionPane = new GridBagLayout();
    GridBagConstraints cUnionPane = new GridBagConstraints();
    resultPane.setLayout(gridbagforUnionPane);

    cUnionPane.fill = GridBagConstraints.BOTH;
    cUnionPane.anchor = GridBagConstraints.CENTER;
    JButton showWizard = new JButton("?");
    cUnionPane.weightx = 0.5;
    cUnionPane.weighty = 0.5;
    JTabbedPane jtp = new JTabbedPane();
    jtp.addTab(name1, comp1);
    jtp.addTab(name2, comp2);
    jtp.addTab(name3, comp3);
    jtp.addTab(name4, comp4);
    cUnionPane.gridx = 0;
    cUnionPane.gridy = 0;
    resultPane.add(jtp,cUnionPane);
    cUnionPane.fill = GridBagConstraints.NORTH;
    cUnionPane.weightx = 0.5;
    cUnionPane.gridx = 1;
    cUnionPane.gridy = 0;
    resultPane.add(showWizard,cUnionPane);

    return resultPane;
}

【问题讨论】:

    标签: java swing grid-layout gridbaglayout jtabbedpane


    【解决方案1】:

    您希望按钮显示为选项卡式窗格的一部分。您不能使用 GridBagLayout(或大多数其他布局),因为它们在面板上以二维方式布置组件。

    也许 OverlayLayout 会做您想做的事情,因为它将组件定位在第三个维度中。例如:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.plaf.*;
    
    public class TabbedPaneWithComponent
    {
        private static void createAndShowUI()
        {
            JPanel panel = new JPanel();
            panel.setLayout( new OverlayLayout(panel) );
    
            JTabbedPane tabbedPane = new JTabbedPane();
            tabbedPane.add("1", new JTextField("one"));
            tabbedPane.add("2", new JTextField("two"));
            tabbedPane.setAlignmentX(1.0f);
            tabbedPane.setAlignmentY(0.0f);
    
            JCheckBox checkBox = new JCheckBox("Check Me");
            checkBox.setOpaque( false );
            checkBox.setAlignmentX(1.0f);
            checkBox.setAlignmentY(0.0f);
    
            panel.add( checkBox );
            panel.add(tabbedPane);
    
            JFrame frame = new JFrame("TabbedPane With Component");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( panel );
            frame.setLocationByPlatform( true );
            frame.setSize(400, 100);
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    【讨论】:

    • 我找到了关于 Overlay Layout 的文档。但我不明白它是如何工作的。如果我们想要checkBox 在中间怎么办。我的意思是你如何处理物品的位置?我知道它把一个物体画在另一个物体上。我不确定我是否解释了自己。
    • @Frakcool What if we want the checkBox on the middle. 同意文档不是很好,布局也不是那么直观。基本上你需要使用setAlignmentX() 属性。我认为您需要将两个组件的对齐方式更改为 0.5f。我只能通过反复试验才能让它发挥作用。
    • 我需要的,谢谢! JCheckBox 看起来不错,但 JButton 在这里看起来不太好,因为它对于选项卡来说太大并且关闭了底部边框。
    • @Denis,您可以使用setMargins(...) 方法更改按钮的边距以消除文本周围的大部分空间。
    • @camickr 好的,谢谢。这不是我的问题,但看起来很有趣。我对 Swing 充满热情,喜欢阅读这些问题。这就是我问的原因。感谢您的回答。
    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-28
    • 1970-01-01
    • 2013-03-28
    • 2014-01-16
    • 1970-01-01
    相关资源
    最近更新 更多