[英]Swing: JList holding multiple Components as a single item
這是我的問題:我正在創建一個負責列出圖層的窗口,該窗口在其中顯示該圖層的當前圖像(截至目前為ImageIcon
的形式),圖層的名稱以及一個復選框,用於更改當前的表示層的/關閉狀態。 整個過程應該是Paint.NET的Layer Window的仿制品 ,如下所示:
我的問題是如何繞過JTable
來構造它。 最后,我想我可能不得不求助於創建一個動態表,但是我想知道是否有一種方法可以使一個可以單獨顯示這三個組件的項目/容器。
我得到的最接近的是使用JLabel
,它使用了圖標和文本屬性,但是我很難弄清楚如何添加復選框。
我是否應該使用布局管理器將標簽列表移至左側,並在窗格內添加一個充滿復選框的新列表?
我的代碼可能如下:
public class StudioLayerWindow extends JFrame
{
// Objects
JPanel buttonPanel;
JScrollPane layerScroll;
JButton addNewLayer;
JButton deleteCurrentLayer;
JButton duplicateCurrentLayer;
JButton mergeCurrentLayer;
JButton moveCurrentLayerUp;
JButton moveCurrentLayerDown;
JButton layerProperties;
// Constructors & Initializers
public StudioLayerWindow()
{
// Main Window Initialization
this.setTitle("Layers");
this.setType(Type.UTILITY);
this.setSize(200,200);
this.setResizable(false);
this.setAlwaysOnTop(true);
initButtons();
buttonPanel = new JPanel(new GridLayout(1,7));
buttonPanel.setPreferredSize(new Dimension(this.getWidth(), this.getHeight() / 7));
buttonPanel.add(addNewLayer);
buttonPanel.add(deleteCurrentLayer);
buttonPanel.add(duplicateCurrentLayer);
buttonPanel.add(mergeCurrentLayer);
buttonPanel.add(moveCurrentLayerUp);
buttonPanel.add(moveCurrentLayerDown);
buttonPanel.add(layerProperties);
// Code for what I'd add here
layerScroll = new JScrollPane();
this.add(layerScroll , BorderLayout.CENTER);
this.add(buttonPanel , BorderLayout.PAGE_END);
}
上面的代碼不包含任何我嘗試過的解決方案,僅包含我所使用的基本模板。
有沒有辦法在一行上制作多個組件?
JList
是泛型類。
JPanel
的類。 (讓我們稱它為RowPanel
) 使用以下面板創建JList
JList<RowPanel> list = new JList<RowPanel>();
ListCellRenderer
和ListCellEditor
: https : //docs.oracle.com/javase/tutorial/uiswing/components/list.html 注意:如果您不想在任何情況下使用JTable
,都應該這樣做。 JTable
是此解決方案的不錯替代方案。
希望這可以幫助。
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.