程序员被很多人说不会制造浪漫,只会一天到晚写程序,其实大家对程序员还是不了解,程序员要是浪漫起来,会让很多人羡慕,比如用java代码表白,那用java如何写表白对话框?下面来我们就来给大家讲解一下。
主界面
Frame.java
效果展示:
代码如下:
public class Frame extends JFrame implements ActionListener { JButton button; String message = "LOVE"; String message2 = "YOU"; String message3 = "想念你笑颜如花 将你深深迷恋 "; String message4 = "想给你日日温暖 将你时时保护 "; String message5 = "想给你万千繁华 将你浓浓爱恋 "; String message6 = "想给你刻刻关怀 将你久久拥抱 "; JLabel messageLabel = new JLabel(message); JLabel messageLabel2 = new JLabel(message2); JLabel messageLabel3 = new JLabel(message3); JLabel messageLabel4 = new JLabel(message4); JLabel messageLabel5 = new JLabel(message5); JLabel messageLabel6 = new JLabel(message6); JLabel messageLabel7 = new JLabel("xxx"); JLabel messageLabel8 = new JLabel("The heart is mine. But it is full of you."); JLabel messageLabel9 = new JLabel("XXX"); JLabel messageLabel10 = new JLabel("2020"); JLabel messageLabel11 = new JLabel("0214"); JLabel label = new JLabel(new ImageIcon("love.jpg")); JLabel title = new JLabel("情人节快乐"); Frame() { button = new JButton("开始"); button.addActionListener(this); addComponent(); setTitle("LOVE"); setSize(500, 400); setVisible(true); setResizable(false); setLocationRelativeTo(null); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } private void addComponent() { setLayout(null); title.setFont(new Font("黑体", Font.BOLD, 16)); title.setForeground(Color.RED); title.setBounds(200, 10, 100, 20); add(title); label.setBounds(-130, -30, 750, 400); add(label); button.setFont(new Font("楷体", Font.BOLD, 16)); button.setForeground(Color.GRAY); button.setBounds(200, 320, 70, 20); add(button); Frame() { button = new JButton("开始"); button.addActionListener(this); addComponent(); setTitle("LOVE"); setSize(500, 400); setVisible(true); setResizable(false); setLocationRelativeTo(null); validate(); setDefaultCloseOperation(DISPOSE_ON_CLOSE); } private void addComponent() { setLayout(null); title.setFont(new Font("黑体", Font.BOLD, 16)); title.setForeground(Color.RED); title.setBounds(200, 10, 100, 20); add(title); label.setBounds(-130, -30, 750, 400); add(label); button.setFont(new Font("楷体", Font.BOLD, 16)); button.setForeground(Color.GRAY); button.setBounds(200, 320, 70, 20); add(button);
MyLOVE.java
动画窗口:(此处放截图,运行时效果更好)
public class MyLove extends JFrame implements Runnable { /** * */ private static final long serialVersionUID = -2421767851330648008 L; public MyLove() { this.setTitle("LOVE"); this.setBounds(-10, 0, 2000, 2000); this.setBackground(Color.BLACK); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void paint(Graphics g) { double x, y, r; Image OffScreen = createImage(2000, 2000); Graphics drawOffScreen = OffScreen.getGraphics(); for (int i = 0; i < 400; i++) { for (int j = 0; j < 400; j++) { r = Math.PI / 45 * i * (1 - Math.sin(Math.PI / 45 * j)) * 18; x = r * Math.cos(Math.PI / 45 * j) * Math.sin(Math.PI / 45 * i) + 900; y = -r * Math.sin(Math.PI / 45 * j) + 200; drawOffScreen.setColor(Color.RED); drawOffScreen.fillOval((int) x, (int) y, 2, 2); } g.drawImage(OffScreen, 0, 0, this); } } public void run() { while (true) { try { Thread.sleep(2000); this.repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
这样就完成了java写表白对话框的步骤,如果你也遇到想要表白的人却不知道怎么办,可以参考以上的操作哦!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。
推荐阅读:
java中级开发工程师标准是什么?如何进阶为中级开发工程师?
python多线程面试题有哪些?python多线程面试题汇总