看,应用程序的背景颜色和2个禁用文本字段的背景颜色不同问题:如何更改禁用和不可编辑文本字段的背景颜色。
t5
是右边的文本字段(在照片中)。我尝试过的是:把t5。挫折背景(颜色…)
,
t5。挫折背景(UIManager.getColor(“t5.background”)
,t5。挫折背景(空)
在构造函数的末尾。我甚至读到,背景颜色在之前被更改后,JTextField的背景颜色在禁用时不会变为“灰色”,而JTextField的背景颜色在启用/禁用时也不会变为“灰色”,但我想不出一种方法来做我想做的事。我使用的是Netbeans 8(Nimbus主题)。如果我将LaF设置为Windows,那么颜色是相同的,但如何使Nimbus本身的颜色相同?
“非活动”颜色(通常)由外观和感觉提供。例如,Windows下的属性TextField。inactiveBackground
可用于影响不可编辑的背景颜色。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
public class TestTextField {
public static void main(String[] args) {
new TestTextField();
}
public TestTextField() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
UIManager.put("TextField.inactiveBackground", new ColorUIResource(new Color(255, 0, 0)));
JTextField field = new JTextField("Hello", 10);
field.setEditable(false);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(field);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
使用Nimbus示例更新
光轮就是喜欢难搞...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.Painter;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.nimbus.AbstractRegionPainter;
public class TestTextField {
public static void main(String[] args) {
new TestTextField();
}
public TestTextField() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextField field = new JTextField("Hello", 10);
field.setEditable(false);
field.setEnabled(false);
UIDefaults overrides = new UIDefaults();
overrides.put("TextField.background", new ColorUIResource(Color.RED));
overrides.put("TextField[Enabled].backgroundPainter", new Painter<JTextField>() {
@Override
public void paint(Graphics2D g, JTextField field, int width, int height) {
g.setColor(Color.RED);
g.fill(new Rectangle(0, 0, width, height));
}
});
overrides.put("TextField[Disabled].backgroundPainter", new Painter<JTextField>() {
@Override
public void paint(Graphics2D g, JTextField field, int width, int height) {
g.setColor(Color.GREEN);
Insets insets = field.getInsets();
g.fill(new Rectangle(
insets.left,
insets.top,
width - (insets.left + insets.right),
height - (insets.top + insets.bottom)));
}
});
field.putClientProperty("Nimbus.Overrides", overrides);
// field.putClientProperty("Nimbus.Overrides.InheritDefaults",false);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(field);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
我只显示了两个值(默认值和禁用值),您需要使用其他值。
TextField.background = DerivedColor(color=255,255,255 parent=nimbusLightBackground offsets=0.0,0.0,0.0,0 pColor=255,255,255
TextField.contentMargins = javax.swing.plaf.InsetsUIResource[top=6,left=6,bottom=6,right=6]
TextField.disabled = DerivedColor(color=214,217,223 parent=control offsets=0.0,0.0,0.0,0 pColor=214,217,223
TextField.disabledText = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
TextField.focusInputMap = javax.swing.plaf.InputMapUIResource@6a4ba620
TextField.font = javax.swing.plaf.FontUIResource[family=SansSerif,name=sansserif,style=plain,size=12]
TextField.foreground = DerivedColor(color=0,0,0 parent=text offsets=0.0,0.0,0.0,0 pColor=0,0,0
TextFieldUI = javax.swing.plaf.synth.SynthLookAndFeel
TextField[Disabled].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@c87b565
TextField[Disabled].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@21960050
TextField[Disabled].textForeground = DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145
TextField[Enabled].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@7eee9569
TextField[Enabled].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@61936199
TextField[Focused].borderPainter = javax.swing.plaf.nimbus.TextFieldPainter@12ecb5db
TextField[Selected].backgroundPainter = javax.swing.plaf.nimbus.TextFieldPainter@72974691
TextField[Selected].textForeground = DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255
有趣的是,如果您使用field.putClientProperty("Nimbus. Overrides. In遗产默认值", false);
,那么您最终会得到一个非常简单的字段(没有边框等)。
这种方法只影响单个组件。。。
我正在使用TextFlow和一些Text项目来显示样式化的文本,但我找不到为Text项目设置简单背景颜色的方法。 我可以设置填充颜色和字体,但它没有设置背景颜色的java方法或css属性。
当批准日期晚于今天时,我试图更改行的颜色。此时,行应该被涂成红色……知道为什么这段代码不能工作吗?
我需要在我的程序中使用文本区域,我也需要它是只读的。 这是我创建textArea的主程序的一部分: 这是我的css文件的一部分: 如果我删除行:ta.setDisable(true);Css的工作方式就像我希望它工作一样。但是在我将禁用 true 设置为 true 之后,它只会使文本区域透明,这使得文本非常难以阅读,并且背景颜色也不是我想要的。 有没有其他方法可以只读设置文本?或者有没有办法在禁用
我正在使用Css3。我正在尝试学习如何实现透明背景,以便我可以看到位于其后面的的背景图像。(背景色:透明!重要= html代码: 代码:
问题内容: 我正在使用Nimbus外观。我需要在JTabbedPane中更改选项卡的背景色和前景色,但在JTabbedPane中未设置颜色。我尝试了setForeground(),setForegroundAt(),setBackground()和setBackgroundAt()方法,但没有用。这是我的代码 } 问题答案: 您可以执行几项不同的操作,具体取决于您希望对确切颜色进行多少控制。最简单
通过RGB值设置背景的颜色。 默认的颜色是 0x000000: // 颜色的参数可以是字符串 "#530000" 或者是十六进制数值 0x530000 controller.setBackgroundColor("#530000); //controller.setBackgroundColor(0x530000);