基本上,我已经创建了两个类;Main 和 JFrameOptions。但是,似乎我无法绘制背景,无论我使用JLabels,setContentPane还是setBackground,所有这些都不起作用。我做错了什么?
主要:
package game;
public class Main{
public static void main(String[] args) {
JFrameOptions.Options();
TSDTDir.Directory();
}
}
JFrameOptions:
package game;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
public class JFrameOptions{
static String setTitle = ("Game");;
public static int width = 920;
public static int height = 517;
public static void Options() {
JFrame window = new JFrame(setTitle);
window.setSize(width, height);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.toFront();
window.setBackground(Color.black);
}
}
编辑:
我通过你的答案得到了最基本的东西:window.getContentPane(). setbackground(Color.PINK);
但是,如何加载必须具有登录字段的图像?
编辑2:它不起作用。背景不绘制。
public static void Options() {
//Displays the title
JFrame window = new JFrame(setTitle);
window.setSize(width, (int) height);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setIconImage(favIcon);
window.setResizable(false);
window.setLocationRelativeTo(null);
window.toFront();
//window.getContentPane().setBackground(Color.black);
BufferedImage img = null;
try {
img = ImageIO.read(new File("Background.png"));
window.setContentPane(new JLabel(new ImageIcon(img)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
当然Background.png文件在游戏文件夹的目录中:http://prntscr.com/9kxc4i
这是一个常见的问题,您在建立UI之前调用了setViable
。Swing在布局UI更改时很懒,需要您决定何时应该更新UI,这是经过深思熟虑的,并且是为了保持效率(想象一下向UI添加十几个新组件,您不希望UI在完成之前重新验证和更新,否则您将浪费大量时间)
简单的解决方案是,最后调用< code>setVisible...
public static void Options() {
//Displays the title
JFrame window = new JFrame(setTitle);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setIconImage(favIcon);
//window.getContentPane().setBackground(Color.black);
BufferedImage img = null;
try {
img = ImageIO.read(new File("Background.png"));
window.setContentPane(new JLabel(new ImageIcon(img)));
} catch (IOException e) {
e.printStackTrace();
}
window.setResizable(false);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
window.toFront();
}
但是请记住,< code>JLabel在默认情况下没有布局管理器,即使您应用了布局管理器,它也不会使用它来计算其< code>preferredSize,而只依赖于< code>icon和< code>text属性。
看看这个讨论更多的细节和选择
通过RGB值设置背景的颜色。 默认的颜色是 0x000000: // 颜色的参数可以是字符串 "#530000" 或者是十六进制数值 0x530000 controller.setBackgroundColor("#530000); //controller.setBackgroundColor(0x530000);
问题内容: 当我尝试使用以下命令在Vim 中或直接在Vim中更改背景色时: …完全不影响我的背景。该选项也没有。但是,当我运行gvim时看起来还可以。 有没有一种方法可以在Vim中更改背景而不更改我的Konsole设置? 编辑 好的,guifg / guibg和ctermfg / ctermbg之间是有区别的。虽然GUI接受许多不同的颜色组合,但是cterm仅允许很少的标准颜色组合。 问题答案:
我想在XSSFWorkbook中设置自己的背景色。到目前为止,我有这个代码: 但是我得到了以下错误: 类型CellStyle中的setFillForegroundColor(short)方法不是 不推荐使用构造函数XSSFColor(Color) 那么如何在XSSFWorkbook中设置自己的颜色呢?
嘿,我正在尝试做一个程序,它加载一个彩色图像作为灰度画布,然后返回颜色点击像素。当setrgb()方法没有做它应该做的事情时,我就被困在这里了。我已经通过getRGB()从原始图像中复制了颜色,并使用setRGB()将其分配给新图像。我试图输出两个像素颜色值,但他们不一样。请帮我解决这个问题。以下是到目前为止的代码:
问题内容: 假设我要在CSS中渲染箭头,箭头应具有头部,尾部和灵活的宽度,以便可以包含文本。我当然可以创建多个div来获得所需的内容,但是如何在CSS3中完成呢? 我可以使用多个背景图片: 的HTML: 这给了我一个带有箭头和尾巴的透明中间部分。似乎不可能在中间部分指定颜色。 仅使用一张背景图像,您可以执行以下操作: 我知道这在很多方面都是可行的,但是背景颜色属性是否真的从速记定义中丢失了? 问题
因此,我尝试使用getContentPane().setBackground(color.white)并尝试将table和scrollpane设置为白色。 这是唯一一个我不能改变颜色的框架,它是在另一个类中创建的- 通过这样做,我得到了另一个面板来成功地改变颜色