@novavava (直裕 野畑)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

java graphics gを使ったプログラムにおいてパネルにボタンを貼りたい!

解決したいこと

java graphics g は通常JPanelにaddされているはずですが
JPanelを定義しているソースが見当たらないのでJButtonにはadd
できませんどうすればいいですか?

該当するソースコード

package study;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class Study{
    public static void main(String[] args) {
        GameWindow gw = new GameWindow("テストプログラム",400,300);
        gw.add(new Draw());
        gw.setVisible(true);
    }
}

//ウィンドウクラス
class GameWindow extends JFrame{
    public GameWindow(String title, int width, int height) {
        super(title);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(width,height);
        setLocationRelativeTo(null);
        setResizable(true);
    }
}

class Draw extends JPanel{
    LinearGradientPaint p = new LinearGradientPaint(
            0
            , 0
            , 300
            , 200
            , new float[] {
                    0.0f
                    , 0.5f
                    , 1.0f
            }
            , new Color[] {
                    new Color(255, 0, 0)
                    , new Color(0, 255, 0, 100)
                    , Color.BLUE
            }
    );
    public void paintComponent(Graphics g){
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;

        g2.setPaint(p);

        g.fillOval(0, 0, 300, 200);

    }

    class Button {
        JButton Button = new JButton("OK");
    }

}

自分で試したこと

特にないです。

0 likes

No Answers yet.

Your answer might help someone💌