1

drawArea JPanel内でpaintComponentを機能させるにはどうすればよいですか?さらに、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;
    }
}
}
4

1 に答える 1

2

paintComponentにメソッドはありませんJApplet

のようなトップレベルのコンテナに直接ペイントすることは避けてくださいJApplet。単層のコンテナだけです。

代わりに、何かから拡張して2番目のクラスを作成し、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;
            }
        }
    }
}
于 2012-12-04T23:51:44.763 に答える