2013-12-16 165 views
-1

有一個帶按鈕和textField的JPanel。 TextField按名稱過濾不必要的按鈕。但是在刪除之後,休息按鈕的大小正在改變。 在textField中過濾後,按鈕的高度不得改變。如何實現這一目標?固定大小的按鈕

public class TestViewer { 
public static void main(String[] args) {EventQueue.invokeLater(new Runnable() { 


public void run() { 

JDialog dialog = new TestDialog(); 
dialog.setLocationRelativeTo(null); 
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 
dialog.setTitle("Type text and press Enter"); 
dialog.setSize(300, 700); 
dialog.setVisible(true); 
dialog.setLocationRelativeTo(null); 
dialog.setModal(true); 
} 
}); 
} 
} 

class TestDialog extends JDialog { 

public TestDialog() { 
    getContentPane().add(new JPan 

el(), BorderLayout.CENTER); 
    getContentPane().add(createBtnPanel(), BorderLayout.CENTER); 
} 



private JPanel createBtnPanel() { 
     int n = 100; 
    Final String ArrButtonNames[] = new String[n]; 
for (int h = 0; h < ArrButtonNames.length; h++) { 
    if ((h%2)==0){ 
     ArrButtonNames[h]="Filter"+h; 
    } 
    else{ 
     ArrButtonNames[h]="Button"+h; 
    } 
} 

final JButton ArrButton[] = new JButton[n]; 
final JPanel btnPanel = new JPanel(new GridLayout(0, ArrButtonNames.length, 1,1)); 
btnPanel.setLayout(new GridLayout(0, 1)); 
final JTextField textField = new JTextField(); 
textField.setColumns(23); 
btnPanel.add(textField); 
for (int i = 0; i < ArrButtonNames.length; i++) { 
    String btnString = ArrButtonNames[i]; 
    JButton button = new JButton(btnString); 

    Dimension d = button.getPreferredSize(); 
    d.setSize(d.getWidth(), d.getHeight()*1); 
    button.setPreferredSize(d); 
    button.setSize(d); 
    button.setMinimumSize(d); 
    button.setMaximumSize(d); 

    btnPanel.add(button); 
    ArrButton[i] = button; 
} 
textField.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
    for(int k = 0; k < ArrButtonNames.length; k++) { 
     if(ArrButtonNames[k].indexOf(textField.getText())==-1) { 
      btnPanel.remove(ArrButton[k]); 
      btnPanel.revalidate(); 
      btnPanel.repaint(); 
     } 
    } 
} 
}); 

JPanel MainPanel = new JPanel(); 
MainPanel.setLayout(new BorderLayout()); 
MainPanel.add(btnPanel, BorderLayout.NORTH); 
final JScrollPane scrollPane = new JScrollPane(btnPanel); 
MainPanel.add(scrollPane, BorderLayout.CENTER); 
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
    return MainPanel; 
} 
} 

回答

1

您有效果,因爲你用GridLayout,該調整組件,以填補整個細胞。

爲了您的目的,我建議您使用GridBagLayout,這可以做你想做的。

嘗試使用下面的代碼來充實你的btnPanel,而不是你的:

final JButton ArrButton[] = new JButton[n]; 
    final JPanel btnPanel = new JPanel(); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.anchor = GridBagConstraints.WEST; 
    c.fill = GridBagConstraints.HORIZONTAL; 
    c.weightx = 1; 
    c.gridx=0; 
    c.gridy=0; 
    btnPanel.setLayout(new GridBagLayout()); 
    final JTextField textField = new JTextField(); 
    btnPanel.add(textField,c); 
    for (int i = 0; i < ArrButtonNames.length; i++) { 
     String btnString = ArrButtonNames[i]; 
     JButton button = new JButton(btnString); 
     c.gridy ++; 
     btnPanel.add(button,c); 
     ArrButton[i] = button; 
    } 
    c.fill = GridBagConstraints.BOTH; 
    c.weighty = 1; 
    c.gridy ++; 
    btnPanel.add(new JLabel(""),c); 

它看起來像:

enter image description here

也用它代替setSize(...)pack()方法,也不要使用setPreferedSize(...)和其他大小的方法讀取有關該here

+0

感謝您的建議,它的工作原理!以及如何使面板相應地減少其大小的按鈕數量? – user3106328

+0

你可以在你的'ActionListener'中爲'JTextField'使用'TestDialog.this.pack();' – alex2410