简体   繁体   English

单选按钮动作监听器中的问题

[英]Issue in Radio Button Action Listener

I have written some code when one of the option in radio button is clicked it should display one jlabel and jtext field. 单击单选按钮中的一个选项时,我已经写了一些代码,它应该显示一个jlabel和jtext字段。 And when other option in the radio button is clicked it should hide the previous shown jlabel and jtext field and display new jlabel and jtext field. 当单击单选按钮中的其他选项时,它应该隐藏先前显示的jlabel和jtext字段并显示新的jlabel和jtext字段。

In the output when I click on one of the radio button it is displaying nothing unless and until I maximize my Window. 在输出中,当我单击一个单选按钮时,除非并且直到我最大化Window,否则它什么也不显示。 After geting my jlabel and jtextfield If I click on other radio button the jlabel and jtextfield is hidden but Im not able to see new jlabel and jtextfield for that radiobutton. 得到我的jlabel和jtextfield之后,如果我单击其他单选按钮,则jlabel和jtextfield被隐藏,但是我看不到该单选按钮的新的jlabel和jtextfield。

enter code here


public class Emp4 {

    private JFrame frame;
 private JTextField jtxtName;
 private JTextField jtxtAge;
 private JTextField jtxtSal;
  private JTextField jtxtHour_Pay;
    private JTextField jtxtHour_Worked;


     public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Emp4 window = new Emp4();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
                }
        }
        });
    }

    public Emp4() {
    initialize();
    }


    private void initialize() {
    frame = new JFrame();
    frame.setBounds(0, 0, 1000, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
    panel.setBounds(30, 11, 414, 36);
    frame.getContentPane().add(panel);
    panel.setLayout(null);

    JLabel lblEmployeeDatabase = new JLabel("Employee Database");
    lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
    lblEmployeeDatabase.setBounds(157, 7, 193, 25);
    panel.add(lblEmployeeDatabase);

    JPanel panel_1 = new JPanel();
    panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
    panel_1.setBounds(10, 61, 464, 230);
    frame.getContentPane().add(panel_1);
    panel_1.setLayout(null);

    JLabel jlblEmpName = new JLabel("Employee Name");
    jlblEmpName.setBounds(10, 11, 110, 14);
    panel_1.add(jlblEmpName);

    jtxtName = new JTextField();
    jtxtName.setBounds(114, 8, 120, 20);
    panel_1.add(jtxtName);
    jtxtName.setColumns(10);

    JLabel jlblEmpAge = new JLabel("Employee Age");
    jlblEmpAge.setBounds(10, 52, 110, 14);
    panel_1.add(jlblEmpAge);

    jtxtAge = new JTextField();
    jtxtAge.setColumns(10);
    jtxtAge.setBounds(114, 49, 120, 20);
    panel_1.add(jtxtAge);

    JLabel jlblEmpType = new JLabel("Employee Type");
    jlblEmpType.setBounds(10, 95, 110, 14);
    panel_1.add(jlblEmpType);

    JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
    JRadioButton jrdbtnContract = new JRadioButton("Contract ");
    JLabel jlblEmpHour = new JLabel("Hourly Rate");
    jlblEmpHour.setBounds(5, 121, 66, 14);
    ButtonGroup group =new ButtonGroup();
    JLabel jlblEmpSal = new JLabel("Salary");
    jlblEmpSal.setBounds(114, 121, 66, 14);
    JLabel jlblEmpWork = new JLabel("Hours Worked");
    jlblEmpWork.setBounds(150, 120, 86, 24);

    jtxtSal = new JTextField();
    jtxtSal.setColumns(10);
    jtxtSal.setBounds(164, 121, 109, 23);

    jtxtHour_Pay = new JTextField();
    jtxtHour_Pay.setColumns(10);
    jtxtHour_Pay.setBounds(75, 121, 59, 23);

    jtxtHour_Worked = new JTextField();
    jtxtHour_Worked.setColumns(10);
    jtxtHour_Worked.setBounds(243, 121, 109, 23);

    group.add(jrdbuttonFullTime);
    group.add(jrdbtnContract);

    jrdbuttonFullTime.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(jrdbuttonFullTime.isSelected()){
                //jrdbtnContract.setSelected(false);

                panel_1.add(jlblEmpSal);
                panel_1.add(jtxtSal);
                jlblEmpHour.setVisible(false);
                jtxtHour_Pay.setVisible(false);
                jtxtHour_Worked.setVisible(false);
                jlblEmpWork.setVisible(false);
            }

        }
    });


    jrdbuttonFullTime.setBounds(113, 91, 109, 23);
    panel_1.add(jrdbuttonFullTime);



    jrdbtnContract.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(jrdbtnContract.isSelected()){
                //jrdbuttonFullTime.setSelected(false);

                panel_1.add(jlblEmpHour);
                panel_1.add(jtxtHour_Pay);
                panel_1.add(jlblEmpWork);
                panel_1.add(jtxtHour_Worked);
                jlblEmpSal.setVisible(false);
                jtxtSal.setVisible(false);
            }


        }
    });
    jrdbtnContract.setBounds(218, 91, 109, 23);
    panel_1.add(jrdbtnContract);






      }
    }

