聖誕表白小程式
阿新 • • 發佈:2018-12-30
高調測試程式:
package 表白小程式; 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("AXiang,你是不是喜歡我?"); //設定標籤文字居中 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("回答我:"); } }