本文介绍了ActionListener和ActionPerformed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我如何将actionListener添加到名为"OK"的按钮上,当我单击该按钮时,该按钮将转到另一个名为MainMenu的窗口吗?

Can anyone guide me how add actionListener to a button named "OK" that when I click the button it goes to another window called MainMenu?

推荐答案

import javax.swing.*;
import java.awt.event.*;
public class ChangeButtonLabel{
  JButton button;
  JDialog dispay;
  MainMenu myDispay;
  public static void main(String[] args){
    ChangeButtonLabel cl = new ChangeButtonLabel();
  }
  public ChangeButtonLabel(){
    JFrame frame = new JFrame("Change JButton Lebel");
    dispay=new JDialog();
    button = new JButton("Click Me");
    myDispay=new MainMenu();  ///MainMenu class file should present in current folder as well as this class must extend from JPanel
    button.addActionListener(new MyAction());
    frame.add(button);
    dispay.add(myDispay);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public class MyAction implements ActionListener{
    public void actionPerformed(ActionEvent e)
   {
               dispay.setSize(300, 300);
               dispay.setVisible(true);
   }
  }
} 


如果您有任何建议,最欢迎


if you have any suggestion most WELCOME



这篇关于ActionListener和ActionPerformed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:44