Insteed of adding and removing components, simply add all and hide/show them on radiobox selection like this: 无需添加和删除组件,只需添加所有组件并在单选框选择中隐藏/显示它们,如下所示:

panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);

panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);

ActionListener myAction = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        jlblEmpHour.setVisible(jrdbtnContract.isSelected());
        jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
        jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
        jlblEmpWork.setVisible(jrdbtnContract.isSelected());

        jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
        jtxtSal.setVisible(jrdbuttonFullTime.isSelected());

    }

};
myAction.actionPerformed(null); // to initialize labels first
    jrdbuttonFullTime.addActionListener(myAction); // add actionlisteners 
    jrdbtnContract.addActionListener(myAction);// add actionlisteners 

As you can see, you dont even need 2 separate action listeners as one but shared instance is just enough. 如您所见,您甚至不需要两个单独的动作侦听器,但是共享实例就足够了。 So the complete app will look like this: 因此,完整的应用程序将如下所示:

public class Emp4 {

    private JFrame frame;
    private JTextField jtxtName;
    private JTextField jtxtAge;
    private JTextField jtxtSal;
    private JTextField jtxtHour_Pay;
    private JTextField jtxtHour_Worked;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Emp4 window = new Emp4();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Emp4() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panel.setBounds(30, 11, 414, 36);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblEmployeeDatabase = new JLabel("Employee Database");
        lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblEmployeeDatabase.setBounds(157, 7, 193, 25);
        panel.add(lblEmployeeDatabase);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panel_1.setBounds(10, 61, 464, 230);
        frame.getContentPane().add(panel_1);
        panel_1.setLayout(null);

        JLabel jlblEmpName = new JLabel("Employee Name");
        jlblEmpName.setBounds(10, 11, 110, 14);
        panel_1.add(jlblEmpName);

        jtxtName = new JTextField();
        jtxtName.setBounds(114, 8, 120, 20);
        panel_1.add(jtxtName);
        jtxtName.setColumns(10);

        JLabel jlblEmpAge = new JLabel("Employee Age");
        jlblEmpAge.setBounds(10, 52, 110, 14);
        panel_1.add(jlblEmpAge);

        jtxtAge = new JTextField();
        jtxtAge.setColumns(10);
        jtxtAge.setBounds(114, 49, 120, 20);
        panel_1.add(jtxtAge);

        JLabel jlblEmpType = new JLabel("Employee Type");
        jlblEmpType.setBounds(10, 95, 110, 14);
        panel_1.add(jlblEmpType);

        JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
        JRadioButton jrdbtnContract = new JRadioButton("Contract ");
        JLabel jlblEmpHour = new JLabel("Hourly Rate");
        jlblEmpHour.setBounds(5, 121, 66, 14);
        ButtonGroup group = new ButtonGroup();
        JLabel jlblEmpSal = new JLabel("Salary");
        jlblEmpSal.setBounds(114, 121, 66, 14);
        JLabel jlblEmpWork = new JLabel("Hours Worked");
        jlblEmpWork.setBounds(150, 120, 86, 24);

        jtxtSal = new JTextField();
        jtxtSal.setColumns(10);
        jtxtSal.setBounds(164, 121, 109, 23);

        jtxtHour_Pay = new JTextField();
        jtxtHour_Pay.setColumns(10);
        jtxtHour_Pay.setBounds(75, 121, 59, 23);

        jtxtHour_Worked = new JTextField();
        jtxtHour_Worked.setColumns(10);
        jtxtHour_Worked.setBounds(243, 121, 109, 23);

        group.add(jrdbuttonFullTime);
        group.add(jrdbtnContract);

        panel_1.add(jlblEmpSal);
        panel_1.add(jtxtSal);

        panel_1.add(jlblEmpHour);
        panel_1.add(jtxtHour_Pay);
        panel_1.add(jlblEmpWork);
        panel_1.add(jtxtHour_Worked);

        ActionListener myAction = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                jlblEmpHour.setVisible(jrdbtnContract.isSelected());
                jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
                jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
                jlblEmpWork.setVisible(jrdbtnContract.isSelected());

                jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
                jtxtSal.setVisible(jrdbuttonFullTime.isSelected());

            }

        };
        myAction.actionPerformed(null); // to initialize labels first
        jrdbuttonFullTime.addActionListener(myAction);
        jrdbtnContract.addActionListener(myAction);

        jrdbtnContract.setBounds(218, 91, 109, 23);
        jrdbuttonFullTime.setBounds(113, 91, 109, 23);
        panel_1.add(jrdbuttonFullTime);
        panel_1.add(jrdbtnContract);

    }
}

在此处输入图片说明 在此处输入图片说明

I've revised your code a little. 我已经稍微修改了您的代码。 This should get you going on the right path: 这应该使您走上正确的道路:

public class Emp4 {

