问题描述
这些是文件。我已将 JFrame 设置为可见,并向其中添加了 JPanel,但代码仍然只显示窗口,其中没有任何内容。
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Collections;
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(350,300);
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
frame.setVisible(true);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.setVisible(true);
}
-------------绘图板文件-------------------
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
public void painting(Graphics pen) {
pen.drawRect(50,50,20,20);
pen.drawRect(100,40,20);
pen.drawoval(200,20);
pen.drawoval(250,20);
pen.drawString("Square",90);
pen.drawString("Rectangle",100,90);
pen.drawString("Cirlce",200,90);
pen.drawString("oval",250,90);
pen.fillRect(50,20);
pen.fillRect(100,20);
pen.filloval(250,20);
pen.drawLine(50,150,300,150);
pen.drawArc(50,180);
pen.fillArc(100,175,75,90,45);
}
}
解决方法
尝试将 DrawingPanel
中的方法从 painting
更改为 paint
,它会在运行时被调用。 paint
是从 JPanel
继承的方法。
编辑:如 NomadMaker 所述,此处使用 paintComponent()
而不是 paint()
。阅读this了解更多信息。
这是使您的代码可运行、修复您的 JFrame
方法调用和修复您的绘图 JPanel
后我得到的结果。
Swing 应用程序应始终以调用 SwingUtilities
invokeLater
方法开始。此方法可确保在 Event Dispatch Thread 上创建和执行 Swing 组件。
您打包了一个 JFrame
。您设置绘图的首选大小 JPanel
。这样,您就可以知道您的绘图 JPanel
有多大,而无需担心 JFrame
装饰。
这是完整的可运行代码。
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawingPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawingPanelExample());
}
@Override
public void run() {
JFrame frame = new JFrame("My Empty Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel panel = new DrawingPanel();
frame.add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(350,300));
}
@Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.drawRect(50,50,20,20);
pen.drawRect(100,40,20);
pen.drawOval(200,20);
pen.drawOval(250,20);
pen.drawString("Square",90);
pen.drawString("Rectangle",100,90);
pen.drawString("Cirlce",200,90);
pen.drawString("Oval",250,90);
pen.fillRect(50,20);
pen.fillRect(100,20);
pen.fillOval(250,20);
pen.drawLine(50,150,300,150);
pen.drawArc(50,180);
pen.fillArc(100,175,75,90,45);
}
}
}
,
你应该像这样覆盖paintComponent:
...
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D pen = (Graphics2D) g;
...
}
还有一些建议:
- 您可以扩展
JComponent
而不是JPanel
(应该双向工作) - 您可以使用
setSize
或setPreferredSize
作为您的panel
以适应您的框架尺寸。 - 在完成所有框架配置后,您只能使用
setVisisble(true);
一次。 - 并将其添加到框架的中心,如下所示:
...
JFrame frame = new JFrame();
frame.setSize(350,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("My Empty Window");
DrawingPanel panel = new DrawingPanel();
panel.setPreferredSize(new Dimensions(350,300));
frame.add(panel,BorderLayout.CENTER);
frame.setVisible(true);
...
附注:
可能不需要添加布局管理器,您也可以像这样将 setPreferredSize
替换为 setBounds
:
panel.setBounds(0,350,300);
frame.add(panel);
其中 0 分别是 x 和 y 坐标。