
正文
java单选框代码监听 javaweb单选框
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java怎样监听一个值是否发生了变化,具体代码
java 自定义监听器监听属性变化
import java.util.EventObject;
public class MyEvent extends EventObject
{
private Object obj;
private String sName;
public MyEvent(Object source,String sName)
{
super(source);
this.obj=source;
this.sName=sName;
}
public Object getObj()
{
return obj;
}
public String getsName()
{
return sName;
}
}
import java.util.EventListener;
public interface MyEventListener extends EventListener
{
public void handleEvent (MyEvent me);
}
import java.util.Iterator;
import java.util.Vector;
import demo.DemoEvent;
public class MyEventSource
{
private Vector list=new Vector();
private String sName = "";
public MyEventSource()
{
super();
}
public void addMyEventListener(MyEventListener me)
{
list.add(me);
}
public void deleteMyEventListener(MyEventListener me)
{
list.remove(me);
}
public void notifyMyEvent(MyEvent me)
{
Iterator it=list.iterator();
while(it.hasNext())
{
((MyEventListener) it.next()).handleEvent(me);
}
}
public void setName(String str)
{
boolean bool = false;
if (str == null sName != null)
bool = true;
else if (str != null sName == null)
bool = true;
else if (!sName.equals(str))
bool = true;
this.sName = str;
// 如果改变则执行事件
if (bool)
notifyMyEvent(new MyEvent(this, sName));
}
public String getsName()
{
return sName;
}
}
public class Test implements MyEventListener
{
public Test()
{
MyEventSource mes = new MyEventSource();
mes.addMyEventListener(this);
mes.setName("niu");
}
public static void main(String args[])
{
new Test();
}
public void handleEvent(MyEvent me)
{
System.out.println(me.getSource());
System.out.println(me.getsName());
}
}
相关问答
Q1: Java按钮监听
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.Color;
import java.awt.*;
public class PanelTest{
public static void main(String args[]) {
/*Scanner sc = new Scanner(System.in); double pi=3.14,s; double r; r=sc.nextDouble(); s=pi*r*r; System.out.println("s等于"+s);*/
EventQueue.invokeLater(new Runnable() {
public void run() {
CricleFrame frame = new CricleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class CricleFrame extends JFrame{
Panel p = new Panel();
TextField t = new TextField();
Button b = new Button("确定");
Label a = new Label("请在此输入半径");
TextField result = new TextField();
public CricleFrame(){
add(a);
add(t);
add(b);
add(result);
add(p);
setVisible(true);
p.setBackground(Color.black);
a.setBackground(Color.yellow);
t.setBackground(Color.white);
result.setBackground(Color.white);
b.setBackground(Color.cyan);
setSize(300, 300);
setTitle("圆的面积");
a.setBounds(105, 45, 90, 25);
t.setBounds(100, 80, 100, 25);
result.setBounds(100, 180, 100, 25);
b.setBounds(111, 120, 80, 40);
b.addActionListener(new ActionListener() { //按钮点击事件监听
public void actionPerformed(ActionEvent event) {
Double r=0.0;
try{
r = Double.parseDouble(t.getText());
}catch (Exception e){
System.out.println(e.getMessage());
}
BigDecimal tmp = new BigDecimal(r * r * Math.PI);
Double area = tmp.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); //保留2位小数
result.setText(""+area);
}
});
}
}
在你的基础上改了一下,界面什么的没有改
Q2: java中为什么要设置监听器,有什么用?
猪哥解答:
1、private JButton jb=new JButton("按钮");这句话声明了一个按钮,名字叫jb。
2、jb.addActionListener(this);这里给jb那个按钮设置了监听,默认为点击触发,当然你写的这个监听有点怪异~
3、点击按钮jb触发监听处理方法actionPerformed,在这里可以做你想要的操作,你代码实现的是改变lab这个label标签的内容。
4、至于java中为什么要用监听,这就像银行装监控一样,监视你的一举一动,银行装监控是为了捕捉每个进银行的人的动作,预防危险的发生。
java中做监听同样是为了监视某个客户端动作用的,万一你给我搞破坏怎么办(监听的作用远不止如此),当然也像平时生活中不是所有的地方都要放监控,要不就没法过了,java中也不是所有的地方都要放监听,具体哪里要放监听,不该是在课本里学的,应该根据实际工厂、公司的需求来定。
Q3: java在swing中单选框的监听是哪个
ItemListener
事件监听接口一般都在java.awt.event.*这个包中定义,你要监听单选框其实也可以用其他的监听,因为实现监听的主要是接口中的方法,而方法是需要你去定义的。
Q4: JAVA中有关JRadioButton绑定监听事件,代码如下
1、this代表本类,就是this所在java单选框代码监听的类。
2、
addItemStateChanged是为java单选框代码监听了监听ItemEvent的,而ItemEvent是指示项被选定或取消选定的语义事件,此高级事件是在用户已选定项或取消选定项时由 ItemSelectable 对象(如 List)生成的。
addActionListener是为了监听ActionEvent的,而ActionEvent是指示发生了组件定义的动作的语义事件,当特定于组件的动作(比如被按下)发生时,由组件(比如 Button)生成此高级别事件。
Q5: JAVA中下拉菜单中的菜单项怎样做监听
****针对楼主的补充说明,我已经作了相应的修改了****
关键的代码是如这样子的:
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int nIndex=jComboBox.getSelectedIndex();
////然后针对不同的nIndex值(即不同的被选项)而写入不同的代码。
}
});
我这里帮你编写了一个非常简单的案例,你可以运行看看。
代码如下:
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JComboBox;
import java.awt.Rectangle;
import javax.swing.JLabel;
public class JianTing extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JComboBox jComboBox = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
/**
* This is the default constructor
*/
public JianTing() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(314, 204);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.setVisible(true);
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(51, 89, 65, 18));
jLabel1.setText("选项内容:");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(51, 110, 186, 36));
jLabel.setText("");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
}
return jContentPane;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
/////这里是重点代码!!!!
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(62, 25, 170, 27));
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
int nIndex=jComboBox.getSelectedIndex();
if(nIndex==0){
jLabel.setText(("选项A"));
}
else if(nIndex==1){
jLabel.setText(("选项B"));
}
else if(nIndex==2){
jLabel.setText(("选项C"));
}
}
});
String[] myList={"选项A","选项B","选项C"};
jComboBox.addItem(myList[0]);
jComboBox.addItem(myList[1]);
jComboBox.addItem(myList[2]);
}
return jComboBox;
}
public static void main(String args[]){
new JianTing();
}
} // @jve:decl-index=0:visual-constraint="10,10"
java单选框代码监听的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于javaweb单选框、java单选框代码监听的信息别忘了在本站进行查找喔。






