Java ActionListener出错

时间:2017-11-28 11:57:59

标签: java actionlistener

我编码,我看不到错误。这不是直接的“错误”,但是当我按下蓝色时,背景不会将颜色变为蓝色。我试图将这个代码从类Meny(ActionListener)替换为public void actionPerformed(ActionEvent e),但它不起作用。

这是代码。

names = ['Japan', 'UK', 'USA', 'Uruguay', 'Brazil', 'Mexico', 'Costa Rica']

for item, name in zip(totalIPs, names):

    mu=statistics.mean(item)
    std=statistics.stdev(item)

    xmin, xmax = plt.xlim()
    x=np.linspace(xmin, xmax, 100)

    fitted_pdf=ss.norm.pdf(x,mu, std)

    plt.figure()

    plt.xlabel('Distribution of patents by IP firms',fontsize=12)

    plt.ylabel('Frequency of firms of each patents',fontsize=12)

    plt.hist(x, bins=10,normed=True, alpha=0.6, edgecolor='black', color = 'g')

    plt.plot(x, fitted_pdf, 'K', linewidth=2)

    title = "{} IP firms patent filings: mu ={:.2f}, std = {:.2f}".format(name, mu, std)
    plt.title(title)

1 个答案:

答案 0 :(得分:0)

public class MenyTest extends JFrame implements ActionListener{

JPanel panel;
JMenuBar menyrad;
JMenu fargmeny,svmeny;
JMenuItem rod,gron,bla,svart,gra,vit;

public MenyTest() {
     panel = new JPanel();
       panel.setBackground(Color.white);
       add(panel);

       menyrad = new JMenuBar();
       fargmeny = new JMenu("Färger");
       svmeny = new JMenu("SvartVitt");
       this.setJMenuBar(menyrad);
       menyrad.add(fargmeny);
       menyrad.add(svmeny);

       rod = new JMenuItem("Röd");
       gron = new JMenuItem("Grön");
       bla = new JMenuItem("Blå");
       svart = new JMenuItem("Svart");
       gra = new JMenuItem("Grå");
       vit = new JMenuItem("Vit");

       fargmeny.add(rod);
       fargmeny.add(gron);
       fargmeny.add(bla);

       svmeny.add(svart);
       svmeny.add(gra);
       svmeny.add(vit);
       rod.addActionListener(this);
       gron.addActionListener(this);
       bla.addActionListener(this);
       svart.addActionListener(this);
       gra.addActionListener(this);
       vit.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
     if (e.getSource() == rod) panel.setBackground(Color.red);
       else if (e.getSource() == gron) panel.setBackground(Color.green);
       else if (e.getSource() == bla) panel.setBackground(Color.blue);
       else if (e.getSource() == svart) panel.setBackground(Color.black);
       else if (e.getSource() == gra) panel.setBackground(Color.gray);
       else if (e.getSource() == vit) panel.setBackground(Color.white);  

}

}

public class Main  {

 public static void main(String[] args) {
        MenyTest f = new MenyTest();
         f.setSize(500, 500);
         f.setLocation(400, 150);
         f.setTitle("MenyTest");
         f.setDefaultCloseOperation(new JFrame().EXIT_ON_CLOSE);
         f.setVisible(true);
     }


}