witam....
Zrobiłem sobie coś takiego :
class Main extends JFrame {
JButton but = new JButton("Click");
int a = 1;
JPanel panel = new Pan(a);
public Main() {
setLayout(new GridLayout(0, 1));
add(panel);
add(but);
but.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a++;
panel.revalidate();
panel.repaint();
}
});
}
class Pan extends JPanel {
int cnt;
public Pan(int cnt) {
this.cnt = cnt;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
switch (cnt) {
case 1:
g.setColor(Color.red);
g.drawLine(0, 0, 50, 50);
break;
case 2:
g.setColor(Color.red);
g.drawLine(0, 0, 100, 50);
break;
case 3:
g.setColor(Color.red);
g.drawLine(0, 0, 100, 100);
break;
case 4:
g.setColor(Color.red);
g.drawLine(0, 0, 300, 300);
break;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame fr = new Main();
fr.setVisible(true);
fr.setSize(500, 500);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
} // end main
}
i mi nie działa rysowanie....
Znaczy rysuje się po raz pierwszy a dalej już nie kce :/