mv700
mv700

Reputation: 35

ActionPerformed does not work

Have a little problem with this code. The actionPerformed method doesn't work. Buttons knappStartSalg and knappStartKunde, don't react when I push the buttons.

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.

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

Upvotes: 0

Views: 554

Answers (1)

harpun
harpun

Reputation: 4110

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 JButtons and assign them to new local variables, not the instance variables you declare above the constructor. 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.

Upvotes: 5

Related Questions