Java JFrame bug中的透明背景
我正在制作一个简单的Java程序。现在它只生成一个8x8跳棋字段。但是,当启动我的程序时,我的JFrame的背景像会为我的屏幕生成一张照片,并且不会改变。但是,我将其设置为白色。是代码有问题还是其他什么地方出了问题 我将背景设置为白色,但根本没有设置。两者都没有帮助Java JFrame bug中的透明背景,java,swing,jframe,paint,Java,Swing,Jframe,Paint,我正在制作一个简单的Java程序。现在它只生成一个8x8跳棋字段。但是,当启动我的程序时,我的JFrame的背景像会为我的屏幕生成一张照片,并且不会改变。但是,我将其设置为白色。是代码有问题还是其他什么地方出了问题 我将背景设置为白色,但根本没有设置。两者都没有帮助 package mainpackage; import com.sun.prism.paint.*; import javax.swing.*; import java.awt.*; import java.awt.Color;
package mainpackage;
import com.sun.prism.paint.*;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.io.File;
class Field extends JFrame {
Field() //little constructor with no arguments
{
this(800, 800);
}
Field(int a, int b) //the most big constructor
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBackground(Color.white);
setLayout(new FlowLayout());
setResizable(false);
setSize(a, b);
}
void makeVisible() //making frame visible
{
setVisible(true);
}
public void makeInvisible() //making frame invisible
{
setVisible(false);
}
public void paint(Graphics g) //painting the board
{
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
g.setColor(getCurrentColor(x,y)); // get and set color of current square
{
if (x % 2 == 0) // if x even
{
if (y % 2 == 0) {
g.setColor(Color.lightGray); // if x even y even make gray
} else {
g.setColor(Color.pink); // if x even and y odd make pink
}
} else { // if odd even
if (y % 2 == 0) {
g.setColor(Color.pink); // if x odd and y even make pink
} else {
g.setColor(Color.lightGray); // if x odd and y odd make gray
}
}
}
if(x>0&&x<9&&y>0&&y<9) { // only if 0 < x < 9 and 0 < y < 9
g.fillRect(50 + 60 * x, 50 + 60 * y, 60, 60); // fill current rectangle
}
else if(y==0&&x>0&&x<9)
{
g.setColor(Color.black);
g.drawString(Integer.toString(x),50 + 60 * x, 50 + 60 * y);
}
/* if (y == 2&& x== 1) {
*//* JLabel label = new JLabel();
label.setLocation(x*60, y*60);
label.setSize(60, 60);
label.setLayout(new GridBagLayout());
label.setIcon(new ImageIcon("C:/Users/Asus/Desktop/My Work/checkers/res/images/checkerimage.png"));
add(label);*//*
}*/
}
}
}
private Color getCurrentColor(int x, int y) //set the color of current square
{
if (x % 2 == 0) // if x even
{
if (y % 2 == 0) {
return Color.red;
} else {
return Color.blue;// if x even and y odd make blue
}
} else { // if odd even
if (y % 2 == 0) {
return Color.blue; // if x odd and y even make blue
} else {
return Color.red; // if x odd and y odd make red
}
}
}
}
package mainpackage;
public class MainClass {
public static void main(String[] args) throws InterruptedException {
Field f1 = new Field();
f1.makeVisible();
}
}
包mainpackage;
导入com.sun.prism.paint.*;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.Color;
导入java.io.File;
类字段扩展JFrame{
Field()//没有参数的小构造函数
{
这(800800);
}
字段(inta,intb)//最大的构造函数
{
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
挫折地面(颜色:白色);
setLayout(新的FlowLayout());
可设置大小(假);
设置大小(a,b);
}
void makeVisible()//使框架可见
{
setVisible(真);
}
public void makeInvisible()//使框架不可见
{
setVisible(假);
}
公共空间绘制(图形g)//绘制电路板
{
对于(int y=0;y<10;y++){
对于(int x=0;x<10;x++){
g、 setColor(getCurrentColor(x,y));//获取并设置当前正方形的颜色
{
if(x%2==0)//如果x为偶数
{
如果(y%2==0){
g、 setColor(Color.lightGray);//如果x甚至y甚至变成灰色
}否则{
g、 setColor(Color.pink);//如果x偶数和y奇数为粉红色
}
}else{//如果奇偶
如果(y%2==0){
g、 setColor(Color.pink);//如果x奇数和y偶数为粉红色
}否则{
g、 setColor(Color.lightGray);//如果x奇数和y奇数为灰色
}
}
}
如果(x>0&&x0&&y0&&x开始,请通读一遍,以便更好地理解Swing中的绘画工作方式以及您应该如何使用它
两个基本问题:
JFrame的覆盖绘画
中断绘制链(未能调用paint
方法的super
方法)
JFrame
是一个复合组件,也就是说,它已经添加了许多组件,这些组件构成了它的功能,这可能会干扰通过paint
绘制的内容
绘制是一个复杂的过程,涉及许多步骤,通过一系列子方法委派。除非您想接管所有责任,否则在执行任何自定义绘制之前,您应该调用super.paintXxx
那么,答案是什么?双重的
- 用户
JPanel
而不是JFrame
作为组件的基础
- 覆盖
paintComponent
而不是paint
(并在执行任何自定义绘制之前调用super.paintComponent
)
例如
import java.awt.*;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test extends JFrame {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.getContentPane().setBackground(Color.RED);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Field());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Field extends JPanel {
public Field() {
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
g2d.setColor(getCurrentColor(x, y)); // get and set color of current square
{
if (x % 2 == 0) // if x even
{
if (y % 2 == 0) {
g2d.setColor(Color.lightGray); // if x even y even make gray
} else {
g2d.setColor(Color.pink); // if x even and y odd make pink
}
} else { // if odd even
if (y % 2 == 0) {
g2d.setColor(Color.pink); // if x odd and y even make pink
} else {
g2d.setColor(Color.lightGray); // if x odd and y odd make gray
}
}
}
if (x > 0 && x < 9 && y > 0 && y < 9) { // only if 0 < x < 9 and 0 < y < 9
g2d.fillRect(50 + 60 * x, 50 + 60 * y, 60, 60); // fill current rectangle
} else if (y == 0 && x > 0 && x < 9) {
g2d.setColor(Color.black);
g2d.drawString(Integer.toString(x), 50 + 60 * x, 50 + 60 * y);
}
/* if (y == 2&& x== 1) {
*//* JLabel label = new JLabel();
label.setLocation(x*60, y*60);
label.setSize(60, 60);
label.setLayout(new GridBagLayout());
label.setIcon(new ImageIcon("C:/Users/Asus/Desktop/My Work/checkers/res/images/checkerimage.png"));
add(label);*//*
}*/
}
}
g2d.dispose();
}
private Color getCurrentColor(int x, int y) //set the color of current square
{
if (x % 2 == 0) // if x even
{
if (y % 2 == 0) {
return Color.red;
} else {
return Color.blue;// if x even and y odd make blue
}
} else { // if odd even
if (y % 2 == 0) {
return Color.blue; // if x odd and y even make blue
} else {
return Color.red; // if x odd and y odd make red
}
}
}
}
}
import java.awt.*;
导入java.awt.Color;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类测试扩展了JFrame{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.getContentPane().setBackground(颜色为.RED);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新字段());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类字段扩展了JPanel{
公共领域(){
设置不透明(假);
}
@凌驾
公共维度getPreferredSize(){
返回新维度(800600);
}
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
对于(int y=0;y<10;y++){
对于(int x=0;x<10;x++){
g2d.setColor(getCurrentColor(x,y));//获取并设置当前方块的颜色
{
if(x%2==0)//如果x为偶数
{
如果(y%2==0){
g2d.setColor(Color.lightGray);//如果x偶数y偶数为灰色
}否则{
g2d.setColor(Color.pink);//如果x偶数和y奇数为粉红色
}
}else{//如果奇偶
如果(y%2==0){
g2d.setColor(Color.pink);//如果x奇数和y偶数为粉红色
}否则{
g2d.setColor(Color.lightGray);//如果x奇数和y奇数为灰色
}
}
}
如果(x>0&&x<9&&y>0&&y<9){//仅当00&&x<9),则为else{
g2d.setColor(Color.black);
g2d.drawString(Integer.toString)