import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import javax.swing.JButton;
/*作者:wangcx
*日期:2019年6月10日
*时间:下午6:35:12
*/
public class GUI123 implements ActionListener {
private Frame f;
private Button redbtu,greenbtu,bluebtu,resetbtu;
JButton btu;
public GUI123() {
f=new Frame("红绿蓝");
redbtu=new Button("红色");
greenbtu=new Button("绿色");
bluebtu=new Button("蓝色");
resetbtu=new Button("JB");
//btu.setToolTipText("登录到服务器");//设置组件提示信息
f.add(redbtu);
f.add(greenbtu);
f.add(bluebtu);
f.add(resetbtu);
f.setBounds(400, 200, 400, 400);
f.setLayout(new FlowLayout());
f.setVisible(true);
redbtu.addActionListener(this);
greenbtu.addActionListener(this);
bluebtu.addActionListener(this);
resetbtu.addActionListener(this);
redbtu.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2) {
redbtu.setBackground(Color.BLUE);
f.setBackground(Color.BLUE);
System.out.println("123双击!");
}
}
});
greenbtu.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
f.setBackground(Color.white);
System.out.println("鼠标退出");
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
f.setBackground(Color.GREEN);
System.out.println("鼠标按下");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标进入");
f.setBackground(Color.yellow);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
f.dispose();
}
});
redbtu.addMouseListener(new MouseAdapter() {
});
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new GUI123();
}
@Override
public void actionPerformed(ActionEvent w) {
// TODO Auto-generated method stub
//有两种办法
Object obj1=w.getActionCommand();//获取的是String 中的标签文字
Object obj=w.getSource();//获取的是组件对象
if(obj1 =="红色") {
f.setBackground(Color.RED);
}
if(obj ==redbtu) {
f.setBackground(Color.RED);
}
}
}
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
常见资源合集和破解beqptwpmc...