无法在 Java 中为 JPanel 设置颜色
Can't set color to JPanel in Java
我刚开始使用 java 中的图形,但我已经卡住了。我试图将 JPanel 的颜色设置为红色,但似乎没有任何效果!非常感谢任何帮助。
JFrame class:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
public class redBoxFrame{
public static void main(String[]args){
JFrame f = new JFrame();
f.setSize(400, 200);
f.setTitle("A red box");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new redBoxPanel();
p.setBackground(Color.RED);
f.add(p);
f.setVisible(true);
}
}
JPanel class:
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class redBoxPanel extends JPanel {
public void paintComponent(Graphics g){
g.fillRect(0, 0, 100, 100);
g.setColor(Color.RED);
}
}
如您所见,我都尝试在 JFrame class 和 JPanel class 中声明颜色,但其中 none 似乎有效。
谢谢!
我认为你的 painComponent 方法中缺少 super.paintComponent(g);
。
我相信解决方案可以正常工作,就像您在问题中所说的那样,在 JFrame class 和 JPanel class 中设置背景。
如果您从 JFrame class 中删除 setBackground,您应该只会看到您正在绘制的矩形。请尝试以下解决方案,如果可行请告诉我们。
JFrame class:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
public class redBoxFrame{
public static void main(String[]args){
JFrame f = new JFrame();
f.setSize(400, 200);
f.setTitle("A red box");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new redBoxPanel();
f.add(p);
f.setVisible(true);
}
}
JPanel class:
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class redBoxPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.RED);
}
}
这里的每个人似乎都忽略了一个事实,即颜色应该在绘图之前设置。
出于演示目的,我会将主要背景设置为蓝色。
public static void main(String[] args) {
//...
JPanel p = new redBoxPanel();
// BLUE bg. This covers the whole panel.
p.setBackground(Color.BLUE);
//...
}
现在是红框!
public static class redBoxPanel extends JPanel {
@Override public void paintComponent(Graphics g) {
// You need to call the superclass' paintComponent() so that the
// setBackground() you called in main() is painted.
super.paintComponent(g);
// You need to set the colour BEFORE drawing your rect
g.setColor(Color.RED);
// Now that we have a colour, perform the drawing
g.fillRect(0, 0, 100, 100);
// More, for fun
g.setColor(Color.GREEN);
g.drawLine(0, 0, 100, 100);
}
}
我刚开始使用 java 中的图形,但我已经卡住了。我试图将 JPanel 的颜色设置为红色,但似乎没有任何效果!非常感谢任何帮助。
JFrame class:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
public class redBoxFrame{
public static void main(String[]args){
JFrame f = new JFrame();
f.setSize(400, 200);
f.setTitle("A red box");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new redBoxPanel();
p.setBackground(Color.RED);
f.add(p);
f.setVisible(true);
}
}
JPanel class:
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class redBoxPanel extends JPanel {
public void paintComponent(Graphics g){
g.fillRect(0, 0, 100, 100);
g.setColor(Color.RED);
}
}
如您所见,我都尝试在 JFrame class 和 JPanel class 中声明颜色,但其中 none 似乎有效。 谢谢!
我认为你的 painComponent 方法中缺少 super.paintComponent(g);
。
我相信解决方案可以正常工作,就像您在问题中所说的那样,在 JFrame class 和 JPanel class 中设置背景。 如果您从 JFrame class 中删除 setBackground,您应该只会看到您正在绘制的矩形。请尝试以下解决方案,如果可行请告诉我们。
JFrame class:
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Color;
public class redBoxFrame{
public static void main(String[]args){
JFrame f = new JFrame();
f.setSize(400, 200);
f.setTitle("A red box");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new redBoxPanel();
f.add(p);
f.setVisible(true);
}
}
JPanel class:
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
public class redBoxPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(0, 0, 100, 100);
g.setColor(Color.RED);
}
}
这里的每个人似乎都忽略了一个事实,即颜色应该在绘图之前设置。
出于演示目的,我会将主要背景设置为蓝色。
public static void main(String[] args) {
//...
JPanel p = new redBoxPanel();
// BLUE bg. This covers the whole panel.
p.setBackground(Color.BLUE);
//...
}
现在是红框!
public static class redBoxPanel extends JPanel {
@Override public void paintComponent(Graphics g) {
// You need to call the superclass' paintComponent() so that the
// setBackground() you called in main() is painted.
super.paintComponent(g);
// You need to set the colour BEFORE drawing your rect
g.setColor(Color.RED);
// Now that we have a colour, perform the drawing
g.fillRect(0, 0, 100, 100);
// More, for fun
g.setColor(Color.GREEN);
g.drawLine(0, 0, 100, 100);
}
}