package swings;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JSample extends JFrame implements ActionListener {
JCheckBox[] check;
public static void main(String[] args) {
JSample frame = new JSample("MyTitle");
frame.setVisible(true);
}
JSample(String title) {
setTitle(title);
setBounds(100, 100, 600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
check = new JCheckBox[3];
check[0] = new JCheckBox("Travel");
check[1] = new JCheckBox("Movie");
check[2] = new JCheckBox("Sports");
JPanel p = new JPanel();
p.add(check[0]);
p.add(check[1]);
p.add(check[2]);
JButton button = new JButton("Check");
button.addActionListener(this);
JPanel jPanel = new JPanel();
jPanel.add(button);
Container container = getContentPane();
container.add(p, BorderLayout.CENTER);
container.add(jPanel, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < check.length; i++) {
sb.append(check[i].getText());
if (check[i].isSelected()) {
sb.append("[Select]");
} else {
sb.append("[Not Select]");
}
sb.append(" ");
}
JOptionPane.showMessageDialog(this, new String(sb));
}
}
勾选项目后点击check按钮