鱼C论坛

 找回密码
 立即注册
查看: 3417|回复: 1

java事件监听方面问题,求帮忙!

[复制链接]
发表于 2017-6-5 11:10:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
        程序在事件监听处出了问题,业务为用户注册界面,具体业务是在运行程序后,
        (1)点击提交按钮,控制台会出现提交的字样。
        (2)点重置按钮,控制台会出现重置的字样。
       
        但实际并没有出现此字样,在我经过多次检验之后最终将问题定位到第188行代码的if语句中。(e.getSource() == this.submitButton)这句代码显示的是false。我将这句代码放入syste.out.printin()中打印出来的还是false。
        我将问题出现位置和相关代码位置都做了注释,方便大神寻找。蟹蟹。
        问题区域187行~194行
        监听事件进行的相关按钮159~164行

package com.java15.test1;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Test01 extends JFrame  implements ActionListener{                //事件监听接口
        private JPanel bodypanel = null;
        private JLabel titleLabel = null;
        //用户名
        private JLabel nameLabel = null;
        private JTextField  nameField = null;
        //密码
        private JLabel passwordLabel = null;
        private JPasswordField passwordField = null;
        private JLabel password2Label = null;
        private JPasswordField password2Field = null;
        //性别
        private JPanel sexPanel = null;
        private JLabel sexLabel = null;
        private JRadioButton boyRadioButton = null;
        private JRadioButton girlRadioButton = null;
        //爱好
        private JLabel likeLabel = null;
        private JPanel likePanel = null;
        private JCheckBox readCheckBox = null;
        private JCheckBox sportCheckBox = null;
        private JCheckBox sleepCheckBox = null;
        //籍贯
        private JLabel cityLabel = null;
        private JComboBox cityComboBox = null;
        //描述
        private JLabel discLabel = null;
        private JTextArea discTextArea = null;
        private JScrollPane discScrollPane = null;
        
        private JPanel buttonPanel = null;
        private JButton submitButton = null;
        private JButton resetButton = null;
        
        private void init() {
                Container content = this.getContentPane();
                GridBagConstraints gbc = new GridBagConstraints();
                this.bodypanel = new JPanel( new GridBagLayout());
                content.add(this.bodypanel);
                this.setTitle("用户注册");
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                
                this.titleLabel = new JLabel("用户注册");
                gbc.gridx = 1;
                gbc.gridy = 0;
                this.bodypanel.add(this.titleLabel , gbc);
//用户名行                
                gbc.anchor = GridBagConstraints.EAST;
                this.nameLabel = new JLabel("用户名:");
                gbc.gridx = 0;
                gbc.gridy = 1;
                this.bodypanel.add(this.nameLabel , gbc);
                
                this.nameField = new JTextField(16);
                gbc.gridx = 1;
                gbc.gridy = 1;
                this.bodypanel.add(this.nameField , gbc);
//密码行                
                this.passwordLabel = new JLabel("密    码:");
                gbc.gridx = 0;
                gbc.gridy = 2;
                this.bodypanel.add(this.passwordLabel , gbc);
                
                this.passwordField = new JPasswordField(16);
                gbc.gridx = 1;
                gbc.gridy = 2;
                this.bodypanel.add(this.passwordField , gbc);
//密码2行                
                this.password2Label = new JLabel("密  码2:");
                gbc.gridx = 0;
                gbc.gridy = 3;
                this.bodypanel.add(this.password2Label , gbc);
                
                this.password2Field = new JPasswordField(16);
                gbc.gridx = 1;
                gbc.gridy = 3;
                this.bodypanel.add(this.password2Field , gbc);
//性别行                
                this.sexLabel = new JLabel("性    别:");
                gbc.gridx = 0;
                gbc.gridy = 4;
                this.bodypanel.add(this.sexLabel , gbc);
                
                this.sexPanel = new JPanel( new FlowLayout(FlowLayout.CENTER, 35, 0));
                this.boyRadioButton =  new JRadioButton("男", true); 
                this.girlRadioButton = new JRadioButton("女");
                ButtonGroup sexGroup =  new ButtonGroup();
                sexGroup.add(this.boyRadioButton);
                sexGroup.add(this.girlRadioButton);
                this.sexPanel.add(this.boyRadioButton);
                this.sexPanel.add(this.girlRadioButton);
                gbc.gridx = 1;
                gbc.gridy = 4;
                this.bodypanel.add(this.sexPanel , gbc);
//爱好行                
                this.likeLabel = new JLabel("爱    好:");
                gbc.gridx = 0;
                gbc.gridy = 5;
                this.bodypanel.add(this.likeLabel , gbc);
                
                this.likePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 6, 0));
                this.readCheckBox = new JCheckBox("阅读");
                this.sportCheckBox = new JCheckBox("运动");
                this.sleepCheckBox = new JCheckBox("睡觉");
                this.likePanel.add(this.readCheckBox);
                this.likePanel.add(this.sportCheckBox);
                this.likePanel.add(this.sleepCheckBox);
                gbc.gridx = 1;
                gbc.gridy = 5;
                this.bodypanel.add(this.likePanel , gbc);
//籍贯行                
                this.cityLabel = new JLabel("籍    贯:");
                gbc.gridx = 0;
                gbc.gridy = 6;
                this.bodypanel.add(this.cityLabel , gbc);
                
                String[] citys = {"西安" , "北京" , "广州" , "上海"};
                this.cityComboBox = new JComboBox(citys);
                this.cityComboBox.setPreferredSize(new Dimension(178, 30));
                gbc.gridx = 1;
                gbc.gridy = 6;
                this.bodypanel.add(this.cityComboBox , gbc);
//简述行                
                this.discLabel = new JLabel("简    述:");
                gbc.gridx = 0;
                gbc.gridy = 7;
                this.bodypanel.add(this.discLabel , gbc);
                
                this.discTextArea  = new JTextArea("请输入个人简介!", 10, 16);
                this.discScrollPane = new JScrollPane(this.discTextArea);
                gbc.gridx = 1;
                gbc.gridy = 7;
                this.bodypanel.add(this.discScrollPane , gbc);
//提交、重置按钮行        
                this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
//      事件监听相关按钮开始
                this.submitButton = new JButton("提交");
                this.submitButton.addActionListener(this);
                this.resetButton = new JButton("重置");
                this.resetButton.addActionListener(this);
//     事件监听相关按钮结束
                this.buttonPanel.add(this.submitButton);
                this.buttonPanel.add(this.resetButton);
                gbc.gridx = 1;
                gbc.gridy = 8;
                this.bodypanel.add(this.buttonPanel , gbc);
                
        }
        
        public Test01() {
                this.init();
        }
        
        public static void main(String[] args) {
                Test01 test01 = new Test01();
                test01.setBounds(150, 50, 400, 650);
                test01.setVisible(true);
                test01.init();

        }        
//出错区域开始
        public void actionPerformed(ActionEvent e) {
                if(e.getSource() == this.submitButton) {
                        System.out.println("提交");
                } else if(e.getSource() == this.resetButton) {
                        System.out.println("重置");
                }
                
        }
//出错区域结束
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-6-5 11:36:26 | 显示全部楼层
本帖最后由 零度非安全 于 2017-6-5 11:38 编辑

请将第 181 行的 test01.init() 这段代码删除

因为你第 178 行代码处 new 的时候已经调用了上面定义的构造器里的方法,第 181 行去掉就可以在控制台显示提交和重置了

0.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 09:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表