java - JList/JTable 作为按钮面板

标签 java swing jtable jscrollpane jlist

我即将制作一个新的应用程序,我偶然发现了一个非常酷的侧边栏(按钮面板),我将尝试添加到该应用程序(它是 Spotify 上的左侧边栏)。我好奇的是原理(看一下附图)。
顶部有 3 个按钮(我假设它是 JList 或 JTable 项目)。

中间有一个“YOUR MUSIC”标题(也许是 JLabel??)

我们还有一个新的播放列表(JButton)

然后是播放列表列表(JList 还是 JTable?)

我想知道的有趣的事情是:

首先,三个 JList(playlistList、appList 和 yourmusicList,我假设它的 JList)共享相同的 JScrollPane,因此 JList/JTables(或无论它是什么)必须根据它包含的项目数量更改大小(高度) 。 (例如:播放列表列表的高度取决于是否有 3 个或 17 个播放列表。)

其次:我设法在同一个 JPanel 中添加按钮、标签、JList,但我不知道如何为它们提供共享的 JScrollPane(当我尝试为两个 JList 提供相同的 ScrollPane 时出现问题)。

我认为,我对应包含项目的 JPanel 使用了错误的 LayoutManager,并且使用了错误类型的组件(JList、JTable)。如果有一个名为 JSideBar 之类的 Swing 组件,那就太好了!

嗯,就是这样!没有代码示例,因为它不会以任何方式真正受益

问候奥利弗

enter image description here

最佳答案

[...] but i could not figure out how to give them a shared JScrollPane (The problem occured when i tried to give two JLists same ScrollPane).

您可以将所有组件添加到 JPanel 中,并将其设置为 JScrollPane View 端口。看看How to Use Scroll Panes教程。

<小时/>

If theres a Swing Component called JSideBar or something, it would be great!

默认情况下不提供,但使用 JPanel 并使用 BoxLayout 制作自己的可滚动侧边栏非常容易。 。查看教程:How to Use BoxLayout 。例如:

import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * @author dic19
 */
public class JSideBar extends JComponent {

    private final JPanel content;

    public JSideBar(boolean scrollable) {
        super();

        content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

        this.setLayout(new BorderLayout());

        if(scrollable) {
            this.add(new JScrollPane(content));
        } else {
            this.add(content);
        }

    }

    /**
     * Adds a component to this side bar.
     * 
     * @param comp The component.
     * @param alignment One of {@code Component.LEFT_ALIGNMENT, Component.CENTER_ALIGNMENT, Component.RIGHT_ALIGNMENT}
     * 
     * @see java.awt.Component#LEFT_ALIGNMENT
     * @see java.awt.Component#CENTER_ALIGNMENT
     * @see java.awt.Component#RIGHT_ALIGNMENT
     */
    public void addItem(JComponent comp, float alignment) {
        comp.setAlignmentX(alignment);
        content.add(comp);
        if(content.isShowing()) {
            revalidate();
            repaint();
        }
    }

    /**
     * Adds a vertical space to this side bar.
     * @param height Height of vertical space.
     */
    public void addVerticalSpace(int height) {
        content.add(Box.createVerticalStrut(height));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); 
        Graphics graphics = g.create();
        graphics.setColor(getBackground());
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.dispose();
    }
}

这是根据您的情况使用的示例:

JLabel appFinderLabel = new JLabel("AppFinder");        
JLabel musicxMatchLabel = new JLabel("musicXmatch");
JLabel tuneWikiLabel = new JLabel("TuneWiki");

JLabel yourMusicLabel = new JLabel("YOUR MUSIC");
JLabel songsLabel = new JLabel("Songs");
JLabel albumsLabel = new JLabel("Albums");
JLabel artistsLabel = new JLabel("Artists");
JLabel localFilesLabel = new JLabel("Local Files");

/* 
 * Set icons, background and stuff if you need to
 * A good option would be use undecorated JButtons instead of JLabels
 */

JSideBar sideBar = new JSideBar(true);
sideBar.addItem(appFinderLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(musicxMatchLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(tuneWikiLabel, Component.LEFT_ALIGNMENT);

sideBar.addVerticalSpace(20);

sideBar.addItem(yourMusicLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(songsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(albumsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(artistsLabel, Component.LEFT_ALIGNMENT);
sideBar.addItem(localFilesLabel, Component.LEFT_ALIGNMENT);

屏幕截图

请注意滚动条:

enter image description here

关于java - JList/JTable 作为按钮面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090858/

相关文章:

java - 如何制作每行多行的表格?

java - wildfly 集群上的 Websocket 问题

java - 如何将java对象转换为clojure中的映射?

java - 更新复杂 JTable、TableModel 等的正确方法

swing - JNLP FileSaveService 打开文件打开对话框

java - JTable 中的类型错误

java - 无法获取服务器中的参数

java - 最后有时让我感到困惑

Java 比较器总是将值读取为字符串

java - JTable 多个标题行