Java 如何在swing中制作youtube视频列表

Java 如何在swing中制作youtube视频列表,java,swing,user-interface,Java,Swing,User Interface,我正在尝试为桌面制作一个类似youtube的应用程序。我有一个包含视频的数据库,我已经实现了我的搜索功能。问题是我不知道如何让界面显示视频列表,这样我就可以点击其中一个并播放它。我想知道是否有一个GUI元素,我可以使用它来添加几个条目,并在youtube将您的订阅中的视频显示为列表时显示它们 我对用户界面还不熟悉,所以提前对noob问题表示抱歉。JPanels和FlowLayout,就这样。。。 创建和JPanel,表示一个视频,我们称之为视频容器,它有JLabel作为标题、视图和作者。你为每一

我正在尝试为桌面制作一个类似youtube的应用程序。我有一个包含视频的数据库,我已经实现了我的搜索功能。问题是我不知道如何让界面显示视频列表,这样我就可以点击其中一个并播放它。我想知道是否有一个GUI元素,我可以使用它来添加几个条目,并在youtube将您的订阅中的视频显示为列表时显示它们

我对用户界面还不熟悉,所以提前对noob问题表示抱歉。

JPanels和FlowLayout,就这样。。。 创建和
JPanel
,表示一个视频,我们称之为视频容器,它有
JLabel
作为标题、视图和作者。你为每一个视频制作一个JPanel,它们必须很小,比如YouTube上的那些。现在要安排这些,诀窍是,
JPanel
,您希望这些视频容器所在的位置,必须具有相同的宽度,或者比视频容器大一点,并使用
FlowLayout
。使用固定宽度和
FlowLayout
,您的视频容器将垂直堆叠在彼此下方,就像YouTube中的屏幕右侧一样

演示 注意,我使用带有Y_轴的BoxLayout来堆叠视频,而不是FlowLayout方法。它是有效的,看看它:D

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.plaf.basic.BasicBorders;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.text.NumberFormat;
import java.util.Locale;

public class YoutubeLikeUI {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel contentPane = new JPanel(new BorderLayout(0,0));
        frame.setContentPane(contentPane);

