我有一个带有图形用户界面的程序,在JFrame上设置了以下内容:
setBackground( new Color(107, 106, 104) );
问题在于我得到的是灰色调,而不是正确的颜色。如果我在PhotoShop中检查它,它会给我RGB值(126, 125, 123)。
附:我已尝试使用HEX值,但结果相同。
我有一个带有图形用户界面的程序,在JFrame上设置了以下内容:
setBackground( new Color(107, 106, 104) );
I have a program with some gui, on the JFrame I set,
setBackground( new Color(107, 106, 104) );
[The problem] It gives a greyish color, but not the right one!
If I check the gui's color in Photo Shop, it gives me the RGB
values (126, 125, 123)
在JFrame
中不能使用setBackground
方法,该方法仅适用于ContentPane
。例如:
JFrame#getContentPane.setBackground(new Color(107, 106, 104));
编辑
来自代码
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Check extends JFrame {
private static final long serialVersionUID = 1L;
public void makeUI() {
JFrame f = new JFrame();
f.getContentPane().setBackground(new Color(107, 106, 104));
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setSize(new Dimension(300, 200));
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Check().makeUI();
}
});
}
}
查看Adam的评论,即使没有起作用,那么在没有任何工作代码的情况下,我只是猜测这种情况是由于零订购或JFrame的布局而引发的。 实际上,在Java Swing中,设置背景颜色需要更多的注意,可以查看Swing Java文档。
import java.awt.*;
import javax.swing.*;
public class Tmp extends Frame { public static void main(String[] args) {
//Frame tmp = new Frame();
Frame tmp = new JFrame();
tmp.setBackground(new Color(107, 106, 104));
tmp.setSize(40,40);
tmp.setVisible(true);
}}
jFrame
对象:JFrame frame = new JFrame();
第二步:
frame.getContentPane().setBackground(new Color(16,144,144));
这对我有用。 希望能帮到你 这段代码将一个JPanel添加到当前的JFrame中,您可以在此面板上进一步构建GUI。您可以自定义JPanel上的RGB颜色,而不是JFrame。
import javax.swing.*;
import java.awt.*;
public class Main{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
//Class class = new Class();
frame.setSize(1920,1080);
//frame.setTitle("XYZ");
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setBackground(new Color(51,153,255));
//panel.add(class);
}
}
if(evt.getSource() == jMenuItem11){
getContentPane().setBackground(new Color(170, 8, 54));
}
if(evt.getSource() == jMenuItem12){
getContentPane().setBackground(new Color(8, 54, 169));
}
if(evt.getSource() == jMenuItem13){
getContentPane().setBackground(new Color(84, 8, 170));
}
}
http://www.tayloredmktg.com/rgb/
当你打开页面时,灰色似乎在页面顶部。 :) 同时确保你的JFrame是不透明的,否则你看不到你的颜色!
setOpaque(true);