Hi, hat jemand eine Idee, wieso mein Button nicht funktioniert? Der button soll das layout vom anderen Fenster ändern!
Java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MyFrame extends JFrame {
public static int anzahl = 0;
public MyFrame(String s,int i,int j){
super(s);
anzahl++;
setSize(i,j);
setLocation(80*anzahl,80*anzahl);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[]args){
MyFrame frame2 = new MyFrame("Fenster2",400,400);
JLabel label0 = new JLabel("Label0");
JLabel label1 = new JLabel("Label1");
JLabel label2 = new JLabel("Label2");
JLabel label3 = new JLabel("Label3");
JLabel label4 = new JLabel("Label4");
frame2.setLayout(new FlowLayout());
frame2.add(label0);
frame2.add(label1);
frame2.add(label2);
frame2.add(label3);
frame2.add(label4);
new MyDialog(frame2,"Buttons");
}
}
class ButtonListener implements ActionListener{
MyFrame frame;
ButtonListener(MyFrame frame) {
this.frame = frame;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Previous")){
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
}
if (e.getActionCommand().equals("Next")){
frame.setLayout(new GridLayout(2,1,10,10));
}
}
}
class MyDialog extends JDialog{
JButton button1 = new JButton("Previous");
JButton button2 = new JButton("Next");
MyFrame frame;
MyDialog(MyFrame f, String s){
setLayout(new GridLayout(1,2,5,5));
setBackground(Color.BLUE);
add(button1);
add(button2);
setSize(200,100);
setTitle(s);
setVisible(true);
frame = f;
ActionListener al = new ButtonListener(frame);
button1.addActionListener(al);
button1.setActionCommand("Previous");
button2.addActionListener(al);
button2.setActionCommand("Next");
}
}
Zuletzt bearbeitet: