
ButtonGroup buttonGroup2 = new ButtonGroup()
JRadioButton select_ID = new JRadioButton()
JRadioButton select_Name = new JRadioButton()
JRadioButton select_Book = new JRadioButton()
JRadioButton select_All = new JRadioButton()
初始化时执行:
buttonGroup2.add(select_ID)
buttonGroup2.add(select_Book)
buttonGroup2.add(select_Name)
buttonGroup2.add(select_All)
判断是否选中:
select_Name.isSelected() 选中时返回true,否则false
用符号:m表示,我们走两步的距离约是 米,
import java.awt.EventQueueimport javax.swing.ButtonGroup
import javax.swing.JFrame
import javax.swing.JRadioButton
public class Test {
private JFrame frame
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test window = new Test()
window.frame.setVisible(true)
} catch (Exception e) {
e.printStackTrace()
}
}
})
}
/**
* Create the application.
*/
public Test() {
initialize()
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame()
frame.setBounds(100, 100, 450, 300)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.getContentPane().setLayout(null)
//****************************
//创建radioBtn
JRadioButton rdbtnNewRadioButton = new JRadioButton("aaa")
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("bbbb")
//设置btn位置
rdbtnNewRadioButton.setBounds(6, 126, 141, 23)
rdbtnNewRadioButton_1.setBounds(123, 126, 141, 23)
frame.getContentPane().add(rdbtnNewRadioButton)
frame.getContentPane().add(rdbtnNewRadioButton_1)
//分组radioBtn
ButtonGroup bg=new ButtonGroup()
bg.add(rdbtnNewRadioButton_1)
bg.add(rdbtnNewRadioButton)
}
}
你要设计的这个单选按钮应该不是说一个表里同时只能选一行吧。。。那你最好还是用checkbox复选框的显示方法替代单选按钮吧。。。因为单选按钮是没有办法点击取消的,一旦选中,就没有办法取消了。。。多蛋疼啊。。。给你一个复选框的例子吧,一样很好用的。
不多说了,直接上代码:
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.FlowLayout
import java.awt.Toolkit
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.JButton
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.TableColumn
public class CheckedBoxTabel extends JFrame {
private JTable table
private JScrollPane sPane
private DefaultTableModel model
private JButton button, button2
private JPanel pane
public CheckedBoxTabel() {
// TODO Auto-generated constructor stub
//窗口设置,不用管
this.setSize(400, 400)
Toolkit toolkit = Toolkit.getDefaultToolkit()
Dimension scrnsize
scrnsize = toolkit.getScreenSize()
setLocation(scrnsize.width / 2 - getWidth() / 2, scrnsize.height / 2
- getHeight() / 2)
this.setDefaultCloseOperation(EXIT_ON_CLOSE)
//table的model设置
model = new DefaultTableModel(new Object[][] {}, new String[] { "论文ID",
"论文标题", "是否提交", "论文负责人" })
//table加载model
table = new JTable(model)
//将第三列的显示设为checkbox类型,楼主想设置哪列自己决定
//但是记得该列的值是Boolean型的
TableColumn tc = table.getColumnModel().getColumn(2)
tc.setCellEditor(table.getDefaultEditor(Boolean.class))
tc.setCellRenderer(table.getDefaultRenderer(Boolean.class))
sPane = new JScrollPane()
sPane.setViewportView(table)
//添加增添一行按钮的事件处理
button = new JButton("加一行")
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
model = (DefaultTableModel) table.getModel()
Object[] data = new Object[4]
data[0] = "1"
data[1] = "论文"
data[2] = new Boolean(false)
data[3] = "张三"
model.addRow(data)
/*
//或者也可以用
model.addRow(new Object[]{"1","论文",new Boolean(false),"张三"})
*/
table.setModel(model)
}
})
//添加显示所选行的按钮的事件处理
button2 = new JButton("显示所选行")
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
model = (DefaultTableModel) table.getModel()
int count = model.getRowCount()
for (int i = 0i <counti++) {
if (model.getValueAt(i, 2).equals(true)){
//在这里添加你要处理行的方法
System.out.println("第" + (i + 1) + "行被选中")
}
}
}
})
pane = new JPanel()
pane.setLayout(new FlowLayout())
pane.add(button)
pane.add(button2)
this.add(sPane, BorderLayout.CENTER)
this.add(pane, BorderLayout.SOUTH)
}
public static void main(String[] args) {
new CheckedBoxTabel().setVisible(true)
}
}
我设计的测试结果是在控制台输出的,楼主可以自己修改:
第4行被选中
第7行被选中
第8行被选中
第10行被选中
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)