Редактировать: Решено, я не использовал 'validate ()' после добавления компонентов.
У меня есть класс 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);
}
}
Мой основнойМетод содержится в другом классе и работает нормально.
Мой вопрос: как я могу вызвать метод displayButton в методе actionPerformed?Я уже попробовал несколько советов, таких как вызов его с помощью «Gui.this.displayButton (« Нажми меня! »).
Я проверил все остальные аспекты моего кода, и это, кажется,Единственная проблема.
Я не получаю ошибок при запуске кода.
При необходимости я могу опубликовать полный класс, но я думаю, что эта проблема заключается в попытке вызвать эти методы.
Каково ваше мнение?