2012-07-15 2 views
3

카드 레이 아웃을 사용하고 있는데 첫 번째 카드에 버튼이 있고 클릭하면 카드로 가져갈 버튼이있는 카드 2로 이동합니다. 1. 현재 코드가 있습니다. actionPerformed 메소드에 몇 가지 사항을 넣으려고 시도했지만 제대로 작동하지 못했습니다. 또한 button1.addActionListener (this); 줄에 "this"구문 오류가 나타납니다. 및 button2.addActionListener (this); 내 actionPerformed 메서드가 올바르게 설정되지 않았기 때문에 가정합니다. 버튼 설정을 얻는 데 도움이되는 점은 크게 감사하겠습니다. 도움을 많이jbutton을 사용하는 카드 간 Java 전환

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Main implements ItemListener { 

JPanel cards; 

public void addComponentToPane(Container pane) {   
    //create cards 
    JPanel card1 = new JPanel(); 
    JPanel card2 = new JPanel(); 
    JButton button1 = new JButton("Button 1"); 
    JButton button2 = new JButton("Button 2"); 
    button1.addActionListener(this); 
    button2.addActionListener(this); 
    card1.add(button1); 
    card2.add(button2); 

    //create panel that contains cards 
    cards = new JPanel(new CardLayout()); 
    cards.add(card1); 
    cards.add(card2);   
    pane.add(cards, BorderLayout.CENTER); 
} 

public void itemStateChanged(ItemEvent evt) { 
    CardLayout cl = (CardLayout)(cards.getLayout()); 
    cl.show(cards, (String)evt.getItem()); 
} 

public static void createAndShowGUI() { 
    //create and setup window 
    JFrame frame = new JFrame("Frame"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //create and setup content pane 
    Main main = new Main(); 
    main.addComponentToPane(frame.getContentPane()); 

    //display window 
    frame.pack(); 
    frame.setVisible(true); 
} 

public void actionPerformed(ActionEvent ae) { 

}    

public static void main(String[] args) { 
    //set look and feel 
    try { 
     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } catch (UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } catch (IllegalAccessException ex) { 
     ex.printStackTrace(); 
    } catch (InstantiationException ex) { 
     ex.printStackTrace(); 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    //turn off metal's bold fonts 
    UIManager.put("swing.boldMetal", Boolean.FALSE);   

    //schedule job for the event dispatch thread creating and showing GUI   
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    });  
} 

}

답변

4
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class CardTest implements ActionListener { 

    private JPanel cards; 
    private JButton button1; 
    private JButton button2; 

    public void addComponentToPane(Container pane) { 
     // create cards 
     JPanel card1 = new JPanel(); 
     JPanel card2 = new JPanel(); 
     button1 = new JButton("Button 1"); 
     button2 = new JButton("Button 2"); 
     button1.addActionListener(this); 
     button2.addActionListener(this); 
     card1.add(button1); 
     card2.add(button2); 

     // create panel that contains cards 
     cards = new JPanel(new CardLayout()); 
     cards.add(card1, "Card 1"); 
     cards.add(card2, "Card 2"); 
     pane.add(cards, BorderLayout.CENTER); 
    } 

    public void itemStateChanged(ItemEvent evt) { 
     CardLayout cl = (CardLayout) (cards.getLayout()); 
     cl.show(cards, (String) evt.getItem()); 
    } 

    public static void createAndShowGUI() { 
     // create and setup window 
     JFrame frame = new JFrame("Frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // create and setup content pane 
     CardTest main = new CardTest(); 
     main.addComponentToPane(frame.getContentPane()); 

     // display window 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     if (ae.getSource() == button1) { 

      CardLayout cl = (CardLayout) (cards.getLayout()); 
      cl.show(cards, "Card 2"); 

     } else if (ae.getSource() == button2) { 

      CardLayout cl = (CardLayout) (cards.getLayout()); 
      cl.show(cards, "Card 1"); 
     } 
    } 

    public static void main(String[] args) { 
     // set look and feel 
     try { 
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 
     // turn off metal's bold fonts 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     // schedule job for the event dispatch thread creating and showing GUI 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

감사합니다. "this"다음에 오류가 계속 발생합니다. class javax.swing.AbstractButton의 addActionListener 메서드는 지정된 유형에 적용 할 수 없습니다. 필수 : ​​발견 java.awt.event.ActionListener : noxia.CardTest 이유 : 클래스 정의를 변경 – Jarod

+0

noxia.CardTest이 메소드 호출 변환에 의해 java.awt.event.ActionListener로 변환 할 수없는 실제 인수는 _ActionListener_ – Reimeus

+0

아 감사를 구현하는 너 너무 많이, 그것은 크게 감사 하네 :) – Jarod