【问题标题】:Java switch between cards using jbuttonJava使用jbutton在卡片之间切换
【发布时间】:2012-07-14 08:37:44
【问题描述】:

我正在使用卡片布局,我想让第一张卡片有一个按钮,当点击它时,它将把它带到卡片 2,它有一个按钮可以把它带回卡片 1。这是我当前的代码,我尝试在 actionPerformed 方法中添加一些东西,但我没有成功让它工作。此外,我在 button1.addActionListener(this); 的行上收到关于“this”的语法错误;和 button2.addActionListener(this);我认为这是因为我的 actionPerformed 方法设置不正确。任何有关设置按钮的帮助将不胜感激。

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();
        }
    });     
}   

}

【问题讨论】:

    标签: java swing cardlayout


    【解决方案1】:
    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();
                }
            });
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助。我仍然在“this”下面收到错误消息:类 javax.swing.AbstractButton 中的方法 addActionListener 不能应用于给定类型;必需:java.awt.event.ActionListener 找到:noxia.CardTest 原因:实参noxia.CardTest 无法通过方法调用转换为java.awt.event.ActionListener
    • 更改类定义实现ActionListener
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多