2012-02-20 235 views
1

我的JFrame中有一个JTabbedPane myTab。它的第一个标签有一个“旧标题”的标题。我想动态更改标题,所以我用这个代码来设置:JTabbedPane:更改选项卡标题时更改选项卡大小

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

不知怎么我的新头衔是比我的老长。问题是,标签大小不会改变,并且它不会完全显示新标题,只有“我的全部n ...”。

如果我点击标签,突然间标签可以显示全新的标题。

我已经尝试过这种代码太,设置标题名称:

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

这段代码可以帮我相应地改变标签大小为新标题。但是关闭标签的十字(x)不再存在。

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

谢谢,非常感谢!

回答

2

我从来没有看到的是,

但这可能只在一个情况下,你跑是出EDT的代码,

Swing是单线程的,并在Swing GUI的所有改变都必须上完成事件分派线程,更多关于这个话题Concurency in Swing,(我建议寻找在这个论坛这个共同的话题),

enter image description hereenter image description here

从代码,

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

谢谢。我发现我的问题是我使用我自己的类扩展JPanel来创建一个带有交叉的选项卡组件: ButtonTabComponent extends JPanel(); setTabComponentAt(index,new ButtonTabComponent(this));我的解决方案是 ((ButtonTabComponent)myTab.getTabComponentAt(myTab.getSelectedIndex()))。updateUI();和我的ButtonTabComponent不实现更新UI函数 – baizen 2012-02-20 08:45:36

+1

我的解决方案是 ((ButtonTabComponent)myTab.getTabComponentAt(myTab.getSelectedIndex()))。 – baizen 2012-02-20 08:52:09

+0

一次更好看,因为...... – mKorbel 2012-02-20 08:53:07

相关问题