我试图在“图形”类中绘制一个矩形,但由于某种原因该矩形没有出现,但程序没有返回任何错误。我以前从未遇到过这样的问题,所以我很困惑。
主函数()
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main
{
public Main()
{
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(container);
window.setSize(600, 400);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
}
图形()
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(500, 500, 500, 500);
}
}
编辑气垫船
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main
{
public Main()
{
JFrame window = new JFrame();
Sound soundCall = new Sound();
Draw drawCall = new Draw();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(drawCall);
window.setSize(600, 400);
window.setLocationRelativeTo(null);
window.setVisible(true);
window.setResizable(false);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
}
}
通过添加此 window.getContentPane().add(drawCall);
要求我将 drawCall
更改为组件
编辑2:
public class Draw
{
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
}
}
错误
Container 类型中的方法 add(Component) 不适用于参数 (Draw)
最佳答案
您将graphicsCall 变量添加为空,因此它不会显示。解决方案:将其添加到容器(例如您刚刚创建的 JPanel)中,或者直接添加到 JFrame 的 contentPane 中。
即更改此:
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(container);
对此:
JFrame window = new JFrame();
Sound soundCall = new Sound();
Graphics graphicsCall = new Graphics();
// final JPanel container = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(soundCall);
顺便说一句,您需要将该类从 Graphics 重命名为其他名称,否则您或您的编译器可能会感到困惑,因为已经存在一个具有该名称的关键 Java 类。
此外,避免使用setSize(...)
。最好让您的绘图 JPanel 重写 getPreferredSize()
并在 JFrame 上调用 pack()
。
编辑
根据 MadProgrammer 敏锐的观察,您正在组件的边界之外进行绘制。
编辑2
关于您的最新代码,如下:
public class Draw
{
public class Graphics extends JPanel
{
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
}
}
真是没用的废物。为什么你不必要地将一个类包装在一个类中?相反,为什么不简单地:
public class Draw extends JPanel {
public void paintComponent(java.awt.Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
}
@Override
public Dimension getPreferredSize() {
// return an appropriate Dimension here
}
}
关于java - 为什么这个图形组件不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716925/