javaswt中单选按钮

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();

}

}

javaswt中单选按钮

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增加到单选按钮组中

}

}

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月24日 03:40:12
下一篇 2024年3月24日 03:46:44

相关推荐

  • excel设置多项选择按钮,excel设置多项选择按钮快捷键

    如何在excel表中做选择按钮? 1、打开需要添加选项的excel表格。点击需要添加选项的单元格。然后选择“数据”菜单,点击“有效性”再选择“数据有效性”。在“数据有效性”对话框中,点击“任何值”旁边的小三角,选择“序列”。 2、打开你的excel表格,点击要设置选择项的单元格后点击上方的数据。找到里面的数据工具的功能组后,点击数据验证的按钮。再在允许的菜单…

    2024年5月17日
    3900
  • javaswt插件,java office插件

    Java桌面应用程序设计:SWT简介 AWT都是重量级组件,通过调用平台对等体实现GUI,所以有个最大公约数问题。界面也很难看。Swing绝大部分是轻量级组件,用Java自己绘制GUI,可以方便的使用各种L&F,但是比较消耗资源。 Java不仅可以用来开发大型的桌面应用程序,而且还特别适合于Internet的应用开发。目前,很多新的技术领域都涉及Ja…

    2024年5月17日
    4300
  • excel选项按钮分组,excel分类按钮

    excel如果不用分组框还有什么方法把选项按钮分组? 1、首先选中类别中明细行,之后点击数据选项卡,找到创建组,点击即可。然后,按照上述方法对所有类别进行分组显示设置,就会出现下述图中所示样式。2按钮是分级显示按钮,点击2可以实现类别中明细的隐藏与显示。 2、如果想取消分组,只需要将相应的数据选中,然后在”数据“选项卡中单击”取消分组“即可。 3、首先我们打…

    2024年5月16日
    32900
  • javaswt声音,java声音处理库

    用java做一个可视化小程序,可以录音并予以保存。 不需要外部包,只是用系统JDK中的 awt包下的 toolkit工具类就可以了。 JBuilder是Borland公司开发的针对java的开发工具,使用JBuilder将可以快速,有效的开发各类java套用,它使用的JDK与sun公司标准的JDK不同,它经过了较多的修改,以便开发人员能够像开发Delphi套…

    2024年5月16日
    3600
  • javaswt主题,java swt界面

    SWT是什么?与swing/awt相比有什么优缺点 SWT是一个开源的GUI编程框架。SWT(Standard Widget Toolkit) 是一个开源的GUI编程框架,与AWT/Swing有相似的用处,著名的开源IDE-eclipse就是用SWT开发的。在SWT之前,Sun已经提供了一个跨平台GUI开发工具包AWT。 另一种释义:SWT(Stationa…

    2024年5月16日
    3600
  • excel小控件如何删除吗,excel如何删除按钮控件

    EXCEL的控件如何删除(如图) 如下图,要删除Excel中的一个复选框,这时点击“开发工具”。进入开发工具选项后,点击开启“设计模式”。如下图,开启设计模式后,鼠标点击一下即可选中该复选框。 确保“开发工具”选项卡可用。确保处于设计模式。在“开发工具”选项卡上的“控件”组中,打开“设计模式”。选择要删除的一个或多个控件。按 Delete。 单击 视图→工具…

    2024年5月16日
    4200
  • excel设置成a4,Excel设置成按钮

    如何设置excel表格为a4大小? 1、.在桌面中找到要修改的表格,点击该表格进入编辑页面,如下图所示。在编辑页面中找到页面布局选项,点击该选项进入页面布局页面,如下图所示。在页面布局中找到纸张大小选项,点击该选项进入纸张大小页面,如下图所示。 2、我们需要准备的材料分别是:电脑、Excel表格。首先我们打开需要编辑的Excel表格。然后我们点击打开页面布局…

    2024年5月15日
    4700
  • excel按钮效果,Excel添加选项按钮

    wps软件EXCEL表格怎么创建按钮,并点击一次对应单元格内数字加1?_百度… 插入一个表单(数值微调按钮)控件,如图:在工作表中拖动鼠标,划出一个按钮,然后按住alt键,调整按键的大小和位置,使用得右边的上下两个触点,落在单元格的右边框的角点上。效果如图:设置按钮。 在该工作表的任意空白位置,找一个空单元格,输入数字“1”。复制该单元格,两种方…

    2024年5月15日
    4000
  • java按钮字体颜色,java按钮颜色设置

    如何改变java按钮中的颜色? 设置Button的前景色就可以.代码如下:b.setForeground(new java.awt.Color(255, 0, 0));或者:b.setForeground(java.awt.Color.red);即可。 当然可以,Button透明背景设置方法:方法1:JButton b1=new JButton();bset…

    2024年5月14日
    3600
  • javascript单选按钮必选,js单选框radio选中事件

    如何用js实现将当前点击的单选按钮变为选中状态? 1、jsp中的单选按钮通常是radio,所以通过js来设置这个默认选中状态即可。 2、你要实现的是单击第一个单选框 第二个自动选中吧?如果是,请看下边。 3、如果你使用jQuery 6 ,代码if ( $(elem).attr(checked) ),将获得一个属性(attribute) ,它不改变该复选框被选…

    2024年5月14日
    4000

发表回复

登录后才能评论



关注微信