2015-12-05 94 views
-1

我有以下两个文本字段的窗口的Java代码:一个可编辑,另一个 - 不是。我想灰掉不可编辑的文本字段。我用setBackground()功能,它似乎在Eclipse设计浏览器的工作:设置Swing的背景JTextField

enter image description here

然而,当我将它导出到jar,生成的应用程序是这样的:

enter image description here

我我在MacOS 10.9.3下使用Eclipse 4.4.1

我的代码:

import java.awt.Container; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JFrame; 
import javax.swing.JTextField; 

@SuppressWarnings("serial") 
public class TestFrame extends JFrame { 
    GridBagConstraints constraints; 
    JTextField text0; 

    public TestFrame() { 
     super("Test window"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     Container container = getContentPane(); 
     container.setLayout(new GridBagLayout()); 
     JTextField editableTextField, nonEditableTextField; 

     constraints = new GridBagConstraints(); 
     editableTextField = new JTextField("Enter text here"); 
     constraints.gridx=0; 
     constraints.gridy=0; 
     container.add(editableTextField, constraints); 

     constraints = new GridBagConstraints(); 
     nonEditableTextField = new JTextField("See result here"); 
     nonEditableTextField.setBackground(getForeground()); 
     nonEditableTextField.setEditable(false); 
     constraints.gridx=0; 
     constraints.gridy=1; 
     container.add(nonEditableTextField, constraints); 

     pack(); 
    } 

    public static void main(String args[]) { 
     new TestFrame(); 
    } 
} 

因此,我有两个问题:

  1. 为什么行为是在观众和jar不同?
  2. 如何灰掉jar中的文本字段?
+1

怎么样setEnabled(false)? – Berger

+0

@Berger'setEnabled()'有一点不同的行为。它使文本变灰,但不是背景,我希望背景上有标准颜色的文本,该颜色与应用程序框架背景的颜色相匹配,就像第一张图像一样。 – Roman

+0

它可能与外观和感觉有关,或者UI默认已安装 – MadProgrammer

回答

-1

事实证明,getBackground()getContentPane().getBackground()之间确实有区别。但是,它们都不是负责实际看到灰色的默认窗口背景。 getForeground()也不会编码这种颜色;然而,由于某种原因,它被初始化为观众的一致价值。

Nevertheles,能够通过getContentPane().setBackground()覆盖默认的背景色(虽然它的初始值不匹配):

... 
    Container container = getContentPane(); 
    container.setBackground(new Color(240,240,240)); 
    ... 
    nonEditableTextField.setBackground(container.getBackground()); 
    ... 

这产生无论是在设计观察者和jar的期望的效果:

enter image description here

0

如果将背景色设置为前景色(在此处为黑色),则会将所有内容都设为相同的颜色。

setBackground(getForeground()); 

尝试设置其他颜色。

+0

这并不回答我的问题。为什么观众不会变黑呢? – Roman

+1

它回答问题2,由您选择灰色。 – Berger

0
setBackground(UIManager.getColor("TextField.inactiveBackground")); 
+2

添加一些解释来回答这是如何帮助OP而不是仅仅添加一个代码行。感谢 –

+1

虽然此代码可能回答问题,但最好包含一些上下文,解释它如何工作以及何时使用它。从长远来看,仅有代码的答案是没有用的。 – Bono

0

关于第一个问题:
正如有人说,这上面It could have something to do with the look and feel and or the UI Defaults been installed

至于第二个问题:

  1. 您可以使用UIManager.getColor OR
  2. 可以使用Color类为您的non editable文本字段设置gray颜色。只需使用Color.LIGHT_GRAYColor.GRAY或您想要使用的任何其他常量。

这两种方法在Eclipse IDE和JAR文件中都是一样的。

改进型:

1) nonEditableTextField.setBackground(UIManager.getColor(Color.lightGray)); 

2) nonEditableTextField.setBackground(UIManager.getColor(Color.LIGHT_GRAY)); 

OR

nonEditableTextField.setBackground(Color.LIGHT_GRAY); 

然而,第一种方法是比第二个更优选的。