图形未绘制/显示 [英] Graphics Not Drawn/Appearing
问题描述
我遇到了这样的问题,即我绘制的对象没有出现在GUI中.我知道它正在处理中,因为数据被推送到日志文件中.但是,该图形没有出现.
I'm having this problem where an object that I've drawn isn't appearing in the GUI. I know it's being processed because data is being pushed to a log file. However, the graphic isn't appearing.
这是我的一些代码:
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.setVisible(true);
}
我在这里和那里放置了一个按钮和其他一些小部件.中央窗格(BorderLayout.CENTER)是显示我的DrawnObject
的地方.
There's a button and a few other widgets that I've placed here and there. The center pane (BorderLayout.CENTER) is where my DrawnObject
is to be displayed.
// Called when button is pushed/clicked
public static void trigger()
{
DrawnObject shape = new DrawnObject();
window.setLayout(new BorderLayout());
window.getContentPane().add(shape, BorderLayout.CENTER);
window.pack;
}
public class DrawnObject extends JComponent()
{
@Override
public Dimension getMinimumSize()
{
return new Dimension(100, 100);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(500, 500);
}
@Override
public Dimension getMaximumSize()
{
return new Dimension(700, 700);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
}
}
我尝试将Graphics
对象转换为Graphics2D
并使用适当的绘制方法,但这没有帮助.
I've tried casting the Graphics
object as Graphics2D
and using the appropriate draw methods, but that hasn't helped.
推荐答案
尝试更改颜色...
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
默认情况下,图形上下文颜色设置为组件背景颜色
The graphics context color is set to the components background color by default
public class PaintTest01 {
public static void main(String[] args) {
new PaintTest01();
}
public PaintTest01() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DrawPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawPane extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(30, 30);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10, 10, 10, 10);
}
}
}
已更新
根据问题中的更新代码,无法编译...
From your updated code in your question, it can't compile...
您在构造函数中创建一个名为JFrame
的窗口,这是一个局部变量...
You create a JFrame
named window in the constructor, which is a local variable...
public static void main(String[] args)
{
JFrame window = new JFrame();
window.setLayout(new BorderLayout());
window.setVisible(true);
}
然后尝试将DrawObject
添加到窗口中...
Then you try and add the DrawObject
to the window...
public static void trigger()
{
DrawnObject shape = new DrawnObject();
window.setLayout(new BorderLayout());
window.getContentPane().add(shape, BorderLayout.CENTER);
window.pack;
}
但是由于window
是未定义的,因此您的示例无法编译.
But because window
is undefined, you example can't compile.
唯一可以编译的方法是,如果您在类级别上有一个名为window
的静态变量,在这种情况下,除非您已初始化该变量,否则它应该生成一个NullPointerException
The only way that this would compile is if you had a static variable at the class level called window
, which in that case, it should be producing a NullPointerException
, unless you've initialised that variable
public class MyDrawing {
public static JFrame window = new JFrame();
这意味着您有两个框架,一个是在构造函数中创建的,另一个是作为静态级别类字段创建的.这是行不通的,因为它们是不同的实例
This would mean you have two frames, one you created in the constructor and one your create as a static level class field. This won't work, because they are different instances
这篇关于图形未绘制/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!