package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class gui7 implements ActionListener {
JFrame frame=new JFrame();
JButton button=new JButton("Click me");
public static void main(String args[]) {
gui7 a=new gui7();
a.go();
}
public void actionPerformed(ActionEvent event) {
frame.repaint(); //to call paintcomponent
}
public void go() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new gui7());
gui8 f=new gui8();
frame.getContentPane().add(BorderLayout.SOUTH,button);
frame.getContentPane().add(BorderLayout.CENTER,f);
frame.setSize(300,300);
frame.setVisible(true);
}
}
package gui;
import java.awt.*;
import javax.swing.*;
public class gui8 extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(0, 0, this.getWidth(),this.getHeight());
// код для создания случайного цвета
int red=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
Color rand=new Color(red,blue,green);
g.setColor(rand);
g.fillOval(70, 70, 100, 100);
}
}