这段代码有点问题。 actionPerformed
方法不起作用。按钮knappStartSalg
和knappStartKunde
,按下按钮时不会做出反应。
导入的所有内容都已导入。
非常感谢任何帮助。
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,运行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);
}
}
答案 0 :(得分:5)
在您的课程中,您可以定义实例变量:
public JButton knappStartSalg, knappStartKunde, knappStartInfo, knappStartStatistikk;
您可以在动作侦听器实现中使用这些变量。但是,在构造函数中,构造JButton
并将它们分配给新的本地变量,而不是在构造函数上方声明的实例变量。所以而不是:
JButton knappStartSalg = new JButton();
写
knappStartSalg = new JButton();
将按钮分配给实例变量,您的动作监听器应该可以工作。