自定义 JComponent 未显示在自定义 JPanel 中
我尝试过 add() 方法,但当我尝试将 Test 添加到 GraphicsTest 时,没有显示任何内容。我应该如何添加它?有人可以告诉我吗?我已经包含了我正在使用的代码。
这是我的方法,但行不通。有人可以告诉我或让我意识到问题到底是什么吗?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class Test extends JComponent
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.red);
g2d.drawString("Hello", 50, 50);
g2d.dispose();
}
}
这是另一个类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class GraphicsTest extends JPanel
{
private Graphics2D g2d;
private String state;
private int x, y;
GraphicsTest()
{
Test t = new Test();
t.setVisible(true);
add(t);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawString("STATE: " + state, 5, 15);
g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);
g2d.setColor(Color.red);
Rectangle2D r2d = new Rectangle2D.Double(x, y, 10, 10);
g2d.draw(r2d);
g2d.dispose();
}
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public void setX(int x) { this.x = x; repaint(); }
public void setY(int y) { this.y = y; repaint(); }
}
I've tried the add() method but nothing is displayed when I try to add Test to GraphicsTest. How should I be adding it? Can someone show me? I've included the code I'm using.
This is my way and it's not working. Can someone show me or make me aware of what the problem actually is?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class Test extends JComponent
{
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.red);
g2d.drawString("Hello", 50, 50);
g2d.dispose();
}
}
Here is the other class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class GraphicsTest extends JPanel
{
private Graphics2D g2d;
private String state;
private int x, y;
GraphicsTest()
{
Test t = new Test();
t.setVisible(true);
add(t);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.drawString("STATE: " + state, 5, 15);
g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);
g2d.setColor(Color.red);
Rectangle2D r2d = new Rectangle2D.Double(x, y, 10, 10);
g2d.draw(r2d);
g2d.dispose();
}
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public void setX(int x) { this.x = x; repaint(); }
public void setY(int y) { this.y = y; repaint(); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题是 g2d.dispose()。把它们拿出来,它应该可以工作。我怀疑这可能会导致不同 jvm 上的不同行为。看来正在发生的事情是 g2d 对象用于在 GraphicsTest 对象上绘制内容。然后,同一个 g2d 对象尝试在 Test 对象上绘制内容,但它已被释放,因此无法绘制任何内容。在某些情况下,您想调用 g2d.dispose() 但这不是其中之一。
为了使您的代码正常工作,我所做的另一件事是更改了布局管理器:
默认布局应该是流布局。我立即不确定为什么它不能与流布局一起使用。
Your problem is g2d.dispose(). Take those out and it should work. I suspect this may cause different behavior on different jvms. It appears what is happening is the g2d object is used to draw stuff on the GraphicsTest object. Then the same g2d object tries to draw stuff on on the Test object, but it has been disposed so it can't draw anything. There are cases where you want to call g2d.dispose() but this is not one of them.
The other thing I did to make your code work was I changed the layout manager:
The default layout should be Flow Layout. I'm not sure off hand why it wouldn't work with Flow Layout.