Java运用jnavlcj实现音乐和视频的播放器1主界面设计
白雨青工作站发文地址:Java运用jna、vlcj实现音乐和视频的播放器1-主界面设计-白雨青工作站
一、播放器主框架 public class MainFrame { public JFrame frame; public int width = 100; public int height = 350; public MainFrame(){ frame = new JFrame(); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(height,width); frame.setLocationRelativeTo(null); frame.setLayout(null); // 去掉窗口的装饰 frame.setUndecorated(true); //采用指定的窗口装饰风格 frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); //设置窗体圆角,最后两个参数分别为圆角的宽度、高度数值,一般这两个数值都是一样的 AWTUtilities.setWindowShape(frame, new RoundRectangle2D.Double(0.0D, 0.0D, frame.getWidth(), frame.getHeight(), 20.0D, 20.0D)); //设置背景颜色,记住一定要修改frame.getContentPane()的颜色,因为我们看到的都是这个的颜色而并不是frame的颜色 frame.getContentPane().setBackground(Color.LIGHT_GRAY); frame.setVisible(true); } }
效果:
二、绘制自定义窗口,添加最小化和关闭按钮
1、自定义一个button类继承JButton import javax.swing.JButton; public class DIYButton extends JButton{ public DIYButton() { super(); // TODO Auto-generated constructor stub // 不绘制边框 setBorderPainted(false); //按钮设置为透明,这样就不会挡着后面的背景 setContentAreaFilled(false); } }
2、自定义一个panel,里面存放logo,窗口名称,最小化,关闭按钮 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MainFrameBar extends JPanel implements ActionListener{ private JLabel titleLabel; private DIYButton win_btn_logo; private DIYButton win_btn_min; private DIYButton win_btn_close; public MainFrameBar(){ win_btn_logo = new DIYButton(); win_btn_logo.setBounds(10,5,16,16); win_btn_logo.setIcon(new ImageIcon("icon/win_btn_logo.png")); titleLabel = new JLabel(); titleLabel.setBounds(28,3,80,20); win_btn_min = new DIYButton(); win_btn_min.setBounds(300,5,16,16); win_btn_min.setIcon(new ImageIcon("icon/win_btn_min.png")); win_btn_close = new DIYButton(); win_btn_close.setBounds(320,5,16,16); win_btn_close.setIcon(new ImageIcon("icon/win_btn_close.png")); setBackground(Color.gray); setLayout(null); add(titleLabel); add(win_btn_logo); add(win_btn_min); add(win_btn_close); win_btn_min.setActionCommand("min"); win_btn_close.setActionCommand("close"); win_btn_min.addActionListener(this); win_btn_close.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand() == "min") { MainFrame.getFrame().setExtendedState(JFrame.ICONIFIED); } if(e.getActionCommand() == "close") { System.exit(0); } } public JLabel getTitleLabel() { return titleLabel; } public void setTitleLabel(JLabel titleLabel) { this.titleLabel = titleLabel; } public DIYButton getWin_btn_logo() { return win_btn_logo; } public void setWin_btn_logo(DIYButton winBtnLogo) { win_btn_logo = winBtnLogo; } public DIYButton getWin_btn_min() { return win_btn_min; } public void setWin_btn_min(DIYButton winBtnMin) { win_btn_min = winBtnMin; } public DIYButton getWin_btn_close() { return win_btn_close; } public void setWin_btn_close(DIYButton winBtnClose) { win_btn_close = winBtnClose; } }
效果:
三、绘制中间区域
1、自定义播放器进度条 import java.awt.Color; import java.awt.Font; import javax.swing.JLabel; import javax.swing.JPanel; /* * 播放器进度条 * */ public class Rate extends JPanel { private JLabel rate; public Rate() { super(); // TODO Auto-generated constructor stub setLayout(null); setBackground(new Color(255,255,255)); rate = new JLabel("", JLabel.CENTER); rate.setForeground(new Color(255,255,255)); rate.setVisible(true); rate.setFont(new Font(Font.DIALOG, 0, 25)); add(rate); setVisible(true); } }
2、进度条风格 import javax.swing.*; import javax.swing.plaf.basic.BasicSliderUI; import java.awt.*; public class SliderStyle extends BasicSliderUI { public SliderStyle(JSlider jSlider) { super(jSlider); } // 重绘游标 public void paintThumb(Graphics g) { Graphics2D g2d = (Graphics2D) g; BasicStroke stroke = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2d.setStroke(stroke); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1)); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint gp = new GradientPaint(0, 0, new Color(255,255,255), 0, thumbRect.height, new Color(255,255,255)); g2d.setPaint(gp); g2d.fillOval(thumbRect.x, thumbRect.y + 5, 10, 10); BasicStroke stroke1 = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2d.setStroke(stroke1); g2d.drawLine(0, thumbRect.height / 2, thumbRect.x + 8, thumbRect.height / 2); } // 重绘进度条 public void paintTrack(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);// 设定渐变 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f)); g2d.setPaint(new GradientPaint(0, 0, new Color(200, 200, 200), 0, trackRect.height, new Color(255, 255, 255), true)); g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); g2d.drawLine(8, trackRect.height / 2 + 1, trackRect.width + 8, trackRect.height / 2 + 1); } }
3、自定义一个panel,存放上一首、下一首、播放暂停按钮、当前时间、总时间、进度条、歌曲名称 import java.awt.Color; import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JSlider; import com.byq.player.ui.component.Rate; import com.byq.player.ui.style.SliderStyle; public class MainFrameContain extends JPanel implements ActionListener{ private DIYButton win_btn_prev; private DIYButton win_btn_play; private DIYButton win_btn_next; private Rate rate; // 进度条 private JLabel mediaName; private JLabel currentTime; private JLabel totalTime; private JSlider playProcessSlider; private int len = 10; public MainFrameContain(){ win_btn_prev = new DIYButton(); win_btn_prev.setBounds(len,3,40,40); win_btn_prev.setIcon(new ImageIcon("icon/win_btn_prev.png")); win_btn_prev.setActionCommand("prev"); add(win_btn_prev); win_btn_play = new DIYButton(); win_btn_play.setBounds(len+50,3,40,40); win_btn_play.setIcon(new ImageIcon("icon/win_btn_play.png")); win_btn_play.setActionCommand("play"); add(win_btn_play); win_btn_next = new DIYButton(); win_btn_next.setBounds(len+100,3,40,40); win_btn_next.setIcon(new ImageIcon("icon/win_btn_next.png")); win_btn_next.setActionCommand("next"); add(win_btn_next); //歌名 mediaName = new JLabel(); mediaName.setBounds(len+165,-5, 300, 30); // mediaName.setForeground(new Color(255,255,255)); add(mediaName); rate = new Rate(); rate.setBounds(len+200,30,100,3); add(rate); setBackground(Color.gray); setLayout(null); // 当前时间 currentTime = new JLabel("00:00"); currentTime.setBounds(len+165,15, 50, 30); currentTime.setForeground(new Color(255,255,255)); add(currentTime); // 结束时间 totalTime = new JLabel("00:00"); totalTime.setBounds(len+305,15, 50, 30); totalTime.setForeground(new Color(255,255,255)); add(totalTime); playProcessSlider = new JSlider(0,100,0); playProcessSlider.setBounds(len+200,20,100,24); playProcessSlider.setUI(new SliderStyle(playProcessSlider)); playProcessSlider.setBackground(new Color(125,125,125)); playProcessSlider.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); add(playProcessSlider); win_btn_prev.addActionListener(this); win_btn_play.addActionListener(this); win_btn_next.addActionListener(this); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand() == "prev") { System.out.println("prev"); } if(e.getActionCommand() == "play") { System.out.println("play"); win_btn_play.setIcon(new ImageIcon("icon/win_btn_pause.png")); win_btn_play.setActionCommand("pause"); } if(e.getActionCommand() == "next") { System.out.println("next"); } if(e.getActionCommand() == "pause") { System.out.println("pause"); win_btn_play.setIcon(new ImageIcon("icon/win_btn_play.png")); win_btn_play.setActionCommand("play"); } } }
效果:
四、绘制下面区域,菜单列表按钮,声音控制条
1、自定义音量控制条 import java.awt.Color; import java.awt.Font; import javax.swing.JLabel; import javax.swing.JPanel; /* * 声音进度条 * */ public class Volume extends JPanel { private JLabel volume; public Volume() { super(); // TODO Auto-generated constructor stub setLayout(null); setBackground(new Color(255,255,255)); volume = new JLabel("", JLabel.CENTER); volume.setForeground(new Color(255,255,255)); volume.setVisible(true); volume.setFont(new Font(Font.DIALOG, 0, 25)); add(volume); setVisible(true); } }
2、自定义一个panel,存放音量控制条和菜单按钮 import java.awt.Color; import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.swing.JSlider; import com.byq.player.ui.component.Volume; import com.byq.player.ui.style.SliderStyle; public class MainFrameBottom extends JPanel implements ActionListener{ private DIYButton win_btn_list; private DIYButton win_btn_volume; private Volume volume; private JSlider volumeSlider; private int len = 10; public MainFrameBottom() { // TODO Auto-generated constructor stub win_btn_list = new DIYButton(); win_btn_list.setBounds(len,5,16,16); win_btn_list.setIcon(new ImageIcon("icon/win_btn_list.png")); win_btn_list.setActionCommand("listOff"); add(win_btn_list); win_btn_volume = new DIYButton(); win_btn_volume.setBounds(len+180,5,16,16); win_btn_volume.setIcon(new ImageIcon("icon/win_btn_volumeOn.png")); win_btn_volume.setActionCommand("volumeOn"); add(win_btn_volume); volume = new Volume(); volume.setBounds(len+200,12,100,3); add(volume); volumeSlider = new JSlider(0,100,0); volumeSlider.setValue(20);//设置默认音量100 volumeSlider.setBounds(len+200,3,100,24); volumeSlider.setUI(new SliderStyle(volumeSlider)); volumeSlider.setBackground(new Color(125,125,125)); volumeSlider.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); add(volumeSlider); setBackground(Color.gray); setLayout(null); } public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
效果: