gpt4 book ai didi

java - ActionPerformed 不起作用

转载 作者:行者123 更新时间:2023-12-01 17:24:05 27 4
gpt4 key购买 nike

这段代码有一点问题。 actionPerformed 方法不起作用。当我按下按钮时,按钮 knappStartSalgknappStartKunde 没有反应。

所有应该导入的内容都已导入。

非常感谢您的帮助。

Startmeny 类。

public class Startmeny extends JFrame implements ActionListener
{

public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;

public JPanel startmeny()
{
JPanel startpanel = new JPanel();
startpanel.setLayout(new GridLayout(2, 0, 25, 25) );
startpanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
startpanel.setBackground(Color.white);

JButton knappStartSalg = new JButton();
knappStartSalg.setText("Salg");
knappStartSalg.setVerticalTextPosition(JButton.BOTTOM);
knappStartSalg.setHorizontalTextPosition(JButton.CENTER);
knappStartSalg.setIcon(new javax.swing.ImageIcon(getClass().getResource("salg.png")));
knappStartSalg.setIconTextGap(6);
knappStartSalg.setForeground(Color.black);
knappStartSalg.setBackground(Color.white);
knappStartSalg.setBorderPainted(false);
knappStartSalg.addActionListener(this);

startpanel.add(knappStartSalg);

JButton knappStartKunde = new JButton();
knappStartKunde.setText("Kontroll");
knappStartKunde.setVerticalTextPosition(JButton.BOTTOM);
knappStartKunde.setHorizontalTextPosition(JButton.CENTER);
knappStartKunde.setIcon(new javax.swing.ImageIcon(getClass().getResource("heiskontroll.png")));
knappStartKunde.setIconTextGap(6);
knappStartKunde.setForeground(Color.black);
knappStartKunde.setBackground(Color.white);
knappStartKunde.setBorderPainted(false);
knappStartKunde.addActionListener(this);

startpanel.add(knappStartKunde);

JButton knappStartInfo = new JButton();
knappStartInfo.setText("Informasjonsvindu");
knappStartInfo.setVerticalTextPosition(JButton.BOTTOM);
knappStartInfo.setHorizontalTextPosition(JButton.CENTER);
knappStartInfo.setIcon(new javax.swing.ImageIcon(getClass().getResource("info.png")));
knappStartInfo.setIconTextGap(6);
knappStartInfo.setForeground(Color.black);
knappStartInfo.setBackground(Color.white);
knappStartInfo.setBorderPainted(false);
knappStartInfo.addActionListener(this);

startpanel.add(knappStartInfo);

JButton knappStartStatistikk = new JButton();
knappStartStatistikk.setText("Statistikk");
knappStartStatistikk.setVerticalTextPosition(JButton.BOTTOM);
knappStartStatistikk.setHorizontalTextPosition(JButton.CENTER);
knappStartStatistikk.setIcon(new javax.swing.ImageIcon(getClass().getResource("statistikk.png")));
knappStartStatistikk.setIconTextGap(6);
knappStartStatistikk.setForeground(Color.black);
knappStartStatistikk.setBackground(Color.white);
knappStartStatistikk.setBorderPainted(false);
knappStartStatistikk.addActionListener(this);

startpanel.add(knappStartStatistikk );

return startpanel;
}


@Override
public void actionPerformed(ActionEvent e)
{
Salgsvindu s = new Salgsvindu();

if(e.getSource() == knappStartSalg)
{
s.visSalgvinduNyBruker();
System.out.println("hallotest");
}
else if(e.getSource() == knappStartKunde)
s.visKontrollvindu();
}
}

主类,运行 GUI。

public class Skisenter
{
public static void main(String[] args)
{
/*Salgsvindu s = new Salgsvindu();
s.visSalgvinduNyBruker();*/
Startmeny startmenyinstanse = new Startmeny();

startmenyinstanse.setSize(600, 630);
startmenyinstanse.setTitle("Startmeny for skisenter");
Startmeny st = new Startmeny();
startmenyinstanse.setContentPane(st.startmeny());
startmenyinstanse.setVisible(true);
startmenyinstanse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

最佳答案

在您的类中,您定义实例变量:

public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;

您可以在操作监听器实现中使用这些变量。然而,在您的构造函数中,您构造 JButton 并将它们分配给新的局部变量,而不是您在构造函数上方声明的实例变量。所以代替:

JButton knappStartSalg = new JButton();

knappStartSalg = new JButton();

将按钮分配给实例变量,您的操作监听器应该可以工作。

关于java - ActionPerformed 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com