我發現自己寫了不少最近的程序,都需要顯示一些數據的集合。到目前爲止,我認爲最好的方法是創建包含集合中每個項目數據的小JPanel,並將它們全部放入一個大的JPanel中,然後放入一個JScrollPane中。它的工作原理和看起來一樣,但有一個問題:我似乎無法讓較小的JPanel從較大的JPanel頂部開始。在列表中組織JPanels的最佳方式是什麼?
問題是唯一明顯的,當我已經有了少量的小JPanels(綠色)加入到更大的JPanel(紅色)。
下面描述的是我用於生產上述的方法,我想知道是否有更好的方法我能做到這一點(在列表從頂部開始像它應該):
我創建了一個擴展JPanel的類,並在其中添加了我想要顯示的所有數據。我們將它稱爲「SmallPanel.java」。我沒有設置它的大小(稍後)。
在我的主窗口的類(它擴展的JFrame):
private JScrollPane scrollPane; private JPanel panel; ... scrollPane = new JScrollPane(); getContentPane().add(scrollPane); panel = new JPanel(); panel.setLayout(new GridBagLayout()); scrollPane.setViewportView(panel); ... private void addPanel() { GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = panel.getComponentCount(); //The new JPanel's place in the list gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.PAGE_START; //I thought this would do it gbc.ipady = 130; //Set the panel's height, the width will get set to that of the container JPanel (which is what I want since I'd like my JFrames to be resizable) gbc.insets = new Insets(2, 0, 2, 0); //Separation between JPanels in the list gbc.weightx = 1.0; SmallPanel smallPanel = new SmallPanel(); panel.add(smallPanel, gbc); panel.revalidate(); panel.invalidate(); panel.repaint(); //Better safe than peeved }
調用addPanel()方法,我想添加一個面板每次。
編輯
最終解決方案(基於以下MadProgrammer的答案):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
public class ListPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private JPanel fillerPanel;
private ArrayList<JPanel> panels;
public ListPanel(List<JPanel> panels, int height)
{
this(panels, height, new Insets(2, 0, 2, 0));
}
public ListPanel(List<JPanel> panels, int height, Insets insets)
{
this();
for (JPanel panel : panels)
addPanel(panel, height, insets);
}
public ListPanel()
{
super();
this.fillerPanel = new JPanel();
this.fillerPanel.setMinimumSize(new Dimension(0, 0));
this.panels = new ArrayList<JPanel>();
setLayout(new GridBagLayout());
}
public void addPanel(JPanel p, int height)
{
addPanel(p, height, new Insets(2, 0, 2, 0));
}
public void addPanel(JPanel p, int height, Insets insets)
{
super.remove(fillerPanel);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = getComponentCount();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.PAGE_START;
gbc.ipady = height;
gbc.insets = insets;
gbc.weightx = 1.0;
panels.add(p);
add(p, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = getComponentCount();
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1.0;
add(fillerPanel, gbc);
revalidate();
invalidate();
repaint();
}
public void removePanel(JPanel p)
{
removePanel(panels.indexOf(p));
}
public void removePanel(int i)
{
super.remove(i);
panels.remove(i);
revalidate();
invalidate();
repaint();
}
public ArrayList<JPanel> getPanels()
{
return this.panels;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setMinimumSize(new Dimension(500, 500));
f.setLocationRelativeTo(null);
f.getContentPane().setLayout(new BorderLayout());
final ListPanel listPanel = new ListPanel();
for (int i = 1; i <= 10; i++)
listPanel.addPanel(getRandomJPanel(), new Random().nextInt(50) + 50);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent paramActionEvent)
{
listPanel.addPanel(getRandomJPanel(), new Random().nextInt(50) + 50);
}
});
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent paramActionEvent)
{
listPanel.removePanel(0);
}
});
f.getContentPane().add(btnRemove, BorderLayout.NORTH);
f.getContentPane().add(btnAdd, BorderLayout.SOUTH);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(listPanel);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setVisible(true);
}
public static JPanel getRandomJPanel()
{
JPanel panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
panel.add(new JLabel("This is a randomly sized JPanel"));
panel.setBackground(new Color(new Random().nextFloat(), new Random().nextFloat(), new Random().nextFloat()));
return panel;
}
}
爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 –
@AndrewThompson我認爲這對於SSCCE來說太籠統了,但我盡力了(嘿,我得到了我需要的答案)。 –