    private JFrame frame;
    private JTextField jtxtName;
    private JTextField jtxtAge;
    private JTextField jtxtSal;
    private JTextField jtxtHour_Pay;
    private JTextField jtxtHour_Worked;

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable() {
            public void run()
            {
                try {
                    Emp4 window = new Emp4();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Emp4()
    {
        initialize();
    }

    private void initialize()
    {
        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panel.setBounds(30, 11, 414, 36);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblEmployeeDatabase = new JLabel("Employee Database");
        lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblEmployeeDatabase.setBounds(157, 7, 193, 25);
        panel.add(lblEmployeeDatabase);

        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
        panel_1.setBounds(10, 61, 464, 230);
        frame.getContentPane().add(panel_1);
        panel_1.setLayout(null);

        JLabel jlblEmpName = new JLabel("Employee Name");
        jlblEmpName.setBounds(10, 11, 110, 14);
        panel_1.add(jlblEmpName);

        jtxtName = new JTextField();
        jtxtName.setBounds(114, 8, 120, 20);
        panel_1.add(jtxtName);
        jtxtName.setColumns(10);

        JLabel jlblEmpAge = new JLabel("Employee Age");
        jlblEmpAge.setBounds(10, 52, 110, 14);
        panel_1.add(jlblEmpAge);

        jtxtAge = new JTextField();
        jtxtAge.setColumns(10);
        jtxtAge.setBounds(114, 49, 120, 20);
        panel_1.add(jtxtAge);

        JLabel jlblEmpType = new JLabel("Employee Type");
        jlblEmpType.setBounds(10, 95, 110, 14);
        panel_1.add(jlblEmpType);

        JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
        JRadioButton jrdbtnContract = new JRadioButton("Contract ");
        JLabel jlblEmpHour = new JLabel("Hourly Rate");
        jlblEmpHour.setBounds(5, 121, 66, 14);
        ButtonGroup group = new ButtonGroup();
        JLabel jlblEmpSal = new JLabel("Salary");
        jlblEmpSal.setBounds(114, 121, 66, 14);
        JLabel jlblEmpWork = new JLabel("Hours Worked");
        jlblEmpWork.setBounds(150, 120, 86, 24);

        jtxtSal = new JTextField();
        jtxtSal.setColumns(10);
        jtxtSal.setBounds(164, 121, 109, 23);

        jtxtHour_Pay = new JTextField();
        jtxtHour_Pay.setColumns(10);
        jtxtHour_Pay.setBounds(75, 121, 59, 23);

        jtxtHour_Worked = new JTextField();
        jtxtHour_Worked.setColumns(10);
        jtxtHour_Worked.setBounds(243, 121, 109, 23);

        //*******************************************************************
        // Add all your salary fields here, not in ActionListeners
        // Start them off invisible
        //*******************************************************************
        jlblEmpSal.setVisible(false);
        panel_1.add(jlblEmpSal);

        jtxtSal.setVisible(false);
        panel_1.add(jtxtSal);

        panel_1.add(jlblEmpHour);
        jlblEmpHour.setVisible(false);

        panel_1.add(jtxtHour_Pay);
        jtxtHour_Pay.setVisible(false);

        panel_1.add(jlblEmpWork);
        jlblEmpWork.setVisible(false);

        jtxtHour_Worked.setVisible(false);
        panel_1.add(jtxtHour_Worked);

        group.add(jrdbuttonFullTime);
        group.add(jrdbtnContract);

        jrdbuttonFullTime.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (jrdbuttonFullTime.isSelected()) {
                    //jrdbtnContract.setSelected(false);

                    // ****************************************************
                    // In ActionListeners for radiobuttons, hide the fields you
                    // don't want to see, make visible the ones you do want to see
                    // ****************************************************
                    jlblEmpSal.setVisible(true);
                    jtxtSal.setVisible(true);
                    jlblEmpHour.setVisible(false);
                    jtxtHour_Pay.setVisible(false);
                    jtxtHour_Worked.setVisible(false);
                    jlblEmpWork.setVisible(false);
                }

            }
        });

        jrdbuttonFullTime.setBounds(113, 91, 109, 23);
        panel_1.add(jrdbuttonFullTime);

        jrdbtnContract.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                if (jrdbtnContract.isSelected()) {
                    //jrdbuttonFullTime.setSelected(false);

                    // ****************************************************
                    // In ActionListeners for radiobuttons, hide the fields you
                    // don't want to see, make visible the ones you do want to see
                    // ****************************************************
                    jlblEmpHour.setVisible(true);
                    jtxtHour_Pay.setVisible(true);
                    jlblEmpWork.setVisible(true);
                    jtxtHour_Worked.setVisible(true);
                    jlblEmpSal.setVisible(false);
                    jtxtSal.setVisible(false);
                }

            }
        });
        jrdbtnContract.setBounds(218, 91, 109, 23);
        panel_1.add(jrdbtnContract);

    }
}

Your radio buttons start off both unchecked, so you see no salary detail initially. 您的单选按钮均处于未选中状态,因此一开始您看不到工资明细。 When you click one or the other, the corresponding details appear. 单击一个或另一个时,将显示相应的详细信息。

在哪种情况下(哪个单选按钮已选中?),您想显示哪些控件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM