java swing中选中一个单选按钮 textfield 才能被编辑
private class ItemChangeListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent arg0) {
boolean enabled = selectField.isEnabled();
selectField.setEnabled(!enabled);
if (jRadioBtns[2].isSelected()) {
selectField.setText(“select,”);
} else {
selectField.setText(“”);
}
selectField.validate();
}
}
这是我的代码,你参考一下就可以了,就是添加ItemListener 就可以了
java中swing怎么将单选按钮的值返回给调用它的函数?
为单选按钮添加监听事件,当选中某个单击值时,你可以做一些你想做的事,下面是个简单的例子。有问题再追问吧,good lucky、!~
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class MyRadioButton extends JFrame implements ActionListener {
private JLabel lj = new JLabel(“默认”);
public MyRadioButton() {
// TODO Auto-generated constructor stub
Container c = this.getContentPane();
c.setLayout(new FlowLayout());
JRadioButton j1 = new JRadioButton(“男”);
JRadioButton j2 = new JRadioButton(“女”);
j1.addActionListener(this);
j2.addActionListener(this);
c.add(j1);
c.add(j2);
c.add(lj);
this.setSize(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String eSource = e.getActionCommand();
//在这里调用方法,进行赋值/传值
if(“男”.equals(eSource)) {
lj.setText(“男”);
}
if(“女”.equals(eSource)) {
lj.setText(“女”);
}
}
public static void main(String[] args) {
new MyRadioButton();
}
}
java中swt表格如何产生单选按钮列
已山寨,复制粘贴即可
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class RadioTable extends JPanel
{
private static final long serialVersionUID = 1L;
private final boolean DEBUG = true;
public RadioTable ()
{
super (new BorderLayout ());
JLabel type = new JLabel (“采集方式:”);
JComboBox cBox1 = new JComboBox (new String[] { “方式一”, “方式二” });
JLabel state = new JLabel (“采集状态:”);
JComboBox cBox2 = new JComboBox (new String[] { “状态一”, “状态二” });
JPanel top = new JPanel ();
top.add (type);
top.add (cBox1);
top.add (state);
top.add (cBox2);
add (top, BorderLayout.NORTH);
JTable table = new JTable (new Model ());
table.setDefaultRenderer (JRadioButton.class, new TableCellRenderer ()
{
@Override
public Component getTableCellRendererComponent ( JTable table, Object radioButton, boolean isSelected,
boolean hasFocus, int row, int column )
{
JRadioButton newButton = (JRadioButton) radioButton;
newButton.setSelected (isSelected);
TableColumn tableColumn = table.getColumnModel ().getColumn (0);
tableColumn.setResizable (false);
tableColumn.setPreferredWidth (15);
newButton.setBorder (BorderFactory.createMatteBorder (0, 10, 0, 0, getBackground ()));
return newButton;
}
});
table.setPreferredScrollableViewportSize (new Dimension (500, 70));
table.setFillsViewportHeight (true);
table.setSelectionMode (ListSelectionModel.SINGLE_SELECTION);
JScrollPane scrollPane = new JScrollPane (table);
add (scrollPane, BorderLayout.CENTER);
}
class Model extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
private String[] columnNames = { “选择”, “实例编号”, “采集模板”, “数据源”, “导出路径”, “起始时间” };
private Object[][] data = { { new JRadioButton (), 1, “223”, “4”, “1”, new Date () },
{ new JRadioButton (), 2, “23”, “34”, “224”, new Date () },
{ new JRadioButton (), 3, “水电费水电费”, “23”, “23”, new Date () },
{ new JRadioButton (), 4, “模板1”, “数据源01”, “导出路径”, new Date () } };
public int getColumnCount ()
{
return columnNames.length;
}
public int getRowCount ()
{
return data.length;
}
public String getColumnName ( int col )
{
return columnNames[col];
}
public Object getValueAt ( int row, int col )
{
return data[row][col];
}
public Class? extends Object getColumnClass ( int c )
{
return getValueAt (0, c).getClass ();
}
public boolean isCellEditable ( int row, int col )
{
return false;
}
public void setValueAt ( Object value, int row, int col )
{
if (DEBUG)
{
System.out.println (“Setting value at ” + row + “,” + col + ” to ” + value + ” (an instance of “
+ value.getClass () + “)”);
}
data[row][col] = value;
fireTableCellUpdated (row, col);
if (DEBUG)
{
System.out.println (“New value of data:”);
printDebugData ();
}
}
private void printDebugData ()
{
int numRows = getRowCount ();
int numCols = getColumnCount ();
for ( int i = 0; i numRows; i++ )
{
System.out.print (” row ” + i + “:”);
for ( int j = 0; j numCols; j++ )
{
System.out.print (” ” + data[i][j]);
}
System.out.println ();
}
System.out.println (“————————–“);
}
}
private static void createAndShowGUI ()
{
JFrame frame = new JFrame (“RadioTable”);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new RadioTable ();
newContentPane.setOpaque (true);
frame.setContentPane (newContentPane);
frame.pack ();
frame.setLocationRelativeTo (null);
frame.setResizable (false);
frame.setVisible (true);
}
public static void main ( String[] args )
{
javax.swing.SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
createAndShowGUI ();
}
});
}
}
java单选按钮传值
单选按钮(JRadioButton)的功能与单选框相似。使用单选按钮的方法是将一些单选按钮用ButtonGroup对象分组,使同一组的单选按钮只允许有一个被选中。单选按钮与单选框的差异是显示的样式不同,单选按钮是一个圆形的按钮,单选框是一个小方框。
JRadioButton类的常用构造方法有以下几个:
1.JRadioButton():用空标题构造单选按钮。
2.JRadioButton(String s):用给定的标题s构造单选按钮。
3.JRadioButton(String s,boolean b):用给定的标题s构造单选按钮,参数b设置选中与否的初始状态。
单选按钮使用时需要使用ButtonGroup将单选按钮分组,单选按钮的分组方法是先创建对象,然后将同组的单选按钮添加到同一个ButtonGroup对象中。参见例6.2程序的子类panel1的声明,组内有3个单选按钮。
java单选按钮传值的示例:
package com.lw;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class JRadioButtonDemo extends JFrame {
private static final long serialVersionUID = 8854703659153206227L;
private JPanel contentPane;
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel(“com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel”);
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JRadioButtonDemo frame = new JRadioButtonDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JRadioButtonDemo() {
setTitle(“单选按钮使用”);// 设置窗体的标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体退出时操作
setBounds(100, 100, 250, 100);// 设置窗体位置和大小
contentPane = new JPanel();// 创建内容面板
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 设置面板的边框
setContentPane(contentPane);// 应用内容面板
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));// 设置内容面板为流式布局
JRadioButton radioButton1 = new JRadioButton(“Java”);// 创建单选按钮
contentPane.add(radioButton1);// 应用单选按钮
JRadioButton radioButton2 = new JRadioButton(“PHP”);// 创建单选按钮
contentPane.add(radioButton2);// 应用单选按钮
JRadioButton radioButton3 = new JRadioButton(“C++”);// 创建单选按钮
contentPane.add(radioButton3);// 应用单选按钮
ButtonGroup group = new ButtonGroup();// 创建单选按钮组
group.add(radioButton1);// 将radioButton1增加到单选按钮组中
group.add(radioButton2);// 将radioButton2增加到单选按钮组中
group.add(radioButton3);// 将radioButton3增加到单选按钮组中
}
}