java - Swing - GridBagLayout 按钮添加错误

标签 java swing grid-layout gridbaglayout jtabbedpane

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

comp1 - “1. Dp/Drho”,comp2 - “2. Df2/Drho”,comp3 - “3. SDSS”,comp4 - “4. Help”。 name1-4 - 此选项卡的名称。 enter image description here

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;
}

最佳答案

您希望按钮显示为选项卡式 Pane 的一部分。您不能使用 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();
            }
        });
    }
}

关于java - Swing - GridBagLayout 按钮添加错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27332964/

相关文章:

java - Ibatis版show sql怎么做

java - 提高 jXLS 的性能

Java:透明 JPanel 上的 MouseEvent

java - mouseEntered(MouseEvent e) 上线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常

html - 三/四列列表,每个元素下方都有弹出式描述

java - 我们可以在作业的 LHS 上构建和使用字符串吗?

java - Tomcat 不呈现 jsp 页面

java - 在同一个 jlabel 上重新显示其他 img 存在困难

html - 如何在 CSS 网格布局中指定行高?

java - 无法将图像添加到 JPanel GridLayout 的特定单元格网格