标签窗格更改标签位置Java Swing

xqnpmsa8  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(181)

我有我的情况下,我的标签窗格有3个标签,然后我选择其中的人,它设置在第一个位置,并移动整个标签。

我在网上找不到这样的东西,每个人都有固定的标签位置。如何固定标签?
//已编辑

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

public class Main {
    public static void main(String[] args) {
        
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException |
                 IllegalAccessException e) {
            throw new RuntimeException(e);
        }

        JFrame frame = new JFrame("Somest");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
        tabbedPane.addTab("Calculation", new JLabel());
        tabbedPane.addTab("Store", new JLabel());
        tabbedPane.addTab("Settings", new JLabel());
        UpperPanel.add(tabbedPane);
        frame.add(UpperPanel);
    }

}

查找try {...}“启用”此问题。使用try {...}将JChooser从标准视图更改为或多或少的Windows选择器视图。我必须做什么?

mgdq6dx1

mgdq6dx11#

原因是在各种外观实现中实现方式不同(参见here,第6.2.1节):
[...]选择不在最前面的行或列中的选项卡会将该行或列移动到前面。在使用默认金属L&F的JTabbedPane中不会发生这种情况,如上面的TabbedPaneDemo示例中所示。但是,在使用Windows、Motif和Basic L& F时会发生这种情况。此功能在金属L&F [...]中被有意禁用
负责的方法是shouldRotateTabRuns(...),它在各种TabbedUI实现中返回true或false(参见Metal L&F与Basic L&F)。
要防止制表符旋转,可以如下所示进行覆盖:

public class TabExample extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    e.printStackTrace();
                }

                TabExample app = new TabExample();
                app.setVisible(true);
            }
        });
    }

    private TabExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 500);
        setLocationRelativeTo(null);
        setVisible(true);

        JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);

        // Check if TabbedPaneUI is instance of WindowsTabbedPaneUI
        // You may add checks for other L&F
        if (tabbedPane.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI) {
            tabbedPane.setUI(new MyWindowsTabbedPaneUI());
        }

        tabbedPane.addTab("Calculation", new JLabel());
        tabbedPane.addTab("Store", new JLabel());
        tabbedPane.addTab("Settings", new JLabel());
        UpperPanel.add(tabbedPane);
        add(tabbedPane, BorderLayout.NORTH);
    }

    // Class that overwrites WindowsTabbedPaneUI and returns false
    class MyWindowsTabbedPaneUI extends com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI{

        @Override
        protected boolean shouldRotateTabRuns(int tabPlacement) {
            return false;
        }

    }

}

相关问题