hallo,
ich möchte eine grafik (irgendwelche Linien) in einem Canvas dann ändern, wenn ich auf einen Button drücke, der Button liegt nict im Canvas, sondern an einer anderen Stelle im jFrame - wie kann ich das machen??
Hier hab ich mal meinen Code bis jetzt.
wer kann mir helfen?
ich möchte eine grafik (irgendwelche Linien) in einem Canvas dann ändern, wenn ich auf einen Button drücke, der Button liegt nict im Canvas, sondern an einer anderen Stelle im jFrame - wie kann ich das machen??
Hier hab ich mal meinen Code bis jetzt.
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class leiauts {
// static Canvas ca = new Canvas();
static int breite=100;
static JButton jb1;
static JFrame f;
static MyCanvas my;
static Container c;
static class MyCanvas extends Canvas implements ActionListener {
public MyCanvas()
{
}
public void paint(Graphics g) {
//super.paint(g);
g.drawString("Canvas", breite, 20);
System.out.println("canvaaaaaaas");
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("ääääägdschen performd");
}
}
public static void main(String[] args) {
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = f.getContentPane();
GridBagLayout gbl = new GridBagLayout();
c.setLayout(gbl);
my = new MyCanvas();
jb1 = new JButton(exitAction);
jb1.setActionCommand("test");
System.out.println("Breite= " + breite);
// x y w h wx wy
addComponent(c, gbl, my, 0, 0, 800, 10, 1, 30);
addComponent(c, gbl, jb1, 0, 10, 1, 1, 1, 1);
f.setSize(800, 600);
f.setVisible(true);
}
static Action exitAction = new AbstractAction("Ende") {
public void actionPerformed(ActionEvent e) {
System.out.println("aFSDASFASHKFDFASGHDASFHDF");
breite = breite + 50;
}
};
static void addComponent(Container cont, GridBagLayout gbl, Component c,
int x, int y, int width, int height, double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = weightx;
gbc.weighty = weighty;
gbl.setConstraints(c, gbc);
cont.add(c);
}
public int getBreite() {
return breite;
}
public void setBreite(int breite) {
this.breite = breite;
}
}
wer kann mir helfen?