简体   繁体   English

ActionPerformed不起作用

[英]ActionPerformed does not work

Have a little problem with this code. 这段代码有一点问题。 The actionPerformed method doesn't work. actionPerformed方法不起作用。 Buttons knappStartSalg and knappStartKunde , don't react when I push the buttons. 按钮knappStartSalgknappStartKunde ,当我按下按钮时不反应。

All that should have been imported are imported. 所有本应导入的都将导入。

Will be very thankful for any help. 非常感谢您的帮助。

Startmeny class. 入门班。

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();
}
}

Main-class, which runs the GUI. 主类,运行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);
}
}

In your class, you define instance variables: 在您的类中,定义实例变量:

public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;

You use these variables in your action listener implementation. 您可以在动作侦听器实现中使用这些变量。 However in your constructor, you construct JButton s and assign them to new local variables, not the instance variables you declare above the constructor. 但是,在构造函数中,您构造JButton并将它们分配给新的局部变量,而不是在构造函数上方声明的实例变量。 So instead of: 所以代替:

JButton knappStartSalg = new JButton();

write

knappStartSalg = new JButton();

to assign the buttons to the instance variables and your action listener should work. 将按钮分配给实例变量,您的动作侦听器应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM