帮助改一下程序

madiankun 2006-12-18 05:23:15
import java.awt.*;
import java.awt.event.*;
public class Interact implements ActionListener {
Frame f=new Frame("按钮交互事件");
f.setLayout(new FlowLayout());//此处出现提示错误。
Button b=new Button("打开");

public void actionPerformed(ActionEvent e)
{ if(e.getSource()==b)
{ if(b.getLabel()=="打开")
{ b.setLabel("关闭");
f.pack();
}
else
{b.setLabel("打开"); f.pack();}

}
我想作一个按钮交互的图像,看看上面的程序怎么改呀。
}
}
...全文
77 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
welshem 2006-12-18
  • 打赏
  • 举报
回复
package ocean.accp.demo;

import java.awt.*;
import java.awt.event.*;

public class Interact implements ActionListener {

public static void main(String[] args) {
Frame frame=new Frame("按钮交互事件");
frame.setLayout(new FlowLayout());
frame.setSize(200, 100);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Button cmd=new Button("打开");
cmd.addActionListener(new Interact());
frame.add(cmd);

frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("打开")){
((Button)e.getSource()).setLabel("关闭");
}else{
((Button)e.getSource()).setLabel("打开");
}
}
}
welshem 2006-12-18
  • 打赏
  • 举报
回复
f.setLayout(new FlowLayout());这是命令不能放在声明成员的位置
happy_sky 2006-12-18
  • 打赏
  • 举报
回复
setLayout 这些是布局管理器来调用的 不能直接用类对象调用`

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