2012-02-20 233 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

相關問題