JTabbedPane:更改选项卡标题时更改选项卡大小

发布于 2025-01-07 09:33:05 字数 520 浏览 1 评论 0原文

我的 JFrame 中有一个 JTabbedPane myTab。它的第一个选项卡的标题为“旧标题”。我想动态更改标题,因此我使用此代码来设置:

myTab.setTitleAt(myTab.getSelectedIndex(), "my full new title");

不知何故,我的新标题比旧标题长。问题是,选项卡大小没有改变,并且它没有完全显示新标题,只有“我的完整n...”。

如果我单击该选项卡,该选项卡突然可以显示完整的新标题。

我也已经尝试过此代码来设置标题名称:

myTab.setTabComponentAt(myTab.getSelectedIndex(), new JLabel("my full new title"));

此代码可以帮助我根据新标题更改选项卡大小。但用于关闭选项卡的十字 (x) 不再存在。

有谁知道如何在更改选项卡标题时更改选项卡大小,但仍保留关闭选项卡选项?

谢谢并非常感谢!

I have a a JTabbedPane myTab within my JFrame. Its first tab has a title of "old title". I want to change the title dynamically, so I use this code to set:

myTab.setTitleAt(myTab.getSelectedIndex(), "my full new title");

And somehow my new title is longer than my old one. The problem is, the tab size does not change, and it does not display the new title fully, only "my full n...".

And if I click on the tab, suddenly the tab can show full new title.

I already tried this code too, to set the title name:

myTab.setTabComponentAt(myTab.getSelectedIndex(), new JLabel("my full new title"));

This code can help me change the tab size accordingly to the new title. But the cross (x) to close tab is not there anymore.

Does anyone know how to change the tab size when changing tab title, but still keep the close tab option?

Thank you and much appreciate!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2025-01-14 09:33:05

我从未见过这种情况,

但这仅在一种情况下是可能的,您运行的代码不在 EDT 之外,

Swing 是单线程的,并且对 Swing GUI 的所有更改都必须在事件调度线程上完成,有关此主题的更多信息,请参见 Swing 中的并发,(我建议搜索这个常见主题在此论坛上),

在此处输入图像描述在此处输入图像描述

来自代码,

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;

/*based on @trashgod code original code 
@see http://stackoverflow.com/questions/5617027 */
public class Cab extends JPanel {

    private static final Random random = new Random();
    private static final String format = "00000000";
    private static final DecimalFormat df = new DecimalFormat(format);
    private static JTabbedPane tabbedPane = new JTabbedPane();
    private static final long serialVersionUID = 1L;
    private Hue hue = Hue.Yellow;
    private Timer timer;
    private JLabel odometer = new JLabel(df.format(0));
    private int km;

    public Cab() {
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(hue.getColor());
        this.add(odometer);
        final JComboBox colorBox = new JComboBox();
        for (Hue h : Hue.values()) {
            colorBox.addItem(h);
        }
        colorBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Hue h = (Hue) colorBox.getSelectedItem();
                Cab.this.setBackground(h.getColor());
                tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
            }
        });
        this.add(colorBox);
        timer = new Timer(250, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                km += random.nextInt(100);
                odometer.setText(df.format(km));
            }
        });
        timer.start();
    }

    private enum Hue {

        Yellow(Color.yellow), Cyan(Color.cyan), Magenta(Color.magenta);
        private final Color color;

        private Hue(Color color) {
            this.color = color;
        }

        public Color getColor() {
            return color;
        }
    }

    private static void display() {
        JFrame f = new JFrame("Dispatch");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tabbedPane.add("Cab #1", new Cab());
        tabbedPane.add("Cab #2", new Cab());
        tabbedPane.add("Cab #3", new Cab());
        f.add(tabbedPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                display();
            }
        });
    }
}

I never saw that,

but this is possible only in one cases, code that you run is out of EDT,

Swing is single threaded and all changes to the Swing GUI must be done on Event Dispatch Thread, more about this topic in Concurency in Swing, (I'd suggest to search for this common topic on this Forum),

enter image description hereenter image description here

from code,

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.*;

/*based on @trashgod code original code 
@see http://stackoverflow.com/questions/5617027 */
public class Cab extends JPanel {

    private static final Random random = new Random();
    private static final String format = "00000000";
    private static final DecimalFormat df = new DecimalFormat(format);
    private static JTabbedPane tabbedPane = new JTabbedPane();
    private static final long serialVersionUID = 1L;
    private Hue hue = Hue.Yellow;
    private Timer timer;
    private JLabel odometer = new JLabel(df.format(0));
    private int km;

    public Cab() {
        this.setPreferredSize(new Dimension(320, 240));
        this.setBackground(hue.getColor());
        this.add(odometer);
        final JComboBox colorBox = new JComboBox();
        for (Hue h : Hue.values()) {
            colorBox.addItem(h);
        }
        colorBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Hue h = (Hue) colorBox.getSelectedItem();
                Cab.this.setBackground(h.getColor());
                tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), "my full new title");
            }
        });
        this.add(colorBox);
        timer = new Timer(250, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                km += random.nextInt(100);
                odometer.setText(df.format(km));
            }
        });
        timer.start();
    }

    private enum Hue {

        Yellow(Color.yellow), Cyan(Color.cyan), Magenta(Color.magenta);
        private final Color color;

        private Hue(Color color) {
            this.color = color;
        }

        public Color getColor() {
            return color;
        }
    }

    private static void display() {
        JFrame f = new JFrame("Dispatch");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tabbedPane.add("Cab #1", new Cab());
        tabbedPane.add("Cab #2", new Cab());
        tabbedPane.add("Cab #3", new Cab());
        f.add(tabbedPane);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                display();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文