如何将标签添加到选项卡式窗格上的额外空间

时间:2014-10-17 21:45:04

标签: java swing layout-manager jtabbedpane

我正在尝试将JLabel添加到JTabbedPane的空白处(位于标签右侧和内容面板上方)。

我非常接近这个SSCCE(使用MigLayout):

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

    JFrame frame = new JFrame();
    frame.setLayout(new MigLayout(new LC().fill().insetsAll("0")));

    JTabbedPane jtp = new JTabbedPane();
    jtp.add(new JPanel(), "Tab 1");
    jtp.add(new JPanel(), "Tab 2");

    JLabel label = new JLabel("label");

    JPanel panel = new JPanel(new MigLayout(new LC().fill()));
    panel.add(jtp, "id tabbedpane, grow, span");
    panel.add(label, "pos (tabbedpane.w-label.w) 10, id label");
    label.setBounds(100, 100, 10, 10);

    frame.add(panel, "grow, span");
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null); // Sorry, Andrew Thompson
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

但是,不幸的是,这个SSCCE遇到MigLayout个错误(读作:MigLayout error: "Unstable cyclic dependency in absolute linked values!")。如果您删除WindowsLookAndFeel代码,则会显示以下内容:

enter image description here

这正是我想要的,除了它缺少WindowsLookAndFeel(这是我的应用程序的要求; MigLayout不是)。但是,如果没有MigLayout,我无法弄清楚如何实现这一目标。

如何在该空白处添加JLabel

1 个答案:

答案 0 :(得分:4)

您可以尝试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();
            }
        });
    }
}

编辑:

这是一个小测试程序,我把它放在一起,检查布局如何根据组件的X / Y对齐进行工作:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class OverlayLayoutTest extends JPanel
    implements ActionListener
{
    JPanel green;
    JPanel red;
    JLabel greenLabel;
    JLabel redLabel;
    JComboBox  greenAlignmentX;
    JComboBox  greenAlignmentY;
    JComboBox  redAlignmentX;
    JComboBox  redAlignmentY;

    public OverlayLayoutTest()
    {
        setLayout( new BorderLayout(10, 10) );
        add(createNorthPanel(), BorderLayout.NORTH);
        add(createCenterPanel(), BorderLayout.CENTER);
        add(createSouthPanel(), BorderLayout.SOUTH);
    }

    private JPanel createNorthPanel()
    {
        JPanel panel = new JPanel();

        panel.add( new JLabel("Green:") );
        greenLabel = new JLabel();
        panel.add( greenLabel );

        panel.add( new JLabel("Red:") );
        redLabel = new JLabel();
        panel.add( redLabel );

        return panel;
    }

    private JPanel createCenterPanel()
    {

        JPanel panel = new JPanel();
        panel.setLayout( new OverlayLayout(panel) );
        panel.setBackground( Color.ORANGE );
        panel.setPreferredSize( new Dimension(200, 200) );

        red = new JPanel();
        red.setBackground( Color.RED );
        red.setPreferredSize( new Dimension(50, 50) );
        red.setMaximumSize( red.getPreferredSize() );
        red.setMinimumSize( red.getPreferredSize() );
        panel.add( red );

        green = new JPanel();
        green.setBackground( Color.GREEN );
        green.setPreferredSize( new Dimension(100, 100) );
        green.setMaximumSize( green.getPreferredSize() );
        green.setMinimumSize( green.getPreferredSize() );
        panel.add( green );

        JPanel wrap = new JPanel();
        wrap.add( panel );
        return wrap;
    }

    private JPanel createSouthPanel()
    {
        JPanel panel = new JPanel( new GridLayout(1, 0, 10, 10) );

        JPanel green = new JPanel(new GridLayout(0, 2, 5, 5) );
        green.setBorder( new TitledBorder("Green Alignment") );
        green.add( new JLabel("X Alignment:") );
        greenAlignmentX = createComboBox();
        green.add( greenAlignmentX );
        green.add( new JLabel("Y Alignment:") );
        greenAlignmentY = createComboBox();
        green.add( greenAlignmentY );
        panel.add( green );

        JPanel red = new JPanel(new GridLayout(0, 2, 5, 5) );
        red.setBorder( new TitledBorder("Red Alignment") );
        red.add( new JLabel("X Alignment:") );
        redAlignmentX = createComboBox();
        red.add( redAlignmentX );
        red.add( new JLabel("Y Alignment:") );
        redAlignmentY = createComboBox();
        red.add( redAlignmentY );
        panel.add( red );

        JButton reset = new JButton("Reset Alignment");
        reset.addActionListener( this );
        panel.add( reset );


        return panel;
    }

    public void actionPerformed(ActionEvent e)
    {
        green.setAlignmentX( ((Float)greenAlignmentX.getSelectedItem()) );
        green.setAlignmentY( ((Float)greenAlignmentY.getSelectedItem()) );
        red.setAlignmentX( ((Float)redAlignmentX.getSelectedItem()) );
        red.setAlignmentY( ((Float)redAlignmentY.getSelectedItem()) );
        JPanel parent = (JPanel)green.getParent();
        parent.revalidate();

        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                greenLabel.setText( green.getLocation().toString() );
                redLabel.setText( red.getLocation().toString() );
            }
        });

    }

    private JComboBox createComboBox()
    {
        JComboBox<Float> comboBox = new JComboBox<Float>();

        comboBox.addItem( new Float(0f) );
        comboBox.addItem( new Float(0.25f) );
        comboBox.addItem( new Float(0.5f) );
        comboBox.addItem( new Float(0.75f) );
        comboBox.addItem( new Float(1.0f) );
        comboBox.setSelectedItem(0.5f);

        return comboBox;
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("OverlayLayoutTest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new OverlayLayoutTest() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}