当前位置: 代码迷 >> Java相关 >> 这个程序可以这样实现吗?
  详细解决方案

这个程序可以这样实现吗?

热度:156   发布时间:2007-05-31 20:23:41.0
这个程序可以这样实现吗?
有一个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  

----------------解决方案--------------------------------------------------------

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


----------------解决方案--------------------------------------------------------

代码如下:

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);
}
}

}



----------------解决方案--------------------------------------------------------
  相关解决方案