固定按钮大小

时间:2013-12-16 07:10:15

标签: java swing button

有一个带按钮和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 个答案:

答案 0 :(得分: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

另外使用pack()方法代替setSize(...),并且不要使用setPreferedSize(...)和其他尺寸方法读取here