设置 JPanel 颜色的问题
这是我扩展 JPanel 的画布类:
package start;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Board extends JPanel
{
private static final long serialVersionUID = 1L;
public Board() {}
public void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
this.setBackground(Color.green);
g.setColor(Color.black);
g.drawOval(0, 0, width, height);
}
}
这是我调用它的方法:
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Hello");
frame.setPreferredSize(new Dimension(700, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board b = new Board();
frame.getContentPane().add(b);
frame.pack();
frame.setVisible(true);
}
但这显示了默认颜色的椭圆形。我也尝试过不使用 this.
,然后尝试设置 b
的颜色,并在构造函数内设置颜色,但这些都不起作用。怎么了?
编辑:抱歉没有说清楚,我的目标是在绿色背景上显示一个细黑色椭圆形。
Here's my canvas class extending JPanel
:
package start;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Board extends JPanel
{
private static final long serialVersionUID = 1L;
public Board() {}
public void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
this.setBackground(Color.green);
g.setColor(Color.black);
g.drawOval(0, 0, width, height);
}
}
Here's the method where I'm calling it:
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Hello");
frame.setPreferredSize(new Dimension(700, 700));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Board b = new Board();
frame.getContentPane().add(b);
frame.pack();
frame.setVisible(true);
}
But this shows the oval on the default colour. I also tried without the this.
, and then tried setting the colour of b
, and setting the colour inside the constructor, but none of these worked. What's wrong?
EDIT: Sorry for not making thing clear, my goal was to display a thin black oval on a green background.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在paintComponent 方法中,您不必使用setBackground 来更改JPanel 的颜色。这应该在paintComponent 之外完成。在更改背景颜色之前,paintComponent 可能会使用背景颜色。
您可以尝试很多事情。一是在构造函数中设置颜色,然后首先调用超类的paintComponent,如下所示:
另请注意,颜色常量均为大写。即黑色或绿色。
如果您想动态更改背景颜色,则可以在事件处理程序中执行此操作,例如 mouseEntered 或 actionPerformed 等。
In the paintComponent method you do not have to use setBackground to change the colour of the JPanel. That should be done outside of paintComponent. paintComponent will probably use the colour of the background before you change it.
There are a number of things you can try. One, is to set the colour in the constructor and then call the super class' paintComponent first like this:
Also note the color constants are all upper case. i.e. BLACK or GREEN.
If you want to change the background colour dynamically then you can do so in the event handler such as mouseEntered or actionPerformed etc.
虽然代码没有完全清楚地表明您的意图是什么,但我尝试解决一些问题:
如果您想要绿色背景,请按照@vincent 告诉您的操作。您应该看到绿色背景中的黑色椭圆形。如果面板不透明,“super.paintComponent”将自动用组件背景填充其区域。
如果你想要白色背景上的绿色椭圆形,也许有黑色边框
编辑
我忘了超级
While the code does not exactly make clear what's your intent i try to fix some issues:
If you want a green background, do as @vincent told you. YOu should see a black oval in green background. The "super.paintComponent" will fill its area with the components background automatically if the panel is opaque.
If you want a green oval on white background, maybe with black border
EDIT
i forgot super