2012-02-23 71 views
2

我創建了一個擴展JDialog的類,其中有一些複選框和3個按鈕:接受,取消和全選。單擊時更改JButton文本

當用戶點擊選擇全部時,每個複選框應該被檢查,如果再次點擊,每個複選框應該被取消選中。這工作正常,但我也希望在「全選」和「全部取消」之間切換按鈕的文本。我在那裏遇到麻煩,所以當用戶單擊按鈕並且文本更改爲「取消全選」時,該按鈕消失。

我有類減少到這裏最簡單的形式:

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

我看不出有什麼不對。任何幫助?

+0

哪裏是:只是改變了按鈕標籤時重繪組件你的代碼的其餘部分?你如何選擇/取消選擇複選框?你是否在其他地方操作'allButton'變量? – Marcelo 2012-02-23 12:03:10

+0

我有一個複選框的偵聽器,但我刪除它使代碼更簡單。選擇/取消選擇工作正常,只有當我也嘗試更改按鈕文本時纔會出現問題。不,allButton不會在其他地方被操縱 – 2012-02-23 12:06:27

回答

5

按鈕不會消失,它只是變得太寬,以適應窗口。

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

作品!謝謝 :) – 2012-02-23 12:20:53

1

event.getSource()來訪問點擊按鈕

+0

這相當於我接近它的方式。編譯,但問題仍然存在,按鈕消失。 – 2012-02-23 12:10:45

2

另一種方法是使用ButtonModel的是Implemeted一個例子有趣的方法

enter image description here

enter image description here

enter image description here

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