我有两个类(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“)”中遇到错误。我确实知道为什么会这样。我只是不知道解决方案是什么。
最佳答案
它们需要放在单独的文件中吗?您可以将PanelTwo
设置为lay
类的内部类。您正在得到错误,因为拖曳变量不在它们所属的lay
类的范围内。如果您执行以下操作,错误将会消失。
public class lay {
...
private class PanelTwo {
}
}
编辑
使用两个单独的类文件需要做的是在
PanelTwo
中创建一个构造函数,在其中接收CardLayout
和JPanel
类的参数。然后用这两个参数实例化lay
。试试看我将
PanelTwo
和CardLayout
传递给JPanel
的构造函数。工作良好。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");
}
});
}
}
在初始化
PanelTwo
之前,我还在CardLayout
构造函数中初始化了lay
。这样可以避免PanelTwo