简体   繁体   中英

Two labels overlap each other when using the Radio Button Listener

I made a simple program where there are two radio buttons each with an action listener. After pressing first button, a label is printed and the same thing happens with the other. The problem is that both the labels overlap after pressing first and second button.

Edit-The previous label must be removed and then new label must be on the screen.
ex-

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ckbxdm{
    JFrame frame;
    JRadioButton r1,r2;
    ButtonGroup grp;
    JLabel l1,l2;

    void box(){
        frame=new JFrame("Hello");
        r1=new JRadioButton("Login");
        r2=new JRadioButton("Signup");
        grp=new ButtonGroup();
        grp.add(r1);
        grp.add(r2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.getContentPane().add(r1);
        frame.getContentPane().add(r2);
        r1.setBounds(100,120,100,20);
        r2.setBounds(200,120,100,20);
        frame.setBounds(100,100,500,500);
        frame.setVisible(true);
        r1.addActionListener(new listener1());
        r2.addActionListener(new listener2());
    }
    class listener1 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            frame.getContentPane().repaint();
            frame.getContentPane().revalidate();
            l1=new JLabel("Login area");
            frame.getContentPane().add(l1);
            l1.setBounds(100,200,100,20);
        }
    }
    class listener2 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            frame.getContentPane().repaint();
            frame.getContentPane().revalidate();
            l2=new JLabel("Signup area");
            frame.getContentPane().add(l2);
            l2.setBounds(100,200,100,20);
        }
    }
    public void itemStateChanged(ItemEvent ie){
        frame.repaint();
    }
}
public class CheckboxDemo{
    public static void main(String args[]){
        ckbxdm obj=new ckbxdm();
        obj.box();
    }
}

try to change ur "sign up position" in X-Ycooridantes direction, let say change following line of code l2.setBounds(100,200,100,20);

to

l2.setBounds(200,200,100,20);

and the same way for l1 change it to l1.setBounds(50,200,100,20); , it will definitely work

In addition to the answer provided above, if you want to hide the other label, you can set its visibility to false and repaint the parent component. You can learn more from here . Note that calling the revalidate method here is unnecessary since you are not removing any component from the hierarchy.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ckbxdm{
    JFrame frame;
    JRadioButton r1,r2;
    ButtonGroup grp;
    JLabel l1,l2;

    void box(){
        frame=new JFrame("Hello");
        r1=new JRadioButton("Login");
        r2=new JRadioButton("Signup");
        grp=new ButtonGroup();
        grp.add(r1);
        grp.add(r2);

        l1=new JLabel("Login area");
        l1.setBounds(100,200,100,20);
        l1.setVisible(false);
        frame.getContentPane().add(l1);

        l2=new JLabel("Signup area");
        l2.setBounds(100,200,100,20);
        l2.setVisible(false);
        frame.getContentPane().add(l2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.getContentPane().add(r1);
        frame.getContentPane().add(r2);
        r1.setBounds(100,120,100,20);
        r2.setBounds(200,120,100,20);
        frame.setBounds(100,100,500,500);
        frame.setVisible(true);
        r1.addActionListener(new listener1());
        r2.addActionListener(new listener2());
    }
    class listener1 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            l2.setVisible(false);
            l1.setVisible(true);
            frame.getContentPane().repaint();

        }
    }
    class listener2 implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            l1.setVisible(false);
            l2.setVisible(true);
            frame.getContentPane().repaint();
        }
    }
    public void itemStateChanged(ItemEvent ie){
        frame.repaint();
    }
}
public class CheckboxDemo{
    public static void main(String args[]){
        ckbxdm obj=new ckbxdm();
        obj.box();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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