java 加入图片代码_java为如何加入图片

本文展示了如何在Java的JFrame中插入图片。通过创建一个LoginFrame类,使用JPanel和GridLayout布局管理器,以及组件如JComboBox、JRadioButton等,实现了一个简单的登录界面。代码中并未直接涉及图片插入,但可以在此基础上添加ImageIcon组件来插入图片。
摘要由CSDN通过智能技术生成

如图我要在里面插入图片!代码如下,请大侠们写一些注释!publicclassLoginFrameextendsJFrame{publicJTextFieldtextField_name=null;publicJPasswordFieldpasswordField=null;JRadio...

如图我要在里面插入图片!

代码如下,请大侠们写一些注释!

public class LoginFrame extends JFrame {

public JTextField textField_name = null;

public JPasswordField passwordField = null;

JRadioButton radioButton1 = null;

JRadioButton radioButton2 = null;

public JComboBox comboBox = null;

public String text = new String("null");

public ButtonGroup buttonGroup = null;

public LoginFrame() {

insert();

this.setTitle("图书馆登录");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(new Dimension(365, 350));

this.setLocationRelativeTo(null);// 显示器的居中

this.setVisible(true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Java图片播放器加入音乐的实现步骤: 1. 创建一个JFrame窗口,设置标题和大小。 2. 创建一个JPanel面板,用于展示图片和音乐控制按钮。 3. 创建一个JButton按钮,用于打开图片和音乐文件。 4. 创建一个JFileChooser文件选择器,用于选择图片和音乐文件。 5. 创建一个ImageIcon对象,用于加载图片文件。 6. 创建一个JLabel标签,用于展示图片。 7. 创建一个JButton按钮,用于播放音乐。 8. 创建一个JFileChooser文件选择器,用于选择音乐文件。 9. 创建一个Clip对象,用于控制音乐播放。 10. 在播放按钮的ActionListener中,打开文件选择器,选择音乐文件,加载音乐文件,并播放音乐。 11. 将面板添加到窗口中,并设置布局。 下面是示例代码: ``` import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.filechooser.FileNameExtensionFilter; public class ImagePlayerWithMusic extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel panel; private JButton openButton; private JLabel imageLabel; private JButton playButton; private JFileChooser imageChooser; private JFileChooser musicChooser; private Clip clip; public ImagePlayerWithMusic() { super("图片播放器"); setSize(400, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); panel = new JPanel(); openButton = new JButton("打开"); imageLabel = new JLabel(); playButton = new JButton("播放"); imageChooser = new JFileChooser(); musicChooser = new JFileChooser(); FileNameExtensionFilter imageFilter = new FileNameExtensionFilter( "图片文件", "jpg", "png"); imageChooser.setFileFilter(imageFilter); FileNameExtensionFilter musicFilter = new FileNameExtensionFilter( "音乐文件", "mp3", "wav"); musicChooser.setFileFilter(musicFilter); openButton.addActionListener(this); playButton.addActionListener(this); panel.setLayout(new BorderLayout()); panel.add(imageLabel, BorderLayout.CENTER); panel.add(playButton, BorderLayout.NORTH); panel.add(openButton, BorderLayout.SOUTH); add(panel); } public void actionPerformed(ActionEvent e) { if (e.getSource() == openButton) { int imageResult = imageChooser.showOpenDialog(this); if (imageResult == JFileChooser.APPROVE_OPTION) { File imageFile = imageChooser.getSelectedFile(); ImageIcon imageIcon = new ImageIcon(imageFile.getAbsolutePath()); imageLabel.setIcon(imageIcon); } int musicResult = musicChooser.showOpenDialog(this); if (musicResult == JFileChooser.APPROVE_OPTION) { File musicFile = musicChooser.getSelectedFile(); try { clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(musicFile)); } catch (Exception ex) { ex.printStackTrace(); } } } else if (e.getSource() == playButton) { if (clip != null) { clip.start(); } } } public static void main(String[] args) { ImagePlayerWithMusic player = new ImagePlayerWithMusic(); player.setVisible(true); } } ``` 运行程序后,点击打开按钮选择图片和音乐文件,点击播放按钮即可播放音乐。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值