【问题标题】:Changing size for JTabbedPane's Title更改 JTabbedPane 标题的大小
【发布时间】:2015-10-05 16:41:34
【问题描述】:

我正在使用 JTabbedPane 创建我的 IDE。当我将标签添加到我的 JTabbedPane 时,有些标题较长,有些则较短。这会导致一些非常长的标题的标题长度非常难看。

我将窗格添加到 JTabbedPane 的示例:

codes.addTab("", icon, scroll.get(scroll.size() - 1));

当我运行程序时,这会导致以下输出:

如您所见,标题较长的标签会使标题标签变得又长又丑。当我添加太多标签时,会发生这种情况:

问题:我能否修复或缩放标题大小,比如限制标题的长度是否太长?也可能是太多文件上的左右箭头?就像这样:

【问题讨论】:

    标签: java swing user-interface title jtabbedpane


    【解决方案1】:

    如果你想设置选项卡的大小,这会有点复杂,因为在某种程度上你应该覆盖 JTabbedPane paint() 方法族和你使用的 LookAndFeel 的默认行为。我想说它并没有你想的那么难看,因为诸如 Eclipse 或 Intellij 之类的 manu IDE 也有不同长度的源文件标签大小:

    日食:

    所以不要担心大小。

    但是关于标签的放置,你必须使用下面的方法来改变标签放置的布局:

    tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    

    您可以在新孵化的 IDE 中添加一些 SplitPanes 以使其模式更专业。试试下面的 sn-p:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextArea;
    import javax.swing.JToolBar;
    import javax.swing.UIManager;
    
    public class MainWindow extends JFrame {
        private static final int FRAME_WIDTH = 1024;
        private static final int FRAME_HEIGHT = 600;
    
        public MainWindow() {
            Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds(dim.width/2-FRAME_WIDTH/2, dim.height/2-FRAME_HEIGHT/2, FRAME_WIDTH, FRAME_HEIGHT);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //
            this.setLayout(new BorderLayout(4, 4));
            //
            JToolBar toolBar = new JToolBar();
            toolBar.add(new JButton("Test"));
            toolBar.add(new JButton("Test"));
            toolBar.add(new JButton("Test"));
            toolBar.add(new JButton("Test"));
            JPanel northPanel = new JPanel();
            northPanel.add(toolBar);
            //
            JPanel southPanel = new JPanel();
            JLabel statusLabel = new JLabel("Test Status...");
            southPanel.add(statusLabel);
            //
            this.add(northPanel, BorderLayout.NORTH);
            this.add(southPanel, BorderLayout.SOUTH);
            //
            JPanel westPanel = new JPanel(new BorderLayout());
            JPanel eastPanel = new JPanel(new BorderLayout());
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setLeftComponent(eastPanel);
            splitPane.setRightComponent(westPanel);
            splitPane.setDividerLocation(250);
            splitPane.setContinuousLayout(true);
            this.add(splitPane, BorderLayout.CENTER);
            //
            JTabbedPane tabbedPane = new JTabbedPane();
            tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
            tabbedPane.addTab("MyFrame.java", new JTextArea());
            tabbedPane.addTab("JButton.java", new JTextArea());
            tabbedPane.addTab("Main.java", new JTextArea());
            tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
            tabbedPane.addTab("MyFrame.java", new JTextArea());
            tabbedPane.addTab("JButton.java", new JTextArea());
            tabbedPane.addTab("Main.java", new JTextArea());
            tabbedPane.addTab("MyNewIdeTabbedPaneTest.java", new JTextArea());
            tabbedPane.addTab("MyFrame.java", new JTextArea());
            tabbedPane.addTab("JButton.java", new JTextArea());
            tabbedPane.addTab("Main.java", new JTextArea());
            tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            tabbedPane.setAutoscrolls(false);
            //tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT); Also try this in comparison with above line
            westPanel.add(tabbedPane, BorderLayout.CENTER);
        }
    
        public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            MainWindow mw = new MainWindow();
            mw.setVisible(true);
        }
    }
    

    祝你好运。

    【讨论】:

      【解决方案2】:

      您可以像这样将标题向左对齐:

      JLabel tabLabel = new JLabel("Tab", JLabel.LEFT);
      //add new label at set location
      jTabbedPane.setTabComponentAt(0, tabLabel);
      

      如果您想了解更多信息,请查看this link,答案非常完整。

      希望对你有帮助

      编辑:您还可以限制标题的长度,这是另一种可能性

      你也可以关注这个tutorial Oracle 制作的,很简单

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-30
        • 1970-01-01
        • 2018-01-16
        • 1970-01-01
        • 2019-02-05
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多