java实现qq秀,给出代码示例
时间: 2023-11-18 11:04:17 浏览: 136
非常感谢您的问题,以下是我为您提供的Java代码示例,用于实现QQ秀:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class QQShow extends JFrame {
private static final long serialVersionUID = 1L;
private JButton button1, button2;
private ImageIcon icon1, icon2;
private JLabel label;
public QQShow() {
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("QQ秀");
setLayout(new FlowLayout());
icon1 = new ImageIcon("img/1.jpg"); // 请替换图片路径
icon2 = new ImageIcon("img/2.jpg"); // 请替换图片路径
button1 = new JButton(icon1);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setIcon(icon1);
setTitle("我是秀1");
}
});
button2 = new JButton(icon2);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setIcon(icon2);
setTitle("我是秀2");
}
});
label = new JLabel();
add(button1);
add(button2);
add(label);
setVisible(true);
}
public static void main(String[] args) {
new QQShow();
}
}
```
在上述代码中,我们使用了Java GUI库Swing来创建窗口和按钮,并在窗口中显示两个图片按钮,当用户点击按钮时,会在窗口中显示对应的QQ秀图片。请注意替换代码中图片的路径来正确引用您的图片文件。希望以上代码能够对您有所帮助。
阅读全文