        // This is the part than you're interested in
        // Adding video to the panel
        JPanel videos = new JPanel();
        // Important, make videos stack under each other / vertically
        BoxLayout layout = new BoxLayout(videos, BoxLayout.Y_AXIS);
        videos.setLayout(layout);
        // Here we are adding the individual videos to the so called "list"
        // You can change the JPanel "video" to an JScrollPanel and make the video list scrollable
        videos.add(new Video("Why choose mechanical keyboard?", 4975742, "Showing Tech 64",
                "https://images.freeimages.com/images/large-previews/5ce/my-keyboard-1241023.jpg"));
        videos.add(new Video("Ports explained", 9284573, "Showing Tech 64",
                "https://images.freeimages.com/images/large-previews/7ed/the-back-side-of-my-external-dvd-rom-1519905.jpg"));
        videos.add(new Video("Java 4 life. Here's why!", 7947173, "Showing Tech 64",
                "https://www.filecluster.com/howto/wp-content/uploads/2014/08/java-logo.jpg"));
        videos.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEmptyBorder(8, 0, 8, 0), null));
        contentPane.add(videos, BorderLayout.EAST);
        // That was it, the magic, down below you see a class Video, look that up also

        // Nothing exciting, a demo video, just for the looks :)
        JPanel demoVideo = new JPanel();
        BoxLayout layout2 = new BoxLayout(demoVideo, BoxLayout.Y_AXIS);
        demoVideo.setLayout(layout2);

        // Search bar
        JTextField search = new JTextField() {
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                if (getText().length() == 0) {
                    int h = getHeight();
                    ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    Insets ins = getInsets();
                    FontMetrics fm = g.getFontMetrics();
                    int c0 = getBackground().getRGB();
                    int c1 = getForeground().getRGB();
                    int m = 0xfefefefe;
                    int c2 = ((c0 & m) >>> 1) + ((c1 & m) >>> 1);
                    g.setColor(new Color(c2, true));
                    g.drawString("Search anything", ins.left, h / 2 + fm.getAscent() / 2 - 2);
                }
            }
        };
        search.setFont(new Font("Arial", Font.PLAIN, 24));
        search.setBackground(new Color(192, 192, 192, 255));
        search.setForeground(Color.WHITE);
        search.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEmptyBorder(8, 8, 8, 8),
                BorderFactory.createLineBorder(Color.white)));
        contentPane.add(search, BorderLayout.NORTH);

        // Video
        demoVideo.add(new JLabel(new ImageIcon(getImage("https://dclottery.com/img/play-button-overlay.png").
                getScaledInstance(640, 360, Image.SCALE_SMOOTH))));
        JPanel info = new JPanel(new BorderLayout(8, 8));

        // Title
        JLabel title = new JLabel("The art of lighting. Get professional");
        title.setFont(new Font("Arial", Font.BOLD, 20));
        demoVideo.add(title);

        // Views
        JLabel views = new JLabel("8,195,384 views");
        views.setFont(new Font("Arial", Font.PLAIN, 18));
        demoVideo.add(views);

        // Author
        JLabel author = new JLabel(" Showing Tech 64 ");
        author.setFont(new Font("Arial", Font.BOLD, 12));
        author.setBorder(BorderFactory.createLineBorder(Color.lightGray));
        author.setBackground(new Color(0, 0, 0, 64));
        demoVideo.add(author);

        demoVideo.add(info);
        // Add gap around the content pane
        demoVideo.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEmptyBorder(8, 8, 8, 8), null));
        contentPane.add(demoVideo, BorderLayout.LINE_START);

        // JFrame info
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static class Video extends JPanel {
        String name, author, imageURL;
        int views;
        BufferedImage image;

        public Video(String title, int views, String author, String imageURL) {
            super(new BorderLayout(8, 8));
            // Set the size of the video panel
            setMaximumSize(new Dimension(460, 96));
            setMinimumSize(new Dimension(460, 96));
            setPreferredSize(new Dimension(460, 96));

            // Initialize variables, add your own if needed
            this.name = title;
            this.views = views;
            this.author = author;
            this.imageURL = imageURL;

            // Get image, remove it and pass the image directly as an argument if needed
            image = getImage(imageURL);

            // Add components, here's the magic
            JLabel thumbnail = new JLabel(new ImageIcon(image.getScaledInstance(170, 96, Image.SCALE_DEFAULT)));
            thumbnail.setPreferredSize(new Dimension(170, 96));
            add(thumbnail, BorderLayout.WEST);

            JPanel info = new JPanel();
            BoxLayout layout = new BoxLayout(info, BoxLayout.Y_AXIS);
            info.setLayout(layout);

            JLabel titleLabel = new JLabel(makeWebSafe(title));
            titleLabel.setForeground(Color.black);
            titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
            titleLabel.setToolTipText(title);
            info.add(titleLabel);
            JLabel authorLabel = new JLabel(makeWebSafe(author));
            authorLabel.setForeground(Color.gray);
            authorLabel.setFont(new Font("Arial", Font.PLAIN, 14));
            info.add(authorLabel);
            JLabel viewsTitle = new JLabel(makeWebSafe(NumberFormat.getNumberInstance(Locale.US).format(views)+" views"));
            viewsTitle.setForeground(new Color(64, 128, 64));
            viewsTitle.setFont(new Font("Arial", Font.PLAIN, 12));
            info.add(viewsTitle);

            add(info, BorderLayout.CENTER);

            // This is here to add a little gap between the videos in the list
            setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(0, 2, 2, 2), null));
        }
    }

    // Inserting strings to an html without telling it, that it is not an piece of code, is very dangerous, use this method!
    // I used it to show the information about videos, because the name of the video might
    // contain html
    public static String makeWebSafe(String string) {
        return string.replace("<", "&lt;").replace(">", "&gt;");
    }

    // Download the image from the url, the image will not be stored as a file
    public static BufferedImage getImage(String imageURL) {
        try { return ImageIO.read(new URL(imageURL));
        } catch (Exception e) {
            return null;
        }
    }
}
导入javax.imageio.imageio;
导入javax.swing.*;
导入javax.swing.plaf.basic.BasicBorders;
导入java.awt.*;
导入java.awt.image.buffereImage;
导入java.net.URL;
导入java.text.NumberFormat;
导入java.util.Locale;
公共类YoutubeLikeUI{
公共静态void main(字符串[]args){
JFrame=新JFrame();
JPanel contentPane=新的JPanel(新的BorderLayout(0,0));
frame.setContentPane(contentPane);
//这是你不感兴趣的部分
//将视频添加到面板
JPanel视频=新的JPanel();
//重要信息,使视频相互堆叠/垂直堆叠
BoxLayout=新的BoxLayout(视频,BoxLayout.Y_轴);
视频。设置布局(布局);
//在这里,我们将个别视频添加到所谓的“列表”中
//您可以将JPanel“视频”更改为JScrollPanel,并使视频列表可滚动
添加(新视频(“为什么选择机械键盘?”),4975742,“显示技术64”,
"https://images.freeimages.com/images/large-previews/5ce/my-keyboard-1241023.jpg"));
添加(新视频(“解释端口”,9284573,“显示技术64”,
"https://images.freeimages.com/images/large-previews/7ed/the-back-side-of-my-external-dvd-rom-1519905.jpg"));
videos.add(新视频(“Java4Life.There'sWhy!”),7947173,“Showingtech64”,
"https://www.filecluster.com/howto/wp-content/uploads/2014/08/java-logo.jpg"));
videos.setBorder(BorderFactory.createCompoundBorder(
createEmptyByOrder(8,0,8,0),null);
contentPane.add(视频,BorderLayout.EAST);
//就是这样,魔术,在下面你可以看到一个课堂视频,也可以向上看
//没有什么令人兴奋的,只是一个演示视频,只是为了外观:)
JPanel demoVideo=新的JPanel();
BoxLayout layout2=新的BoxLayout(demoVideo,BoxLayout.Y_轴);
demoVideo.setLayout(layout2);
//搜索栏
JTextField search=新的JTextField(){
@凌驾
公共空间涂料(图g){
超级油漆(g);
如果(getText().length()==0){
inth=getHeight();
((Graphics2D)g).setRenderingHint(RenderingHits.KEY\u TEXT\u ANTIALIAS,RenderingHits.VALUE\u TEXT\u ANTIALIAS\u ON);
Insets-ins=getInsets();
FontMetrics fm=g.getFontMetrics();
int c0=getBackground().getRGB();
int c1=get前台().getRGB();
int m=0xfefe;
int c2=((c0&m)>>>1)+((c1&m)>>>1);
g、 setColor(新颜色(c2,真));
g、 抽绳(“搜索任何东西”,ins.left,h/2+fm.getAscent()/2-2);
}
}
};
search.setFont(新字体(“Arial”,Font.PLAIN,24));
search.setBackground(新颜色(192192192255));
search.setForeground(颜色:白色);
search.setboorder(BorderFactory.createCompoundBorder(
CreateEmptyByOrder(8,8,8,8),
BorderFactory.createLineBorder(Color.white));
添加(搜索,BorderLayout.NORTH);
//录像带
demoVideo.add(新JLabel(新图像)图标(getImage(“https://dclottery.com/img/play-button-overlay.png").
getScaledInstance(640360,Image.SCALE_SMOOTH));
JPanel info=新JPanel(新边界布局(8,8));
//头衔
JLabel title=新JLabel(“照明艺术,变得专业”);
title.setFont(新字体(“Arial”,Font.BOLD,20));
添加(标题);
//观点
JLabel视图=新JLabel(“8195384视图”);
views.setFont(新字体(“Arial”,Font.PLAIN,18));
demoVideo.add(视图);
//作者
JLabel author=新JLabel(“显示技术64”);
author.setFont(新字体(“Arial”,Font.BOLD,12));
author.setboorder(BorderFactory.createLineBorder(Color.lightGray));
作者:挫折背景(新颜色(0,0,0,64));
demoVideo.add(作者);
demoVideo.add(信息);
//在内容窗格周围添加间隙
demoVideo.setBorder(BorderFactory.createCompoundBorder(
createEmptyByOrder(8,8,8,8),null);
contentPane.add(demoVideo,BorderLayout.LINE_START);
//JFrame信息
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
公共统计