简体   繁体   English

使用单选按钮侦听器时,两个标签彼此重叠

[英]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- 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); 尝试在X-Ycooridantes方向上更改您的“注册位置”,假设更改以下代码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); 并以相同的方式将l1更改为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. 除了上面提供的答案外,如果要隐藏其他标签,可以将其可见性设置为false并重新绘制父组件。 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. 请注意,此处不需要调用revalidate方法,因为您没有从层次结构中删除任何组件。

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();
    }
}

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

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