鱼C论坛

 找回密码
 立即注册
查看: 3241|回复: 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行

  1. package com.java15.test1;

  2. import java.awt.Container;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;

  9. import javax.swing.ButtonGroup;
  10. import javax.swing.JButton;
  11. import javax.swing.JCheckBox;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JRadioButton;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextArea;
  20. import javax.swing.JTextField;

  21. public class Test01 extends JFrame  implements ActionListener{                //事件监听接口
  22.         private JPanel bodypanel = null;
  23.         private JLabel titleLabel = null;
  24.         //用户名
  25.         private JLabel nameLabel = null;
  26.         private JTextField  nameField = null;
  27.         //密码
  28.         private JLabel passwordLabel = null;
  29.         private JPasswordField passwordField = null;
  30.         private JLabel password2Label = null;
  31.         private JPasswordField password2Field = null;
  32.         //性别
  33.         private JPanel sexPanel = null;
  34.         private JLabel sexLabel = null;
  35.         private JRadioButton boyRadioButton = null;
  36.         private JRadioButton girlRadioButton = null;
  37.         //爱好
  38.         private JLabel likeLabel = null;
  39.         private JPanel likePanel = null;
  40.         private JCheckBox readCheckBox = null;
  41.         private JCheckBox sportCheckBox = null;
  42.         private JCheckBox sleepCheckBox = null;
  43.         //籍贯
  44.         private JLabel cityLabel = null;
  45.         private JComboBox cityComboBox = null;
  46.         //描述
  47.         private JLabel discLabel = null;
  48.         private JTextArea discTextArea = null;
  49.         private JScrollPane discScrollPane = null;
  50.        
  51.         private JPanel buttonPanel = null;
  52.         private JButton submitButton = null;
  53.         private JButton resetButton = null;
  54.        
  55.         private void init() {
  56.                 Container content = this.getContentPane();
  57.                 GridBagConstraints gbc = new GridBagConstraints();
  58.                 this.bodypanel = new JPanel( new GridBagLayout());
  59.                 content.add(this.bodypanel);
  60.                 this.setTitle("用户注册");
  61.                 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  62.                
  63.                 this.titleLabel = new JLabel("用户注册");
  64.                 gbc.gridx = 1;
  65.                 gbc.gridy = 0;
  66.                 this.bodypanel.add(this.titleLabel , gbc);
  67. //用户名行               
  68.                 gbc.anchor = GridBagConstraints.EAST;
  69.                 this.nameLabel = new JLabel("用户名:");
  70.                 gbc.gridx = 0;
  71.                 gbc.gridy = 1;
  72.                 this.bodypanel.add(this.nameLabel , gbc);
  73.                
  74.                 this.nameField = new JTextField(16);
  75.                 gbc.gridx = 1;
  76.                 gbc.gridy = 1;
  77.                 this.bodypanel.add(this.nameField , gbc);
  78. //密码行               
  79.                 this.passwordLabel = new JLabel("密    码:");
  80.                 gbc.gridx = 0;
  81.                 gbc.gridy = 2;
  82.                 this.bodypanel.add(this.passwordLabel , gbc);
  83.                
  84.                 this.passwordField = new JPasswordField(16);
  85.                 gbc.gridx = 1;
  86.                 gbc.gridy = 2;
  87.                 this.bodypanel.add(this.passwordField , gbc);
  88. //密码2行               
  89.                 this.password2Label = new JLabel("密  码2:");
  90.                 gbc.gridx = 0;
  91.                 gbc.gridy = 3;
  92.                 this.bodypanel.add(this.password2Label , gbc);
  93.                
  94.                 this.password2Field = new JPasswordField(16);
  95.                 gbc.gridx = 1;
  96.                 gbc.gridy = 3;
  97.                 this.bodypanel.add(this.password2Field , gbc);
  98. //性别行               
  99.                 this.sexLabel = new JLabel("性    别:");
  100.                 gbc.gridx = 0;
  101.                 gbc.gridy = 4;
  102.                 this.bodypanel.add(this.sexLabel , gbc);
  103.                
  104.                 this.sexPanel = new JPanel( new FlowLayout(FlowLayout.CENTER, 35, 0));
  105.                 this.boyRadioButton =  new JRadioButton("男", true);
  106.                 this.girlRadioButton = new JRadioButton("女");
  107.                 ButtonGroup sexGroup =  new ButtonGroup();
  108.                 sexGroup.add(this.boyRadioButton);
  109.                 sexGroup.add(this.girlRadioButton);
  110.                 this.sexPanel.add(this.boyRadioButton);
  111.                 this.sexPanel.add(this.girlRadioButton);
  112.                 gbc.gridx = 1;
  113.                 gbc.gridy = 4;
  114.                 this.bodypanel.add(this.sexPanel , gbc);
  115. //爱好行               
  116.                 this.likeLabel = new JLabel("爱    好:");
  117.                 gbc.gridx = 0;
  118.                 gbc.gridy = 5;
  119.                 this.bodypanel.add(this.likeLabel , gbc);
  120.                
  121.                 this.likePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 6, 0));
  122.                 this.readCheckBox = new JCheckBox("阅读");
  123.                 this.sportCheckBox = new JCheckBox("运动");
  124.                 this.sleepCheckBox = new JCheckBox("睡觉");
  125.                 this.likePanel.add(this.readCheckBox);
  126.                 this.likePanel.add(this.sportCheckBox);
  127.                 this.likePanel.add(this.sleepCheckBox);
  128.                 gbc.gridx = 1;
  129.                 gbc.gridy = 5;
  130.                 this.bodypanel.add(this.likePanel , gbc);
  131. //籍贯行               
  132.                 this.cityLabel = new JLabel("籍    贯:");
  133.                 gbc.gridx = 0;
  134.                 gbc.gridy = 6;
  135.                 this.bodypanel.add(this.cityLabel , gbc);
  136.                
  137.                 String[] citys = {"西安" , "北京" , "广州" , "上海"};
  138.                 this.cityComboBox = new JComboBox(citys);
  139.                 this.cityComboBox.setPreferredSize(new Dimension(178, 30));
  140.                 gbc.gridx = 1;
  141.                 gbc.gridy = 6;
  142.                 this.bodypanel.add(this.cityComboBox , gbc);
  143. //简述行               
  144.                 this.discLabel = new JLabel("简    述:");
  145.                 gbc.gridx = 0;
  146.                 gbc.gridy = 7;
  147.                 this.bodypanel.add(this.discLabel , gbc);
  148.                
  149.                 this.discTextArea  = new JTextArea("请输入个人简介!", 10, 16);
  150.                 this.discScrollPane = new JScrollPane(this.discTextArea);
  151.                 gbc.gridx = 1;
  152.                 gbc.gridy = 7;
  153.                 this.bodypanel.add(this.discScrollPane , gbc);
  154. //提交、重置按钮行       
  155.                 this.buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
  156. //      事件监听相关按钮开始
  157.                 this.submitButton = new JButton("提交");
  158.                 this.submitButton.addActionListener(this);
  159.                 this.resetButton = new JButton("重置");
  160.                 this.resetButton.addActionListener(this);
  161. //     事件监听相关按钮结束
  162.                 this.buttonPanel.add(this.submitButton);
  163.                 this.buttonPanel.add(this.resetButton);
  164.                 gbc.gridx = 1;
  165.                 gbc.gridy = 8;
  166.                 this.bodypanel.add(this.buttonPanel , gbc);
  167.                
  168.         }
  169.        
  170.         public Test01() {
  171.                 this.init();
  172.         }
  173.        
  174.         public static void main(String[] args) {
  175.                 Test01 test01 = new Test01();
  176.                 test01.setBounds(150, 50, 400, 650);
  177.                 test01.setVisible(true);
  178.                 test01.init();

  179.         }       
  180. //出错区域开始
  181.         public void actionPerformed(ActionEvent e) {
  182.                 if(e.getSource() == this.submitButton) {
  183.                         System.out.println("提交");
  184.                 } else if(e.getSource() == this.resetButton) {
  185.                         System.out.println("重置");
  186.                 }
  187.                
  188.         }
  189. //出错区域结束
  190. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> 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-4-18 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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