JTabbedPane:更改选项卡标题时更改选项卡大小 [英] JTabbedPane: change tab size when change tab title

查看:126
本文介绍了JTabbedPane:更改选项卡标题时更改选项卡大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JFrame 中有一个 JTabbedPane myTab.它的第一个标签的标题是旧标题".我想动态更改标题,所以我用这段代码来设置:

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");

不知何故,我的新标题比旧标题长.问题是,标签大小没有改变,也没有完全显示新标题,只有我的完整n...".

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"));

此代码可以帮助我根据新标题更改标签大小.但是关闭标签的十字 (x) 不再存在.

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?

谢谢,非常感谢!

推荐答案

我从未见过,

但这仅在一种情况下是可能的,您运行的代码超出了 EDT,

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

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

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),

来自代码,

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

这篇关于JTabbedPane:更改选项卡标题时更改选项卡大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