如何从外部类访问CardLayout变量?

时间:2014-01-04 13:28:32

标签: java swing jpanel cardlayout

我有两个班级(lay和panelTwo)。在“lay”中我有MainMethod,这就是我放置CardLayout的地方。我想要做的是在另一个外部类中访问该类。但在尝试编写ActionListener时,我似乎无法访问它:

 public class lay {

JFrame frame;
JPanel panelCont;
JPanel panelOne;
JButton buttonOne;
CardLayout cards;

PanelTwo panelTwo;

public lay() {
    frame = new JFrame("Start");
    panelCont = new JPanel();
    panelOne = new JPanel();
    panelTwo = new PanelTwo();
    buttonOne = new JButton("Got to two");
    cards = new CardLayout();

    panelCont.setLayout(cards);

    panelOne.add(buttonOne);
    panelOne.setBackground(Color.BLUE);

    panelCont.add(panelOne, "1");
    panelCont.add(panelTwo, "2");
    cards.show(panelCont, "1");

    buttonOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cards.show(panelCont, "2");
        }
    });

    frame.add(panelCont);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new lay();
        }
    });
}

}

这是我的另一个问题所在的课程:

public class PanelTwo extends JPanel {

JButton buttonTwo;

public PanelTwo() {

    buttonTwo = new JButton("Go to one");

    add(buttonTwo);
    setBackground(Color.GREEN);

    buttonTwo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cards.show(panelCont, "1");
        }
    });


    }

}

你可能会说,我在“cards.show(panelCont,”1“)”中收到错误。我明白为什么会这样。我只是不知道解决方案是什么。

1 个答案:

答案 0 :(得分:2)

他们需要是否在单独的文件中?您可以使PanelTwo成为lay类的内部类。您收到错误,因为两个变量不属于lay类的范围。如果您执行了以下操作,错误就会消失。

public class lay {
   ...
   private class PanelTwo {

   }
}

修改

使用两个单独的类文件需要做的是在PanelTwo中创建一个构造函数,您可以从CardLayoutJPanel中获取lay的参数}类。然后使用这两个参数实例化PanelTwo

试一试。我将CardLayoutJPanel传递给PanelTwo的构造函数。工作正常。

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class lay {

    JFrame frame;
    JPanel panelCont;
    JPanel panelOne;
    JButton buttonOne;
    CardLayout cards;

    PanelTwo panelTwo;

    public lay() {
        frame = new JFrame("Start");
        panelCont = new JPanel();
        panelOne = new JPanel();
        cards = new CardLayout();
        panelTwo = new PanelTwo(cards, panelCont);
        buttonOne = new JButton("Got to two");


        panelCont.setLayout(cards);

        panelOne.add(buttonOne);
        panelOne.setBackground(Color.BLUE);

        panelCont.add(panelOne, "1");
        panelCont.add(panelTwo, "2");
        cards.show(panelCont, "1");

        buttonOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cards.show(panelCont, "2");
            }
        });

        frame.add(panelCont);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new lay();
            }
        });
    }

}

class PanelTwo extends JPanel {

    JButton buttonTwo;
    CardLayout layout;
    JPanel panelCont;

    public PanelTwo(final CardLayout layout, final JPanel panelCont) {
        this.layout = layout;
        this.panelCont = panelCont;
        buttonTwo = new JButton("Go to one");

        add(buttonTwo);
        setBackground(Color.GREEN);

        buttonTwo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                layout.show(panelCont, "1");
            }
        });

    }
}

在初始化CardLayout之前,我还初始化了lay构造函数中的PanelTwo。这避免了NullPointerException