编辑:已解决,添加组件后我没有使用“validate()”。Java - 调用嵌套类中的方法,特别是ActionListener
我有一个GUI类的结构是这样的(这是我的代码一个非常基本的表示):
编辑:这里是我的全部代码
package source;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Gui extends JFrame
{
public String[] list1 = {"equation1","equation2","equation3","equation4", "equation5"};
public JOptionPane opt1;
private JButton custom;
private JTextField[] tf;
public HandlerClass2 itemhandler = new HandlerClass2();
private JList list;
private static int index = 0;
private static int lastlistindex = 0;
private JPanel buttonpanel;
private JPanel buttonpanel2[] = new JPanel[3];
private JPanel listpanel[] = new JPanel[4];
private JPanel checkpanel;
private JCheckBox checkboxes[];
private SpringLayout layout;
public Container contentPane;
private JButton but;
public Gui()
{
super("Physics Helper v0.1");
setBackground(Color.DARK_GRAY);
layout = new SpringLayout();
contentPane = getContentPane();
contentPane.setLayout(layout);
displayPortal();
}
public void displayPortal()
{
Icon a = new ImageIcon(getClass().getResource("button.png"));
Icon b = new ImageIcon(getClass().getResource("button2.png"));
custom = new JButton("", a);
custom.setRolloverIcon(b);
buttonpanel = new JPanel();
buttonpanel.setBackground(Color.GRAY);
buttonpanel.add(custom);
contentPane.add(buttonpanel);
layout.putConstraint(SpringLayout.WEST, buttonpanel, 5, SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.EAST, buttonpanel, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.NORTH, buttonpanel, 5, SpringLayout.NORTH, contentPane);
custom.addActionListener(new HandlerClass());
}
public void displayButton(String s)
{
but = new JButton(s);
buttonpanel2[index] = new JPanel();
buttonpanel2[index].setBackground(Color.GRAY);
buttonpanel2[index].add(but);
contentPane.add(buttonpanel2[index]);
layout.putConstraint(SpringLayout.SOUTH, buttonpanel2[index], -5, SpringLayout.SOUTH, contentPane);
if (index < 1)
{
layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.WEST, contentPane);
}
else
{
layout.putConstraint(SpringLayout.WEST, buttonpanel2[index], 5, SpringLayout.EAST, buttonpanel2[index - 1]);
}
index++;
}
public void displayList(String[] t)
{
list = new JList(t);
list.setVisibleRowCount(8);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(list));
listpanel[lastlistindex] = new JPanel();
listpanel[lastlistindex].setBackground(Color.GRAY);
listpanel[lastlistindex].add(list);
contentPane.add(listpanel[lastlistindex]);
layout.putConstraint(SpringLayout.NORTH, listpanel[lastlistindex], 5, SpringLayout.SOUTH, buttonpanel);
if (lastlistindex < 1)
{
layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.WEST, contentPane);
}
else
{
layout.putConstraint(SpringLayout.WEST, listpanel[lastlistindex], 5, SpringLayout.EAST, listpanel[lastlistindex - 1]);
}
lastlistindex++;
}
public void displayInputValues(String[] p)
{
checkboxes = new JCheckBox[p.length];
GridLayout gridlayout = new GridLayout(p.length, 2);
tf = new JTextField[p.length];
checkpanel = new JPanel();
checkpanel.setBackground(Color.GRAY);
checkpanel.setLayout(gridlayout);
for (int b = 0; b < p.length; b++)
{
checkboxes[b] = new JCheckBox(p[b]);
checkpanel.add(checkboxes[b]);
tf[b] = new JTextField("", 9);
checkpanel.add(tf[b]);
tf[b].setFont(new Font("Serif", Font.PLAIN, 14));
}
contentPane.add(checkpanel);
layout.putConstraint(SpringLayout.EAST, checkpanel, -5, SpringLayout.EAST, contentPane);
layout.putConstraint(SpringLayout.SOUTH, checkpanel, -5, SpringLayout.SOUTH, contentPane);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
displayButton("Back");
displayButton("Next");
displayList(list1);
}
}
我的主要方法是包含在另一个类中,并且工作正常。
我的问题是如何在actionPerformed方法中调用“displayButton”方法?我已经尝试了一些技巧,比如用“Gui.this.displayButton(”Press me!“)调用它。
我测试了我的代码的其他方面,这似乎是唯一的问题。
我没有得到任何错误,当我运行的代码。
如果需要的话,我可以张贴满级的,但我想这个问题就在于试图调用这些方法。
你对此有何看法?
您在displayButton末尾缺少分号(“Press me!”)。我猜这是错字。 – JProgrammer 2012-02-29 16:33:42
“我在运行代码时没有遇到任何错误。” - 但是你的代码不能编译... – DNA 2012-02-29 16:34:17
如果你修正了编译错误,它对我很有用(编辑:如果你按照@ DNA的推荐添加处理程序到按钮 - 1+投票给他的答案)。我认为你的问题存在于代码中的其他地方,尤其是在这里:'//更多代码来显示包含contentPane中的按钮的JPanel' – 2012-02-29 16:35:45