JFrame 中的透明背景错误
Transperent background in JFrame bug
我正在制作一个简单的 Java 程序。现在它只是一个 8x8 的跳棋场。但是当启动我的 JFrame 程序背景时,我的屏幕照片并没有改变。但是,我将其设置为白色。是代码有问题还是其他什么地方不对?
我把背景设置成白色了,根本没设置。都没有帮助。
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();
}
}
Picture of the programm
The second picture
首先通读 Painting in AWT and Swing and Performing Custom Painting 以更好地了解绘画在 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
}
}
}
}
}
如果你真的想要一个透明的window,那么你应该看看How to Create Translucent and Shaped Windows
我正在制作一个简单的 Java 程序。现在它只是一个 8x8 的跳棋场。但是当启动我的 JFrame 程序背景时,我的屏幕照片并没有改变。但是,我将其设置为白色。是代码有问题还是其他什么地方不对?
我把背景设置成白色了,根本没设置。都没有帮助。
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();
}
}
Picture of the programm
The second picture
首先通读 Painting in AWT and Swing and Performing Custom Painting 以更好地了解绘画在 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
}
}
}
}
}
如果你真的想要一个透明的window,那么你应该看看How to Create Translucent and Shaped Windows