2

JPanelを拡張したクラスをフレームに追加してボタンを追加するという簡単なGUIアプリケーションを実装しようとしましたが、ボタンをクリックしても何も起こりません。何が問題なのですか?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class dup extends JPanel {

    public void paintComponent(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.green);

        g2d.fillRect(0, 0, this.WIDTH, this.HEIGHT);
        System.out.println("inside paint component class");
    }
}

public class drawing implements ActionListener {
    JFrame frame;
    dup d1;

    public static void main(String args[]) {
        drawing d2 = new drawing();
        d2.go();
    }

    public void go() {
        frame = new JFrame();
        JButton button = new JButton("click me");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        d1 = new dup();
        button.addActionListener(this);

        frame.getContentPane().add(BorderLayout.WEST, button);
        frame.getContentPane().add(BorderLayout.CENTER, d1);
        frame.setSize(300, 300);
        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent ae) {
        frame.repaint();

    }
}

これの何が問題なのですか?

4

1 に答える 1