A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import javax.swing.*;
import java.awt.event.*;
public class TestJavax extends JFrame implements ActionListener {
  JButton a=new JButton();
  JButton b=new JButton();
  JButton c=new JButton();
  JPanel x=new JPanel();
  
public TestJavax() {
    a.setText("你好");
    this.setBounds(500,200,300,200);
    this.setTitle("窗口");
    this.setVisible(true);
    this.add(x);
    x.add(a);
    a.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {

   a.setVisible(false);
   b.setText("HELLO");
   this.setBounds(500,200,300,200);                           
   this.setVisible(true);
   this.setTitle("窗口2");
    x.add(b);
    b.addActionListener(this);                                            //问题在这里,当点击上面的“你好”按钮的时候,会出现下面的这里的“HELLO”按钮。  
                                                  //但是我想继续当点这个“HELLO”的时候,能够再生成一个“你好-HELLO”的按钮,也就是处理actionPerformed方法里面的
                                                  //内容,应该如何做?
}


public static void main(String args[]){
  TestJavax a=new TestJavax();
}
}

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

9 个回复

倒序浏览
伯阳兄:请看代码,不懂再问我哈:
import javax.swing.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener {
  JButton a=new JButton();
/* JButton b=new JButton();*/
/* JButton c=new JButton();*/
  JPanel x=new JPanel();
  
  int count = 1;
public Test() {
    a.setText("你好");
    this.setBounds(500,200,300,200);
    this.setTitle("窗口");
    this.setVisible(true);
    this.add(x);
    x.add(a);
    a.addActionListener(this);
    a.setVisible(true);
}
public void actionPerformed(ActionEvent e) {

   if(count == 1)
   {
   a.setText("HELLO");
   }
   else{
           a.setText("你好,HELLO");
   }
                     
   count++;                                    
}


public static void main(String args[]){
  Test a=new Test();
}
}
回复 使用道具 举报
黑马11期李项京 发表于 2012-6-2 14:01
伯阳兄:请看代码,不懂再问我哈:
import javax.swing.*;
import java.awt.event.*;

京兄:你写的表现形式看起来是那样,不过你是只对JButton a进行监听,然后if判断。我想要的是点了个a之后 重新生成一个JButton b,而且窗口也是一个新的窗口是“窗口2”  然后对这个新生成的b再监听,点了这个b 生成个窗口3里面有JButton c,c的内容是“你好,HELLO”。
回复 使用道具 举报
学艺不精。小妹说点自己的理解哈~
楼主的代码,在监听到的事件处理public void actionPerformed(ActionEvent e) {}中 应该只是对窗口进行了重新改变,而不是新建的窗口。
因而按楼主的要求,在“新窗口”内再加入监听,应该几个监听都是加载在一个窗口内的。

因而加入count ++,对点击进行累计记录而对应相应的方法很好哈~

回复 使用道具 举报
张洁 发表于 2012-6-2 14:23
学艺不精。小妹说点自己的理解哈~
楼主的代码,在监听到的事件处理public void actionPerformed(ActionEven ...

我觉得不然;    二楼回答的和楼主本意有点不对上;   就像平时有些人作恶搞一样,   一个JDialog中有一按扭“你确实要退出么”   你点击了“确实”   然后他又弹出一个JDialog,  又说,  “我想再说一次,你真想退出?”  于是又点击“是的“    ,   完后,,又出现内容 “  看来你是蛮急的嘛,就不要你退出来!要不你再点击试试”   就这样一直出现    我觉得楼主的问题有这味道 ;   
回复 使用道具 举报
刘伯阳 发表于 2012-6-2 14:11
京兄:你写的表现形式看起来是那样,不过你是只对JButton a进行监听,然后if判断。我想要的是点了个a之后 ...

你是想弹出新的窗口?
回复 使用道具 举报
刘伯阳 发表于 2012-6-2 14:11
京兄:你写的表现形式看起来是那样,不过你是只对JButton a进行监听,然后if判断。我想要的是点了个a之后 ...

按照你的意思又改了一下;P
import javax.swing.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {
        JButton a = new JButton();
        JButton b = new JButton();
        JButton c = new JButton();
        JPanel x = new JPanel();

        int count = 1;

        public Test() {
                a.setText("你好");
                this.setBounds(500, 200, 300, 200);
                this.setTitle("窗口");
                this.setVisible(true);
                this.add(x);
                x.add(a);
                a.addActionListener(this);
                a.setVisible(true);
        }

        public void actionPerformed(ActionEvent e) {

                if (count == 1) {
                        a.setVisible(false);
                        b.setText("HELLO");
                        this.setBounds(500, 200, 300, 200);

                        this.setVisible(true);
                        this.setTitle("窗口2");
                        x.add(b);
                        b.addActionListener(this);
                } else {
                        b.setVisible(false);
                        c.setText("你好,HELLO");
                        this.setBounds(500, 200, 300, 200);

                        this.setVisible(true);
                        this.setTitle("窗口3");
                        x.add(c);
                        c.addActionListener(this);
                }

                count++;
        }

        public static void main(String args[]) {
                Test a = new Test();
        }
}

评分

参与人数 1技术分 +1 收起 理由
袁錦泰 + 1

查看全部评分

回复 使用道具 举报
{:soso_e113:}
回复 使用道具 举报
黑马11期李项京 发表于 2012-6-2 14:36
你是想弹出新的窗口?

弹出一个新的Button,只不过是覆盖在以前那个上面,然后对这个新的Button再点一次,再生成一个新的覆盖在它的上面,就是对一个actionPerformed()里面的键继续监听  这么个意思
回复 使用道具 举报
黑马11期李项京 发表于 2012-6-2 14:44
按照你的意思又改了一下
import javax.swing.*;
import java.awt.event.*;

对  是这个意思 !  赞一个!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马