GraphicsWindow类:
import java.awt.*;
import java.awt.geom.*;
public class GraphicsWindow extends BlankWindow
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.ORANGE);
}
}
BlankWindow Class(main):
import java.awt.*;
import javax.swing.*;
import java.awt.Container;
public class BlankWindow extends JFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Picture");
JPanel pane = new JPanel();
frame.setSize(400,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
GraphicsWindow component = new GraphicsWindow();
frame.add(pane);
pane.add(component);
}
}
我在pane.add(组件)上收到错误;每当我尝试运行它。
答案 0 :(得分:0)
在GraphicsWindow
中,您必须延长javax.swing.JComponent
而不是BlankWindow
来源:http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html