我刚刚开始学习 Java,最近正在学习控制台之外的新东西。我从教授的董事会复制了代码,但我似乎无法让它工作。现在应该绘制圆形和矩形,但当我尝试绘画时,我只是得到一个空白窗口。我想我可能错过了一行代码或其他东西。我在 Mac 上使用 Eclipse。
我知道可能有很多方法可以做到这一点,但我想保持代码目前的样子,此外还有您认为可以使其工作的任何修复。非常感谢。
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Bunny {
public static void main(String [] args)
{
MyFrame f = new MyFrame();
f.setSize(500,400);
f.setVisible(true);
f.setLocation(50,100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
}
}
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double Circle = new Ellipse2D.Double(100,50,75,75);
g2.setColor(Color.RED);
g2.fill(Circle);
Rectangle box = new Rectangle(200,100,150,150);
g2.setColor(Color.RED);
g2.fill(box);
Color myColor = new Color(255,0,0);
}
}
最佳答案
我非常想告诉你的教授,他们需要回到学校学习如何正确使用 Swing
您应该首先查看 Painting in AWT and Swing和 Performing Custom Painting有关 Swing 中绘画工作原理的更多详细信息
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
Ellipse2D.Double Circle = new Ellipse2D.Double(100, 50, 75, 75);
g2.setColor(Color.RED);
g2.fill(Circle);
Rectangle box = new Rectangle(200, 100, 150, 150);
g2.setColor(Color.RED);
g2.fill(box);
g2.dispose();
}
}
}
代码审查...
public class Bunny {
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setSize(500, 400);
f.setVisible(true);
f.setLocation(50, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
不好:从事件调度线程的上下文之外与 UI 进行交互。请参阅Initial Threads了解详情
public class MyFrame extends JFrame {
public MyFrame() {
}
}
坏:从顶级容器(如JFrame
)扩展通常是不鼓励的,您不会向类添加任何新功能,这是最容易出现问题的领域之一。您可能想对“组合优于继承”进行一些研究。
您实际上也没有向框架添加任何内容,因此它将显示为空白。
public class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double Circle = new Ellipse2D.Double(100, 50, 75, 75);
g2.setColor(Color.RED);
g2.fill(Circle);
Rectangle box = new Rectangle(200, 100, 150, 150);
g2.setColor(Color.RED);
g2.fill(box);
Color myColor = new Color(255, 0, 0);
}
}
不好:paintComponent
永远不应该是 public
,任何其他类都没有理由调用此方法。不调用 super.paintComponent 会导致无休止的图形故障和问题,您应该始终首先调用 super.paintComponent 。唯一不这样做的时候是当您知道自己在做什么并且您有充分的理由不这样做时...大约<1% 的时间。
关于JPanel 中的 Java 代码绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36660618/