Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击时更改JButton文本_Java_Swing_Jbutton - Fatal编程技术网

Java 单击时更改JButton文本

Java 单击时更改JButton文本,java,swing,jbutton,Java,Swing,Jbutton,我已经创建了一个扩展JDialog的类,其中有一些复选框和3个按钮:接受、取消和全选 当用户单击全选时,应选中每个复选框,如果再次单击,则应取消选中每个复选框。这很好,但我也希望按钮的文本在“全部选择”和“全部取消选择”之间切换。我在那里遇到了问题,所以当用户单击按钮,文本变为“全部取消选择”时,按钮就会消失 我在这里将类简化为最简单的形式: public class NodeSelectionCheckBoxJDialog extends JDialog { public en

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

当用户单击全选时,应选中每个复选框,如果再次单击,则应取消选中每个复选框。这很好,但我也希望按钮的文本在“全部选择”和“全部取消选择”之间切换。我在那里遇到了问题,所以当用户单击按钮,文本变为“全部取消选择”时,按钮就会消失

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

    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;
    }
}
public类节点选择checkboxjdialog扩展JDialog{
公共枚举选项{ACEPT,CANCEL};
私人选择权;
私有JButton-allButton;
已选择专用布尔值所有复选框;
私有JButton aceptButton;
公共节点选择CheckBoxJDialog(){
super(MainFrame.getInstance());
此.setTitle(“选择要应用的节点”);
此.setModal(true);
selectedOption=Options.CANCEL;
nodeCheckBoxesSet=newhashset();
试一试{
初始化组件();
}捕获(例外e){
showMessageDialog(null,例如getMessage(),“Error”,JOptionPane.Error_MESSAGE);
}
这个包();
此.setLocationRelativeTo(空);
此.setVisible(true);
}
私有void initComponents()引发异常{
这个.getContentPane().add(createActionButtons(),BorderLayout.SOUTH);
}
私有组件createActionButtons(){
JPanel buttonPanel=新的JPanel();
allCheckBoxesSelected=false;
aceptButton=newjbutton(“接受”);
aceptButton.setEnabled(false);
按钮面板添加(aceptButton);
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
selectedOption=Options.ACEPT;
处置();
}
});
JButton cancelButton=新JButton(“取消”);
按钮面板添加(取消按钮);
cancelButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
selectedOption=Options.CANCEL;
处置();
}
});
allButton=新按钮(“全选”);
按钮面板添加(所有按钮);
allButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(选中所有复选框){
allCheckBoxesSelected=false;
allButton.setText(“全选”);
}否则{
allCheckBoxesSelected=true;
allButton.setText(“全部取消选择”);
}
}
});
返回按钮面板;
}
}

我看不出有什么问题。有任何帮助吗?

event.getSource()要访问单击的按钮,按钮不会消失,只是变得太宽,无法放入窗口中。更改按钮标签时只需重新绘制组件:

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

另一种方法是使用ButtonModel,例如实现有趣的方法

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TextAreaInButton {

    private JFrame frame = new JFrame("sssssssss");
    private JButton tip1Null = new JButton(" test button ");

    public TextAreaInButton() {
        Border line, raisedbevel, loweredbevel, title, empty;
        line = BorderFactory.createLineBorder(Color.black);
        raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();
        title = BorderFactory.createTitledBorder("");
        empty = BorderFactory.createEmptyBorder(1, 1, 1, 1);
        final Border compound;
        Color crl = (Color.blue);
        compound = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl));
        Color crl1 = (Color.red);
        final Border compound1;
        compound1 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl1));
        Color crl2 = (Color.black);
        final Border compound2;
        compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2));
        tip1Null.setFont(new Font("Serif", Font.BOLD, 14));
        tip1Null.setForeground(Color.darkGray);
        tip1Null.setPreferredSize(new Dimension(50, 30));
        tip1Null.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        tip1Null.setBorderPainted(true);
        tip1Null.setFocusPainted(false);
        tip1Null.setBorder(compound);
        tip1Null.setHorizontalTextPosition(SwingConstants.CENTER);
        tip1Null.setVerticalTextPosition(SwingConstants.BOTTOM);

        tip1Null.getModel().addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                ButtonModel model = (ButtonModel) e.getSource();
                if (model.isRollover()) {
                    tip1Null.setBorder(compound1);
                } else {
                    tip1Null.setBorder(compound);
                }
                if (model.isPressed()) {
                    tip1Null.setBorder(compound2);
                    String btnText = (tip1Null.getText());
                    if (btnText.equals("Selected")) {
                        tip1Null.setText("Un_Selected");
                    } else {
                        tip1Null.setText("Selected");
                    }
                }
            }
        });

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(tip1Null, BorderLayout.CENTER);
        frame.setLocation(150, 150);
        frame.setPreferredSize(new Dimension(310, 75));
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

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

            @Override
            public void run() {
                TextAreaInButton taib = new TextAreaInButton();
            }
        });
    }
}

class OldRoundedBorderLine extends AbstractBorder {

    private final static int MARGIN = 5;
    private static final long serialVersionUID = 1L;
    private Color color;

    OldRoundedBorderLine(Color clr) {
        color = clr;
    }

    public void setColor(Color clr) {
        color = clr;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        ((Graphics2D) g).setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(color);
        g.drawRoundRect(x, y, width, height, MARGIN, MARGIN);
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(MARGIN, MARGIN, MARGIN, MARGIN);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        insets.left = MARGIN;
        insets.top = MARGIN;
        insets.right = MARGIN;
        insets.bottom = MARGIN;
        return insets;
    }
}


你剩下的代码在哪里?如何选择/取消选择复选框?您是否在其他地方操作
allButton
变量?我有一个用于复选框的侦听器,但我删除了它以使代码更简单。选择/取消选择工作正常,仅当我尝试更改按钮文本时才会出现问题。不,allButton没有在其他地方被操纵,这相当于我接近它的方式。编译,但问题仍然存在,按钮消失。