当前位置: 代码迷 >> Eclipse >> java救助,该怎么处理
  详细解决方案

java救助,该怎么处理

热度:64   发布时间:2016-04-23 14:16:39.0
java救助
这个程序如何变为,运行时显示hao,当按下时,按钮文字变成hao are you,,再按一次,又变成hao,就这样循环

import java.awt.*;
import java.awt.event.*;
public class asas extends Frame implements ActionListener
{
  static asas frm=new asas();
  static Button btn1=new Button("hao");
  static Button btn2=new Button("hao are you ");
  
  public static void main(String args[])
 {
  btn1.addActionListener(frm);
  btn2.addActionListener(frm);

  frm.setTitle("Action Event");
  frm.setLayout(new FlowLayout(FlowLayout.CENTER));
  frm.setSize(200,150);
  frm.add(btn1);
  frm.setVisible(true);
 }
  
  public void actionPerformed(ActionEvent e)

  Button btn=(Button) e.getSource();
  if(btn==btn1)
  frm.setBackground("hao are you");
  else 
  (btn==btn2)
  frm.setBackground("hao");
}
}


------解决方案--------------------
你的问题的提法,好像是下面的这意思的样?http://www.bcclt.cn 编程村论坛
import java.awt.*; 
import java.awt.event.*; 
public class asas extends Frame implements ActionListener 

static asas frm=new asas(); 
 static Button btn1=new Button("hao"); 
static int i=0;
public static void main(String args[]) 
 { 
btn1.addActionListener(frm); 
btn1.setBounds(0,0,200,100);
frm.setTitle("Action Event");
frm.setSize(200,150); 
frm.add(btn1); 
frm.setVisible(true); 
 } 
 
public void actionPerformed(ActionEvent e) 
{
Button btn=(Button) e.getSource(); 
if(btn==btn1) 
{
i++;
if(i%2==1)
{btn1.setLabel("hao are you"); 
}
else if(i%2==0)
{btn1.setLabel("hao");
}
}

}
  相关解决方案