将组件添加到窗口

时间:2014-09-07 21:52:03

标签: java swing class jframe components

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(组件)上收到错误;每当我尝试运行它。

1 个答案:

答案 0 :(得分:0)

GraphicsWindow中,您必须延长javax.swing.JComponent而不是BlankWindow

来源:http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html