我有以下声明:
public JPanel compPanes[] = new JPanel[13];
public JPanel userPanes[] = new JPanel[13];
public int cNum = 0;
现在,我有一种方法,它将四个JPanels
添加到另一个面板,直到它有四个面板(只是方法的一个较短的替代版本):
public void addCompPanels(JPanel compPane)
{
Random rand = new Random();
cPanelNum = (int)(Math.round(Math.random() * 10 + 1));
compPanes[cNum] = new JPanel();
compPanes[cNum].setPreferredSize(new Dimension(80, 90));
compPanes[cNum].add(new JLabel());
JLabel label = (JLabel)compPanes[cNum].getComponent(0);
label.setFont(pickFont);
label.setText("" +cPanelNum);
compPanes[cNum].setBackground(color);
compPane.add(compPanes[cNum]);
compPane.revalidate();
cNum++;
}
总共添加四个面板后,我想根据JLabel
的整数值对面板进行排序。
我尝试了如何对整数进行排序,尝试它是否有效(因为我认为它适用于JPanels
)。但它似乎不适用,因为它返回一个空指针异常。
for(int i = 0; i < compPanes.length; i++){
for(int j = i+1; j < compPanes.length; j++){
JLabel lblOne = (JLabel)compPanes[i].getComponent(0);
JLabel lblTwo = (JLabel)compPanes[j].getComponent(0);
if((Integer.parseInt(lblOne.getText())) >(Integer.parseInt(lblTwo.getText())));
{
tempPanel = compPanes[i];
compPanes[i] = compPanes[j];
compPanes[j] = tempPanel;
compPane.add(compPanes[i]);
compPane.add(compPanes[j]);
compPane.revalidate();
compPane.repaint();
}
}
}
直观表示:
交换之前
交换后
谢谢。
答案 0 :(得分:1)
因为你问了一个例子我会添加一个简单的例子。请注意,它将有点伪代码,因此您必须根据自己的需要进行调整(主要目的是教导不提供易于使用的代码):
Collection<JPanel> panels = ...;
Collections.sort( panels, new Comparatory<JPanel>() {
public int compare( JPanel o1, JPanel o2) {
JLabel l1 = o1.getLabel();
JLabel l2 = o2.getLabel();
if( l1 != null && l2 != null ) {
//note that the text might be null as well, so handle this
//further note that if the text represents multi-digit numbers you might have to parse them first
return l1.getText().compareTo( l2.getText() );
}
else ... //decide how to handle null labels
}
};
如果您想直接对数组进行排序,请使用Arrays.sort( array, comparator )
。请注意,我不确定您是否会以某种方式干扰某些Swing组件内部(我现在无法深入了解Swing以检查它)。
答案 1 :(得分:1)
这是一个简短的测试程序,使用Comparator基于JLabel文本对JPanel进行排序,就像上面的建议一样
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.Comparator;
import javax.swing.*;
class Main {
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGui();
}
});
}
private static void createAndShowGui()
{
JPanel panel[] = new JPanel[4];
for(int i = 0; i != panel.length; ++i)
{
int num = (int) (Math.round(Math.random() * 10 + 1));
JLabel lbl = new JLabel("" + num);
panel[i] = new JPanel();
panel[i].add(lbl);
panel[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
Arrays.sort(panel, new JPanelSort());
JFrame frame = new JFrame("Sort JPanel Example");
frame.setLayout(new FlowLayout());
for(int i = 0; i != panel.length; ++i)
frame.add(panel[i]);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
}
private static class JPanelSort implements Comparator<JPanel>
{
@Override
public int compare(JPanel arg0, JPanel arg1)
{
String firstNum = ((JLabel) arg0.getComponent(0)).getText();
String secondNum = ((JLabel) arg1.getComponent(0)).getText();
return Integer.compare(Integer.parseInt(firstNum), Integer.parseInt(secondNum));
}
}
}