Pessoal, boa tarde.
Fiz o seguinte exercicio do Use a Cabeça, Java!
Pórem, não está sendo exibido desenho algum no JFrame. Não deveria exibir algo que foi desenhado na classe MyDraw?
Segue abaixo o código completo, mostrado no livro.
public class MyDraw extends JPanel {
public void ComponentGraphical(Graphics G){
G.setColor(Color.orange);
G.fillRect(20, 50, 100, 100);
}
}
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Principal implements ActionListener {
JFrame frame;
public static void main(String[] args){
Principal Prin = new Principal();
Prin.Go();
}
public void Go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Change Collor");
button.addActionListener(this);
MyDraw Draw = new MyDraw();
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.getContentPane().add(BorderLayout.CENTER, Draw);
frame.setSize(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
frame.repaint();
}
}