2011-06-02 57 views
1
JSpinner waitHr = new JSpinner(); 
waitHr.setEnabled(false); 

我有一個微調,我需要防止用戶暫時編輯它。問題是,當微調框被禁用時,它的文字顏色使得它很難閱讀,在這種情況下這是不可接受的。我注意到,你可以用JTextField就做這樣的:設置禁用的JSpinner的文本顏色(以便於閱讀)

JTextField txtTest = new JTextField(); 
txtTest.setDisabledTextColor(Color.BLACK); 

是否有類似的事情,可用於的JSpinner?

回答

2

你可以用你想要的

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.LineBorder; 

public class InactiveBackgroundTest { 

    public JComponent makeUI() { 
     JSpinner s0 = new JSpinner(); 
     s0.setPreferredSize(new Dimension(100, 20)); 
     s0.setEnabled(false); 
     UIManager.put("FormattedTextField.inactiveBackground", Color.RED); 
     JSpinner s1 = new JSpinner(); 
     s1.setEnabled(false); 
     s1.setPreferredSize(new Dimension(100, 20)); 
     JSpinner s2 = new JSpinner(); 
     s2.setEnabled(false); 
     s2.setPreferredSize(new Dimension(100, 20)); 
     JTextField field = ((JSpinner.NumberEditor) s2.getEditor()).getTextField(); 
     field.setEditable(false); 
     field.setBackground(UIManager.getColor("FormattedTextField.background")); 
     JSpinner s3 = new JSpinner(); 
     s3.setPreferredSize(new Dimension(100, 20)); 
     s3.setEnabled(false); 
     s3.setBorder(null); 
     JTextField tf = ((JSpinner.DefaultEditor) s3.getEditor()).getTextField(); 
     tf.setDisabledTextColor(Color.black); 
     tf.setBackground(Color.white); 
     tf.setBorder(new LineBorder(Color.blue, 1)); 
     s3.setBorder(new LineBorder(Color.red, 1)); 
     int n = s3.getComponentCount(); 
     if (n > 0) { 
      Component[] components = s3.getComponents(); 
      String compName = ""; 
      for (int i = 0, l = components.length; i < l; i++) { 
       if (components[i] instanceof JButton) { 
        JButton button = (JButton) components[i]; 
        if (button.hasFocus()) { 
         String btnMane = button.getName(); 
        } 
        button.setBorder(new LineBorder(Color.red, 1)); 
        System.out.println("JButton"); 
       } else if (components[i] instanceof JComboBox) { 
        System.out.println("JComboBox"); 
       } else if (components[i] instanceof JTextField) { 
        System.out.println("JTextField"); 
       } else if (components[i] instanceof JFormattedTextField) { 
        System.out.println("JFormattedTextField"); 
       } else if (components[i] instanceof JTable) { 
        System.out.println("JTable"); 
       } else if (components[i] instanceof JScrollPane) { 
        System.out.println("JScrollPane"); 
       } else if (components[i] instanceof JPanel) { 
        JPanel panel = (JPanel) components[i]; 
        panel.setBackground(Color.red); 
        panel.setBorder(null); 
        System.out.println("JPanel"); 
       } 
      } 
     } 
     JPanel p = new JPanel(); 
     p.setBackground(Color.black); 
     p.add(s0); 
     p.add(s1); 
     p.add(s2); 
     p.add(s3); 
     return p; 
    } 

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

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

    public static void createAndShowGUI() { 
     try { 
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { 
       if ("Windows".equals(laf.getName())) { 
        UIManager.setLookAndFeel(laf.getClassName()); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new InactiveBackgroundTest().makeUI()); 
     f.setPreferredSize(new Dimension(120, 140)); 
     f.setLocationRelativeTo(null); 
     f.pack(); 
     f.setVisible(true); 
    } 
} 
由於
+0

+1非常全面! – trashgod 2011-06-02 22:34:03

+0

令人驚歎。謝謝:) – David 2011-06-03 10:41:34

+0

@大衛,不客氣 – mKorbel 2011-06-03 10:56:19

2

您可以使用JSpinner.DefaultEditorgetTextField()方法。

+0

尊重你的人沒人玩都知道,當你需要的:-) http://forums.oracle.com/forums/thread.jspa ?messageID = 9337792&#9337792 :-),pfuuuuj really crossUpVoters – mKorbel 2011-06-03 08:23:35

+0

@mKorbel:很好的參考鏈接。我很抱歉,但我不明白「pfuuuuj真的是crossUpVoters」。你能詳細說明嗎? – trashgod 2011-06-03 16:59:46

+0

嗯只是我upVoted你的文章.... – mKorbel 2011-06-03 17:11:31