首页 > java > JTextArea背景问题

JTextArea背景问题 (JTextArea background issue)

问题

private JDialog dialog;
private JTextArea text;
private JPanel buttons, filler;
private JRadioButton questions, list;
private ButtonGroup group;
private JButton confirm;

dialog = new JDialog(Main.masterWindow, lang.getString("newTitle"), true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
dialog.setResizable(false);

text = new JTextArea();

//this works
text.setBackground(Color.RED);

//this both don't
text.setBackground((Color)UIManager.get("control");
text.setBackground(dialog.getContentPane().getBackground());

dialog.setVisible(true);

我正在使用Nimbus L&F,“control”是我对话框的背景颜色。如果我设置任何其他颜色(在此示例中为红色),则显示,但如果我将其设置为此颜色,则它将保持白色。

我没有默认(金属)L&F的问题...

有什么问题?

解决方法

出于某种原因,它似乎不喜欢ColorUIResourceUIManager.get调用返回的对象。我不明白为什么,因为它来源于Color

如果你做的事情

JDialog dialog = new JDialog((JFrame) null, "Help", true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));

JTextArea text = new JTextArea(10, 10);

Color color = new Color(UIManager.getColor("control").getRGB()); // <-- Create a new color

text.setBackground(bg);

dialog.add(text);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

这似乎有效。

你应该这样做吗?我不这么认为,但我尝试的其他所有事情都没有用

问题

private JDialog dialog;
private JTextArea text;
private JPanel buttons, filler;
private JRadioButton questions, list;
private ButtonGroup group;
private JButton confirm;

dialog = new JDialog(Main.masterWindow, lang.getString("newTitle"), true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
dialog.setResizable(false);

text = new JTextArea();

//this works
text.setBackground(Color.RED);

//this both don't
text.setBackground((Color)UIManager.get("control");
text.setBackground(dialog.getContentPane().getBackground());

dialog.setVisible(true);

I am using Nimbus L&F, and "control" is the background color of my dialog. If I set any other color (red in this example) it shows, but if I set it to this one, it's stays white.

I don't have tis problem on default (metal) L&F...

What's the problem?

解决方法

For some reason, it doesn't seem to like the ColorUIResource object the is returned from UIManager.get call. I can't see why, because it's derived from Color.

If you do something like

JDialog dialog = new JDialog((JFrame) null, "Help", true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));

JTextArea text = new JTextArea(10, 10);

Color color = new Color(UIManager.getColor("control").getRGB()); // <-- Create a new color

text.setBackground(bg);

dialog.add(text);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

This seems to work.

Should you have to do it. I don't think so, but every thing else I tried didn't work

相似信息