mardi 5 mai 2015

Swing Basics - JButton funktioniert nicht.

Hi, hat jemand eine Idee, wieso mein Button nicht funktioniert? Der button soll das layout vom anderen Fenster ändern!

Java Code:

  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8. public class MyFrame extends JFrame {
  9. public static int anzahl = 0;
  10.  
  11. public MyFrame(String s,int i,int j){
  12. super(s);
  13. anzahl++;
  14.  
  15.  
  16. setSize(i,j);
  17. setLocation(80*anzahl,80*anzahl);
  18. setVisible(true);
  19. setDefaultCloseOperation(EXIT_ON_CLOSE);
  20.  
  21. }
  22.  
  23.  
  24.  
  25. public static void main(String[]args){
  26.  
  27. MyFrame frame2 = new MyFrame("Fenster2",400,400);
  28.  
  29. JLabel label0 = new JLabel("Label0");
  30. JLabel label1 = new JLabel("Label1");
  31. JLabel label2 = new JLabel("Label2");
  32. JLabel label3 = new JLabel("Label3");
  33. JLabel label4 = new JLabel("Label4");
  34.  
  35. frame2.setLayout(new FlowLayout());
  36. frame2.add(label0);
  37. frame2.add(label1);
  38. frame2.add(label2);
  39. frame2.add(label3);
  40. frame2.add(label4);
  41.  
  42. new MyDialog(frame2,"Buttons");
  43.  
  44.  
  45. }
  46. }
  47.  
  48. class ButtonListener implements ActionListener{
  49. MyFrame frame;
  50.  
  51. ButtonListener(MyFrame frame) {
  52. this.frame = frame;
  53. }
  54.  
  55. public void actionPerformed(ActionEvent e) {
  56. if (e.getActionCommand().equals("Previous")){
  57. frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
  58. }
  59. if (e.getActionCommand().equals("Next")){
  60. frame.setLayout(new GridLayout(2,1,10,10));
  61. }
  62.  
  63.  
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70. class MyDialog extends JDialog{
  71.  
  72. JButton button1 = new JButton("Previous");
  73. JButton button2 = new JButton("Next");
  74.  
  75. MyFrame frame;
  76.  
  77. MyDialog(MyFrame f, String s){
  78.  
  79. setLayout(new GridLayout(1,2,5,5));
  80. setBackground(Color.BLUE);
  81. add(button1);
  82. add(button2);
  83. setSize(200,100);
  84. setTitle(s);
  85. setVisible(true);
  86.  
  87. frame = f;
  88.  
  89. ActionListener al = new ButtonListener(frame);
  90. button1.addActionListener(al);
  91. button1.setActionCommand("Previous");
  92. button2.addActionListener(al);
  93. button2.setActionCommand("Next");
  94. }
  95.  
  96. }


Swing Basics - JButton funktioniert nicht.

0 commentaires:

Enregistrer un commentaire