将JPanel和paintComponent链接在一起

时间:2012-12-04 23:40:59

标签: java swing jpanel actionlistener paintcomponent

如何让paintComponent在drawArea JPanel中工作?此外,我尝试设置drawArea的尺寸失败。一旦运行就不会出现绘图,并且JPanel的大小最小。这与MigLayout有关吗?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.layout.*;
import net.miginfocom.swing.MigLayout;
import java.awt.geom.*;

public class OvalDrawer extends JApplet
{
private JLabel numberL;
private JTextField numberTF;

private NumHandler numHandler;

public static final int WIDTH = 500;
public static final int HEIGHT = 500;

public int number;

//Create Layout
public void init()
{
    setLayout(new MigLayout("wrap 2"));
    numberL = new JLabel("Enter number of ovals to draw:");
    numberTF = new JTextField(7);

    add(numberL);
    add(numberTF);

    numHandler = new NumHandler();
    numberTF.addActionListener(numHandler);

    JPanel drawArea = new JPanel();
    drawArea.setSize(400, 400);
    drawArea.setBorder(BorderFactory.createTitledBorder("Draw Area"));
    add(drawArea, "span 2");

    setSize(WIDTH, HEIGHT);
}

//Event Handler
public class NumHandler implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        number = Integer.parseInt(numberTF.getText());
        repaint();
    }

}

//Draw Ovals
public void paintComponent (Graphics g)
{
    super.paintComponents(g);
    int x = 10;
    int y = 10;
    int width = 200;
    int height = 100;

    for (int i = 0; i < number; i++)
    {
        g.drawOval(x, y, width, height);

        x += 5;
        y += 5;
        width += 5;
        height += 5;
    }
}
}

1 个答案:

答案 0 :(得分:2)

paintComponent中没有JApplet方法。

你应该避免直接绘制到像JApplet这样的顶级容器上,而不仅仅是单层容器。

相反,创建第二个类,从JPanel扩展并在那里执行自定义绘制。

<强>已更新

你也应该避免使用setPreferred/Minimum/MaximumSize,这对其他人来说很容易规避。

public class OvalDrawer extends JApplet {

    private JLabel numberL;
    private JTextField numberTF;
    private NumHandler numHandler;
    public static final int WIDTH = 500;
    public static final int HEIGHT = 500;
    public int number;

    //Create Layout
    public void init() {
//        setLayout(new MigLayout("wrap 2"));
        setLayout(new BorderLayout());
        numberL = new JLabel("Enter number of ovals to draw:");
        numberTF = new JTextField(7);

        JPanel header = new JPanel();
        header.add(numberL);
        header.add(numberTF);

        add(header, BorderLayout.NORTH);
        add(new PaintPane());

        numHandler = new NumHandler();
        numberTF.addActionListener(numHandler);

//        JPanel drawArea = new JPanel();
//        drawArea.setSize(400, 400);
//        drawArea.setBorder(BorderFactory.createTitledBorder("Draw Area"));
//        add(drawArea, "span 2");

        setSize(WIDTH, HEIGHT);
    }

    //Event Handler
    public class NumHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            number = Integer.parseInt(numberTF.getText());
            repaint();
        }
    }

    //Draw Ovals
    public class PaintPane extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = 10;
            int y = 10;
            int width = 200;
            int height = 100;

            for (int i = 0; i < number; i++) {
                g.drawOval(x, y, width, height);

                x += 5;
                y += 5;
                width += 5;
                height += 5;
            }
        }
    }
}