Created
May 17, 2018 14:15
-
-
Save yukeehan/d161ef068ed2f7b5f20feaee08dde314 to your computer and use it in GitHub Desktop.
check box Swing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class CBDemo implements ItemListener { | |
JLabel jlabSelected; | |
JLabel jlabChanged; | |
JCheckBox jcbAlpha; | |
JCheckBox jcbBeta; | |
JCheckBox jcbGamma; | |
CBDemo() { | |
// Create a new JFrame container. | |
JFrame jfrm = new JFrame("Demonstrate Check Boxes"); | |
// Specify FlowLayout for the layout manager. | |
jfrm.setLayout(new FlowLayout()); | |
// Give the frame an initial size. | |
jfrm.setSize(280, 120); | |
// Terminate the program when the user closes the application. | |
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// Create empty labels. | |
jlabSelected = new JLabel(""); | |
jlabChanged = new JLabel(""); | |
// Make check boxes. | |
jcbAlpha = new JCheckBox("Alpha"); | |
jcbBeta = new JCheckBox("Beta"); | |
jcbGamma = new JCheckBox("Gamma"); | |
// Events generated by the check boxes | |
// are handled in common by the itemStateChanged() | |
// method implemented by CBDemo. | |
jcbAlpha.addItemListener(this); | |
jcbBeta.addItemListener(this); | |
jcbGamma.addItemListener(this); | |
// Add checkboxes and labels to the content pane. | |
jfrm.add(jcbAlpha); | |
jfrm.add(jcbBeta); | |
jfrm.add(jcbGamma); | |
jfrm.add(jlabChanged); | |
jfrm.add(jlabSelected); | |
// Display the frame. | |
jfrm.setVisible(true); | |
} | |
// This is the handler for the check boxes. | |
public void itemStateChanged(ItemEvent ie) { | |
String str = ""; | |
// Obtain a reference to the check box that | |
// caused the event. | |
JCheckBox cb = (JCheckBox) ie.getItem(); | |
// Report what check box changed. | |
if(cb.isSelected()) | |
jlabChanged.setText(cb.getText() + " was just selected."); | |
else | |
jlabChanged.setText(cb.getText() + " was just cleared."); | |
// Report all selected boxes. | |
if(jcbAlpha.isSelected()) { | |
str += "Alpha "; | |
} | |
if(jcbBeta.isSelected()) { | |
str += "Beta "; | |
} | |
if(jcbGamma.isSelected()) { | |
str += "Gamma"; | |
} | |
jlabSelected.setText("Selected check boxes: " + str); | |
} | |
public static void main(String args[]) { | |
// Create the frame on the event dispatching thread. | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
new CBDemo(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment