单击时更改JButton文本

我创建了一个扩展JDialog的类,我有一些复选框和3个按钮:accept,cancel和select all。

当用户单击全选时,应检查每个复选框,如果再次单击,则应取消选中每个复选框。 这工作正常,但我也希望按钮的文本在“全选”和“取消全选”之间切换。 我在那里遇到麻烦,所以当用户点击按钮并且文本更改为“取消全选”时,按钮会消失。

我在这里将课程简化为最简单的forms:

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(); 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; } } 

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

按钮不会消失,只是变得太宽而无法放入窗口。 只需在更改按钮标签时重新绘制组件:

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

event.getSource()访问单击的按钮

另一种方法是使用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; } }