java使用swing制作告白程序

项目文件图:

java使用swing制作告白程序_第1张图片

 运行效果:

java使用swing制作告白程序_第2张图片

 java使用swing制作告白程序_第3张图片

源代码:

/**
 * @author
 * @vision createtime:2018下午3:25:16
 */
package com.gaorufeng.facepanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.rmi.server.LoaderHandler;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * @author Administrator gaopu
 */
public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	
	protected int i = 0;
	
	String[] msgs = new String[] {"别想了", "想都别想", "还不同意?", "再不同意翻车了", "哈哈哈哈哈,没有办法我就是这么强大"};

	public void initLoveFrame() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.setSize(500, 300);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setLocationRelativeTo(null);
		this.setUndecorated(true);
		this.setResizable(false);
		
		JPanel jPanelPrimary = new JPanel();
		jPanelPrimary.setPreferredSize(new Dimension(500, 200));

		this.add(jPanelPrimary);
		
		// 上面的告白信息和图片
		JPanel jPanelTop = new JPanel() {

			@Override
			protected void paintComponent(Graphics g) {
				// 图片
				g.drawImage(new ImageIcon(MainFrame.class.getClassLoader().getResource("confession.jpg")).getImage(), 300, 50, 100, 100, null);
			}
		};
		// 文字
		JLabel jLabel0 = new JLabel("来自一位帅气小哥哥的邀请:");
		JLabel jLabel1 = new JLabel("小姐姐,观察你很久了");
		JLabel jLabel2 = new JLabel("做我女朋友好不好?");
		jLabel0.setFont(new Font("楷体", Font.PLAIN, 15));
		jLabel0.setForeground(Color.BLACK);
		jLabel1.setFont(new Font("楷体", Font.PLAIN, 22));
		jLabel1.setForeground(Color.BLUE);
		jLabel2.setFont(new Font("楷体", Font.PLAIN, 30));
		jLabel2.setForeground(Color.RED);
		jLabel0.setBounds(15, 10, 300, 50);
		jLabel1.setBounds(50, 60, 300, 50);
		jLabel2.setBounds(15, 100, 300, 50);
		
		// 将文字添加到上面
		jPanelTop.setLayout(null);
		jPanelTop.add(jLabel0);
		jPanelTop.add(jLabel1);
		jPanelTop.add(jLabel2);
		
		jPanelTop.setPreferredSize(new Dimension(500, 200));
		this.add(jPanelTop, BorderLayout.NORTH);
		// 下面的按钮选项
		JPanel jPanelBottom = new JPanel();
		
		JButton buttonOk = new JButton("好");
		JButton buttonNo = new JButton("不好");
		
		// 添加下面按钮
		jPanelBottom.setLayout(null);
		buttonOk.setFont(new Font("楷体", Font.BOLD, 18));
		buttonOk.setBounds(105, 25, 75, 50);
		buttonOk.setBorder(BorderFactory.createRaisedBevelBorder());
		buttonNo.setFont(new Font("楷体", Font.BOLD, 18));
		buttonNo.setBounds(305, 25, 75, 50);
		buttonNo.setBorder(BorderFactory.createRaisedBevelBorder());

		jPanelBottom.add(buttonNo);
		jPanelBottom.add(buttonOk);
		
		jPanelBottom.setPreferredSize(new Dimension(500, 100));
		this.add(jPanelBottom, BorderLayout.SOUTH);
		this.setVisible(true);

		buttonOk.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent var1) {
				JOptionPane.showMessageDialog(MainFrame.this.getContentPane(), "我也是这么想的>_<", "这么快就同意了?", JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
		});
		
		buttonNo.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent var1) {
				if (i == msgs.length) {
					i = 0;
				}
                JOptionPane.showMessageDialog(MainFrame.this.getContentPane(), msgs[i], "不再看看?", JOptionPane.WARNING_MESSAGE);
				i++;
			}
		});
		
	}
	
//	@Override
//	protected void processWindowEvent(WindowEvent e) {
//		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//			JOptionPane.showConfirmDialog(this, "再考虑一下吧 =_=", "提示", JOptionPane.OK_OPTION);
//		}
//	}

	public static void main(String[] args) {
		MainFrame mf = new MainFrame();
		mf.initLoveFrame();
	}
	
	
}

图片地址:https://download.csdn.net/download/gp3056/20112378

你可能感兴趣的:(java)