jframe单选框判断是否勾选
时间: 2023-09-28 09:13:50 浏览: 125
在Java Swing中,可以通过JRadioButton和ButtonGroup实现单选框的选择。若要判断单选框是否被选中,可以使用isSelected()方法来判断。
具体实现方法如下:
1.首先创建一个JRadioButton对象,例如:
JRadioButton radioButton = new JRadioButton("单选框");
2.创建一个ButtonGroup对象,并将JRadioButton对象添加到ButtonGroup中,例如:
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioButton);
3.判断单选框是否被选中,例如:
if (radioButton.isSelected()) {
System.out.println("单选框被选中");
} else {
System.out.println("单选框未被选中");
}
以上代码片段可以在Java Swing应用程序的ActionListener事件中使用,以便在用户单击按钮时执行相应的操作。
相关问题
javaswing单选框获取数据
在Java Swing中,可以使用JRadioButton组件来创建单选框。要获取单选框的选中数据,可以使用ButtonGroup类的getSelection()方法。具体代码如下:
```java
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class RadioButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("RadioButton Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");
JRadioButton radioButton3 = new JRadioButton("Option 3");
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
frame.add(radioButton1);
frame.add(radioButton2);
frame.add(radioButton3);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
// 获取选中的单选框的数据
String selectedOption = group.getSelection().getActionCommand();
System.out.println("Selected option: " + selectedOption);
}
}
```
运行以上代码,将会创建一个包含三个单选框的窗口。当用户选择其中一个单选框时,程序将会打印出选中的单选框的数据。
给jtable添加单选框的代码
以下是给JTable添加单选框的示例代码:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class JTableWithRadioButtonExample {
private JFrame frame;
private JTable table;
private MyTableModel model;
private MyCellRenderer renderer;
private List<Boolean> checkedList;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableWithRadioButtonExample window = new JTableWithRadioButtonExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JTableWithRadioButtonExample() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("Check");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < checkedList.size(); i++) {
System.out.println("Row " + i + ": " + checkedList.get(i));
}
}
});
panel.add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
model = new MyTableModel();
model.addColumn("Name");
model.addColumn("Select");
renderer = new MyCellRenderer();
table = new JTable(model) {
public TableCellRenderer getCellRenderer(int row, int column) {
if (column == 1) {
return renderer;
} else {
return super.getCellRenderer(row, column);
}
}
public Class getColumnClass(int column) {
if (column == 1) {
return JCheckBox.class;
} else {
return super.getColumnClass(column);
}
}
};
scrollPane.setViewportView(table);
checkedList = new ArrayList<Boolean>();
model.addRow(new Object[] {"John", new Boolean(false)});
checkedList.add(new Boolean(false));
model.addRow(new Object[] {"Jane", new Boolean(false)});
checkedList.add(new Boolean(false));
model.addRow(new Object[] {"Jim", new Boolean(false)});
checkedList.add(new Boolean(false));
}
class MyTableModel extends DefaultTableModel {
public boolean isCellEditable(int row, int col) {
if (col == 1) {
return true;
} else {
return false;
}
}
}
class MyCellRenderer implements TableCellRenderer {
private JCheckBox box;
public MyCellRenderer() {
box = new JCheckBox();
}
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
box.setSelected((Boolean)value);
return box;
}
}
}
```
这段代码会创建一个含有单选框的JTable,并且每个单选框对应一个布尔值,当单选框被选中时,布尔值为true,否则为false。用户可以通过点击按钮来打印所有行的布尔值,以检查哪些单选框被选中了。
阅读全文