单击秋千中的按钮获取复选框值 [英] Getting checkbox values on clicking the button in swings
本文介绍了单击秋千中的按钮获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下情况下,我需要使用Java中的一小段代码:
I need a small code in java for the following scenario:
按钮应获取选中的复选框并执行以下代码 我的表单中的那些复选框.
Button should get the selected checkboxes and executing the code for those checkboxes in my form.
推荐答案
以下是我为您制作的一个示例:
Here is an example I made for you:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class TestJCheckBox {
private JFrame frame;
private JCheckBox jcb;
private JButton button;
public TestJCheckBox() {
initComponents();
}
private void initComponents() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
jcb = new JCheckBox("JCheckBox1");
button = new JButton("Is JCheckBox selected?");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (jcb.isSelected()) {
JOptionPane.showMessageDialog(frame, "JCheckBox is selected");
} else {
JOptionPane.showMessageDialog(frame, "JCheckBox is NOT selected");
}
}
});
frame.add(jcb, BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJCheckBox();
}
});
}
}
这篇关于单击秋千中的按钮获取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文