|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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("重置");
- }
-
- }
- //出错区域结束
- }
复制代码 |
|