2012-05-17 42 views
5

我需要一個JLabel風格的JTextField(換句話說,我需要一個JLabel,但我希望能夠選擇文本)。使用GTKLookAndFeel和JLabel風格的JTextField

JTextField value = new JTextField(valueText); 
value.setEditable(false); 
value.setBorder(BorderFactory.createEmptyBorder); 
value.setForeground(UIManager.getColor("Label.foreground"));    
value.setFont(UIManager.getFont("Label.font")); 
value.setBackground(UIManager.getBackground("Label.background")); 
... 

它工作正常,在Windows和Mac:

JTextField like a JLabel (Mac)

但在Ubuntu(GTKLookAndFeel)我得到了一個結果:

JTextField like a JLabel (Ubuntu)

其中, 「通過」 是JTextField和「Size」是JLabel

+0

出於興趣,你運行的是哪個版本的Ubuntu/Java?我剛剛使用OpenJDK 1.6.0_24在Ubuntu 12.04機器上運行此代碼,並且按預期運行;它看起來就像一個標籤。 – Jamie

+0

@Jamie Ubuntu 10.04和java 1.6.0.22。你有沒有設置UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())? – Ivan

+0

啊,這確實打破了......不好意思,我以爲默認會使用GTK LookAndFeel,但顯然不是! – Jamie

回答

1

最後我得到了使用,而不是JTextField的JEditorPane中預期的結果:

JEditorPane value = new JEditorPane(); 
value.setText(valueText); 
... 

看來,GTK外觀及感覺不允許JTextField中自定義背景: http://bugs.sun.com/view_bug.do?bug_id=6531760

2

我認爲setBorder(null);在/復位默認BordersUIMananager來抓GTK大號& F或爲JTextField

編輯將該值設置爲UIMananager直接:

你必須改變BorderUIResource,以同樣的方式作爲demonstrated here for FontUIResource

EDIT2:

enter image description here和在1,5秒enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.Border; 
import javax.swing.border.LineBorder; 
import javax.swing.plaf.BorderUIResource; 

public class SystemFontDisplayer extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JFrame frame = new JFrame("Nimbus UIDeafaults and Font"); 
    private javax.swing.Timer timer = null; 
    private JTextField testTextField = new JTextField("testTextField"); 
    private JButton testButton = new JButton("testButton"); 

    public SystemFontDisplayer() { 
     frame.setLayout(new GridLayout(4, 0, 20, 20)); 
     testTextField.setBorder(null); 
     frame.add(testTextField); 
     frame.add(testButton); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(200, 105); 
     frame.pack(); 
     frame.setVisible(true); 
     start(); 
    } 

    private void start() { 
     timer = new javax.swing.Timer(1500, updateCol()); 
     timer.setRepeats(false); 
     timer.start(); 
    } 

    public Action updateCol() { 
     return new AbstractAction("text load action") { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       Border border = new LineBorder(Color.red, 2); 
       final BorderUIResource res = new BorderUIResource(border); 
       UIManager.getLookAndFeelDefaults().put("TextField.border", new BorderUIResource(border)); 
       UIManager.getLookAndFeelDefaults().put("Button.border", new BorderUIResource(border)); 
       SwingUtilities.updateComponentTreeUI(frame); 

       /*Border border = new LineBorder(Color.red, 2); 
       try { 
       LookAndFeel lnf = UIManager.getLookAndFeel().getClass().newInstance(); 
       final BorderUIResource res = new BorderUIResource(border); 
       UIDefaults uiDefaults = lnf.getDefaults(); 
       uiDefaults.put("TextField.border", res); 
       uiDefaults.put("Label.border", res); 
       uiDefaults.put("Button.border", res); 
       UIManager.getLookAndFeel().uninitialize(); 
       UIManager.setLookAndFeel(lnf); 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 
       UIDefaults defaults = UIManager.getDefaults(); 
       BorderUIResource res = new BorderUIResource(border); 
       defaults.put("TextField.border", res); 
       defaults.put("Label.border", res); 
       defaults.put("Button.border", res); 
       SwingUtilities.updateComponentTreeUI(frame);*/ 
      } 
     }; 
    } 

    public static void main(String arg[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       SystemFontDisplayer systemFontDisplayer = new SystemFontDisplayer(); 
      } 
     }); 
    } 
} 
+0

我已經嘗試過使用setBorder(null)獲得相同的結果。我不明白第二個解決方案。 – Ivan

+0

請運行[UIManager默認值](http://tips4java.wordpress.com/2008/10/09/uimanager-defaults/),選擇GTK並搜索相關值爲JTextField – mKorbel

+0

請參閱編輯我的回答 – mKorbel