单击时更改JButton文本 [英] Changing a JButton text when clicked

查看:40
本文介绍了单击时更改JButton文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个扩展JDialog的类,其中有一些复选框和3个按钮:接受,取消和全选.

I have created a class which extends JDialog, where I have some checkboxes and 3 buttons: accept, cancel, and select all.

当用户单击全选时,应选中每个复选框,如果再次单击,则应取消选中每个复选框.效果很好,但我还希望按钮的文本在全选"和全选"之间切换.我在这里遇到了麻烦,因此当用户单击按钮并将文本更改为取消全选"时,该按钮消失了.

When user clicks select all, every checkbox should get checked, and if clicked again, every checkbox should get unchecked. That's working fine, but i also want the text of the button to change between "select all" and "unselect all". I'm having troubles there, so when user clicks the button and text changes to "unselect all", the button dissapears.

我在这里将类简化为最简单的形式:

I have reduced the class to its simplest form here:

    public class NodeSelectionCheckBoxJDialog extends JDialog {
    public enum Options {ACEPT, CANCEL};
    private Options selectedOption;
    private JButton allButton;
    private boolean allCheckBoxesSelected;
    private JButton aceptButton;

    public NodeSelectionCheckBoxJDialog(){
        super(MainFrame.getInstance());
        this.setTitle("Select nodes to apply");
        this.setModal(true);

        selectedOption = Options.CANCEL;
        nodeCheckBoxesSet = new HashSet<NodeCheckBox>();

        try {
            initComponents();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    private void initComponents() throws Exception {
        this.getContentPane().add(createActionButtons(), BorderLayout.SOUTH);
    }

    private Component createActionButtons() {
        JPanel buttonsPanel = new JPanel();
        allCheckBoxesSelected = false;
        aceptButton = new JButton("Accept");
        aceptButton.setEnabled(false);
        buttonsPanel.add(aceptButton);
        aceptButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                selectedOption = Options.ACEPT;
                dispose();
            }
        });

        JButton cancelButton = new JButton("Cancel");
        buttonsPanel.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                selectedOption = Options.CANCEL;
                dispose();
            }
        });

        allButton = new JButton("Select all");
        buttonsPanel.add(allButton);
        allButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(allCheckBoxesSelected){
                    allCheckBoxesSelected = false;
                    allButton.setText("Select all");
                } else {
                    allCheckBoxesSelected = true;
                    allButton.setText("Unselect all");
                }
            }
        });

        return buttonsPanel;
    }
}

我看不到有什么问题.有帮助吗?

I can't see what's wrong. Any help?

推荐答案

按钮不会消失,只是变得太宽而无法容纳在窗口中.只需在更改按钮标签时重新绘制组件即可:

The button does not disappear, it just becomes too wide to fit in the window. Just redraw the component when changing the button label :

@Override
public void actionPerformed(ActionEvent e) {
    if(allCheckBoxesSelected){
        allCheckBoxesSelected = false;
        allButton.setText("Select all");
    } else {
        allCheckBoxesSelected = true;
        allButton.setText("Unselect all");
        NodeSelectionCheckBoxJDialog.this.pack();
    }
}

这篇关于单击时更改JButton文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