java:表白,挑逗窗体程序
Posted VipMao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java:表白,挑逗窗体程序相关的知识,希望对你有一定的参考价值。
package Ch08;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//定义WindowListener事件
class MyWindowListener extends WindowAdapter
//窗体关闭过程中触发函数
public void windowClosing(WindowEvent e)
new MyJDialog().setVisible(true);
System.exit(0);;
//定义关闭窗体时需要弹出的对话框
class MyJDialog extends JDialog
public MyJDialog()
super(new JFrame(),"提示:",true);
Container c=getContentPane();
JLabel label=new JLabel("你以为关闭就能回避这个话题吗?");
label.setHorizontalAlignment(SwingConstants.CENTER);
JButton btnY=new JButton("是");
JButton btnN=new JButton("不是");
//添加按钮监听事件
btnN.addMouseListener(new MouseListener()
//鼠标点击、移上、移出、松开事件
@Override
public void mouseClicked(MouseEvent arg0)
// TODO Auto-generated method stub
@Override
public void mouseEntered(MouseEvent arg0)
// TODO Auto-generated method stub
btnN.setText("是");
@Override
public void mouseExited(MouseEvent arg0)
// TODO Auto-generated method stub
btnN.setText("不是");
@Override
public void mousePressed(MouseEvent arg0)
// TODO Auto-generated method stub
@Override
public void mouseReleased(MouseEvent arg0)
// TODO Auto-generated method stub
);
//添加按钮监听事件
btnN.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent arg0)
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
);
btnY.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent arg0)
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
);
btnY.setBounds(30, 100, 80, 30);
btnN.setBounds(158, 100, 80, 30);
c.add(btnY);
c.add(btnN);
c.add(label);
setSize(300, 200);
//主窗体
public class Test extends JFrame
public void creatJFrame(String title)
JFrame jf=new JFrame(title);
Container container=jf.getContentPane();
JLabel label=new JLabel("你是不是喜欢我?");
//设置标签文字居中
label.setHorizontalAlignment(SwingConstants.CENTER);
JButton btnY=new JButton("是");
JButton btnN=new JButton("不是");
//添加按钮监听事件
btnN.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent arg0)
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
);
btnY.addActionListener(new ActionListener()
@Override
public void actionPerformed(ActionEvent arg0)
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
);
btnN.addMouseListener(new MouseListener()
//鼠标点击、移上、移出、松开事件
@Override
public void mouseClicked(MouseEvent arg0)
// TODO Auto-generated method stub
@Override
public void mouseEntered(MouseEvent arg0)
// TODO Auto-generated method stub
btnN.setText("是");
@Override
public void mouseExited(MouseEvent arg0)
// TODO Auto-generated method stub
btnN.setText("不是");
@Override
public void mousePressed(MouseEvent arg0)
// TODO Auto-generated method stub
@Override
public void mouseReleased(MouseEvent arg0)
// TODO Auto-generated method stub
);
btnY.setBounds(30, 100, 80, 30);
btnN.setBounds(158, 100, 80, 30);
container.add(btnY);
container.add(btnN);
container.add(label);
jf.setSize(300, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.addWindowListener(new MyWindowListener());
public static void main(String[] args)
new Test().creatJFrame("回答我:");
运行结果:
当鼠标放在不是按钮上时:当对方不想选择想关闭窗体时:当对方无奈选择一个按钮时:
以上是关于java:表白,挑逗窗体程序的主要内容,如果未能解决你的问题,请参考以下文章