Java广泛的应用于生活中,很多企业在开发项目的时候就会使用java,不过java的用处可不止这些,还可以利用Java写表白代码哦,那用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进行表白的话,就可以参考以上这些方法,新颖有创意,肯定对你有帮助!最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。
推荐阅读: