标题:这个程序可以这样实现吗?
只看楼主
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
 问题点数:0 回复次数:2 
这个程序可以这样实现吗?
有一个java程序,就是一个窗口上有2个按钮(一个隐藏,一个显示),要求按一个按钮,另一个就出现,而它自己隐藏。

我想这样实现:一个frame里有两个Panel,每个里面有一个button,设置初始状态为一个可见一个不可见,setVisible(true),和setVisible(false),程序在下面,编译没问题,可他说没主程序,可我明明有入口,这是错了吗?为什么?
import java.awt.*;
import java.awt.event.*;
public class Buttonhide implements MouseListener,WindowListener
{
/**
* @param args
*/
private Frame f;
private Panel p1;
private Panel p2;
private Button b1;
private Button b2;
boolean i=true;
boolean j=false;
boolean temp;
public static void main(String[] args)
{
Buttonhide one = new Buttonhide();
one.go();
}
public void go()
{
f=new Frame("i'm xuwei");
b1=new Button("button1");
b2=new Button("button2");
b1.addMouseListener(this);
b2.addMouseListener(this);
f.addWindowListener(this);
f.setLayout(new BorderLayout());
f.setSize(200,200);
f.setVisible(true);
f.add(p1,"north");
f.add(p2,"south");
p1.add(b1);
p2.add(b2);
p1.setVisible(i);
p2.setVisible(j);
}
public void mouseClicked(MouseEvent e)
{
temp=i;
i=j;
j=temp;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}

}
搜索更多相关主题的帖子: java程序 public frame button import 
2007-05-31 20:23
xw2014
Rank: 1
等 级:新手上路
帖 子:54
专家分:0
注 册:2006-1-17
得分:0 

这个可以怎么实现啊`?大家教教我撒~~我不怎么会`


2007-05-31 22:41
风月_无边
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:60
帖 子:2959
专家分:652
注 册:2007-1-19
得分:0 

代码如下:

import java.awt.*;
import java.awt.event.*;
public class Buttonhide implements ActionListener,Runnable
{
/**
* @param args
*/
private Frame f;
private Panel p1;
private Panel p2;
private Button b1;
private Button b2;
boolean i=true;
boolean j=false;
boolean temp;
public static void main(String[] args)
{
Buttonhide one = new Buttonhide();
Thread t=new Thread(one);
t.start();

}
public Buttonhide()
{
f=new Frame("i'm xuwei");
b1=new Button("button1");
b2=new Button("button2");
p1=new Panel();
p1.setLayout(new FlowLayout());
p2=new Panel();

p2.setLayout(new FlowLayout());
b1.addActionListener(this);
b2.addActionListener(this);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setLayout(new BorderLayout());
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.SOUTH);
p1.add(b1);
p2.add(b2);

f.setSize(400,400);
f.setVisible(true);


}


public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==b1)
{
temp=i;
i=j;
j=temp;
}
if(e.getSource()==b2)
{
temp=i;
i=j;
j=temp;
}
}
public void run() {
// TODO Auto-generated method stub
while(true)
{
p1.setVisible(i);
p2.setVisible(j);
}
}

}



我的网站 http://www.
2007-06-01 09:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-143912-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.135287 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved