首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActionListener问题

ActionListener问题
EN

Stack Overflow用户
提问于 2011-01-14 16:49:51
回答 2查看 1.3K关注 0票数 2

我正在尝试在另一个按钮的一个按钮上做一个actionListener,这个按钮也有一个actionlistener,但是我就是想不通。我正试着在第二个按钮上做一个动作,但我不能计算它out.If任何人帮助我,我将不胜感激!代码如下:

代码语言:javascript
复制
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*;
import java.io.*;
import java.util.*;

public class basic implements ActionListener{

   public static void main(String[] args) {
       basic process = new basic (); 
  }

   public basic(){



           JFrame fan = new JFrame("Scheme"); 


           JPanel one = new JPanel(new BorderLayout()); 
          fan.add(one);

         JPanel uno = new JPanel();
           uno.setLayout(new BoxLayout(uno, BoxLayout.Y_AXIS));
           JButton addB = new JButton("first choice");


         addB.setAlignmentX(Component.CENTER_ALIGNMENT);
           uno.add(addB); 

           addDButton.setActionCommand("hehe");
           addDButton.addActionListener(this);

         one.add(uno,BorderLayout.CENTER); 

           fan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
           fan.setSize(500,700);
          fan.setLocationByPlatform(true);
           fan.setVisible(true);
}

            public void actionPerformed(ActionEvent evt) {

    JPanel markP = new JPanel(new FlowLayout(FlowLayout.RIGHT,10,20)); 
    JDialog dialog = new JDialog((JFrame)null);
    dialog.getContentPane().add(markP,BorderLayout.CENTER); 

     if (evt.getActionCommand().equals("hehe")) {


     JLabel title = new JLabel("Proceed");
     title.setFont(new Font("Arial",Font.BOLD,15)); 
     markP.add(title,BorderLayout.NORTH);



    JButton exit = new JButton("Exit");
    markP.add(exit);


//here i want to create another actionListener on the exit button only without affecting the other content which is in the button "addB " so that when i click on the addB button the J dialog pops up, and than when i click on exit button the program will return to the menu.I couldn't figure it out.



    dialog.toFront(); 
    dialog.setModal(true); 
    dialog.pack(); // 
    dialog.setLocationRelativeTo(null); // 
               dialog.setVisible(true);

}

// here the code goes on but the problem is that of the actionListener which is concerned.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-14 17:03:32

代码语言:javascript
复制
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt) {
         // you code here
   }
});

你应该使用更好的变量名。跟上你的代码并不容易

票数 0
EN

Stack Overflow用户

发布于 2011-01-14 17:13:55

如果您使用以下命令检查操作的源代码,则可以使用相同的ActionListener

代码语言:javascript
复制
if (evt.getSource().equals(addDButton) { original code }
else { code for the other button }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4689352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档