
java图形界面事件监听的一些小问题
我需要在一个界面里面用两个按钮,我要如何做到,一个界面的多个按钮的事件监听?这是我的代码publicclasssearchextendsJFrameimplementsA...
我需要在一个界面里面用两个按钮,我要如何做到,一个界面的多个按钮的事件监听?这是我的代码public class search extends JFrame implements ActionListener{ JPanel jp1,jp2,jp3;//面板 JLabel jl1;//标签 JButton jb1,jb2;//按钮 JRadioButton jrb1,jrb2=null;//按钮功能选项 ButtonGroup bg1=null; public search() { // TODO Auto-generated constructor stub jp1=new JPanel(); jp2=new JPanel(); jp3=new JPanel(); bg1=new ButtonGroup(); jb1=new JButton(" 确定 "); jb2=new JButton(" 返回 "); jl1=new JLabel("查询功能"); jl1.setFont(new Font("宋体",Font.PLAIN,24)); jrb1=new JRadioButton(" 查看余额 "); jrb2=new JRadioButton(" 基本信息 "); //字体 jb1.setFont(new Font("宋体",Font.PLAIN,16)); jb2.setFont(new Font("宋体",Font.PLAIN,16)); jrb1.setFont(new Font("宋体",Font.PLAIN,16)); jrb2.setFont(new Font("宋体",Font.PLAIN,16)); //颜色 jp1.setBackground(Color.YELLOW); jp2.setBackground(Color.YELLOW); jp3.setBackground(Color.YELLOW); jrb1.setBackground(Color.YELLOW); jrb2.setBackground(Color.YELLOW); //查看近期存取款记录,可以的话,在根据时间筛选读出 //添加组件 bg1.add(jrb1); bg1.add(jrb2); jp1.add(jl1); jp2.add(jrb1); jp2.add(jrb2); jp3.add(jb1); jp3.add(jb2); //添加面板 this.add(jp1); this.add(jp2); this.add(jp3); //布局 this.setLayout(new GridLayout(3,1)); //给窗口设置标题 this.setTitle("查询功能"); //设置窗体大小 this.setSize(400, 350); //设置当关闭窗口退出 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //显示窗体 this.setVisible(true); this.setResizable(true); setLocationRelativeTo(null);//在屏幕中间显示 //监听事件 jrb1.addActionListener(this); jrb2.addActionListener(this); jb1.addActionListener(this); jb2.addActionListener(this); } @Override//这里的事件监听是有问题的 public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand()==" 查看余额 ") { if(e.getActionCommand()==" 确定 ") { System.out.println("进行查找功能!!!"); } if(e.getActionCommand()==" 退出 ") { System.out.println("退出啦!!!"); } }else if(e.getActionCommand()==" 基本信息 "){ if(e.getActionCommand()==" 确定 ") { System.out.println("进行读取信息功能!!!"); } if(e.getActionCommand()==" 退出 ") { System.out.println("退出啦!!!"); } } /* * else if(e.getActionCommand()==" 确定 "){ System.out.println("先按功能咯!!!"); }else { System.out.println("直接退出呀!!!"); }*/ }}然后配图是我实现的样子,我希望能监听到功能选项和下面的确定和退出,求大佬!!!
展开
1个回答
展开全部
错误一: jb2=new JButton(" 返回 "); if(e.getActionCommand()==" 退出 ")
这两行代码中的字符串, 内容不一致 . 为了避免这种情况,我们可以定义一个常量字符串,进行使用
错误二: 事件的触发是点击两个按钮, 跟jrb1和jrb2没有关系的,所以只需要给jb1和jb2添加事件响应
修改后的效果图
完整的参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class search extends JFrame implements ActionListener {
JPanel jp1, jp2, jp3;
JLabel jl1;
JButton jb1, jb2;
JRadioButton jrb1, jrb2 = null;
ButtonGroup bg1 = null;
// 为了避免前后使用的字符串不一致 , 定义两个字符串常量
static final String OK_CMD=" 确定 ";
static final String EXIT_CMD=" 返回 ";
public search() {
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
bg1 = new ButtonGroup();
jb1 = new JButton(OK_CMD);
jb2 = new JButton(EXIT_CMD);
jl1 = new JLabel("查询功能");
jl1.setFont(new Font("宋体", Font.PLAIN, 24));
jrb1 = new JRadioButton(" 查看余额 ");
jrb2 = new JRadioButton(" 基本信息 ");
jb1.setFont(new Font("宋体", Font.PLAIN, 16));
jb2.setFont(new Font("宋体", Font.PLAIN, 16));
jrb1.setFont(new Font("宋体", Font.PLAIN, 16));
jrb2.setFont(new Font("宋体", Font.PLAIN, 16));
jp1.setBackground(Color.YELLOW);
jp2.setBackground(Color.YELLOW);
jp3.setBackground(Color.YELLOW);
jrb1.setBackground(Color.YELLOW);
jrb2.setBackground(Color.YELLOW);
bg1.add(jrb1);
bg1.add(jrb2);
jp1.add(jl1);
jp2.add(jrb1);
jp2.add(jrb2);
jp3.add(jb1);
jp3.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.setLayout(new GridLayout(3, 1));
this.setTitle("查询功能");
this.setSize(400, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
setLocationRelativeTo(null);
// jrb1.setSelected(true); //一般会有一个单选按钮处于选中状态
this.setVisible(true); // 一般布局完成,最后才调用显示方法
jb1.addActionListener(this);
jb2.addActionListener(this);
}
@Override // 这里的事件监听是有问题的
public void actionPerformed(ActionEvent e) { //
String cmd = e.getActionCommand();
if (OK_CMD.equals(cmd)) { // 字符串内容是否相等 ,强烈推荐使用equals方法, 不要使用 ==!!!
// 点击了确定按钮,接下来判断 单选按钮的选中情况
if (jrb1.isSelected()) {
System.out.println("进行查找功能!!!");
} else if (jrb2.isSelected()) {
System.out.println("进行读取信息功能!!!");
} else {// 如果jrb1和jrb2都没有选择
System.out.println("先按功能咯!!!");
}
} else if (EXIT_CMD.equals(cmd)) {
System.out.println("退出啦!!!");
}
}
//添加一个main方法用于测试
public static void main(String[] args) {
new search();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询