微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅当鼠标悬停在组件上时才会出现组件

如何解决仅当鼠标悬停在组件上时才会出现组件

我使用 Swing 框架创建了一个 JFrame,在其中添加一个 JButton,它将显示在框架的 JPanel 上创建的表单。

我在按钮上添加了动作监听器,它按照我的预期显示面板;

但是无法看到单选按钮和复选框等组件的框。

只有当我将鼠标悬停在它们上面时它们才会出现。

查看单选按钮和复选框中的另一个选项:

代码如下:

package codes;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

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.JRadioButton;
import javax.swing.JTextField;

public class JFrameBackground extends JFrame implements ActionListener
{

    
    private static final long serialVersionUID = 1L;
    
    
    JLabel l1,l2,l3,l4;
    JTextField t1;
    JButton b1;
    JRadioButton r1;
    JComboBox com1;
    JCheckBox chk1;
    
    JPanel p2;
    JButton b;
    
    

    public JFrameBackground() 
    {
        
        p2=new JPanel();
        p2.setLayout(null);
        p2.setBounds(250,400,400);
        add(p2);
        
        
        l1 = new JLabel("Name:");
        l1.setBounds(100,10,70,30);
        p2.add(l1);
        
        t1 = new JTextField();
        t1.setBounds(100,50,30);
        p2.add(t1);
        
        l2 = new JLabel("Gender:");
        l2.setBounds(100,130,30);
        p2.add(l2);
        
        r1 = new JRadioButton("Male");
        r1.setBounds(100,150,30);
        p2.add(r1);
        
        r1 = new JRadioButton("Female");
        r1.setBounds(150,30);
        p2.add(r1);
        
        l3 = new JLabel("Course:");
        l3.setBounds(100,180,30);
        p2.add(l3);
        
        chk1= new JCheckBox("Bca");
        chk1.setBounds(100,200,30);
        p2.add(chk1);
        
        chk1= new JCheckBox("BBA");
        chk1.setBounds(150,30);
        p2.add(chk1);
        
        chk1= new JCheckBox("BCOM");
        chk1.setBounds(200,30);
        p2.add(chk1);
        
        l4 = new JLabel("Country:");
        l4.setBounds(100,220,30);
        p2.add(l4);
        
        String name[] = {"India","USA","UK","Rus"};
        
        com1=new JComboBox(name);
        com1.setBounds(100,250,30);
        p2.add(com1);
        
        b1= new JButton("Submit");
        b1.setBounds(140,300,90,30);
        p2.add(b1);
        
        
        
        
        b =new JButton("Show form");
        b.setBounds(0,4,190,40);
        b.setFocusPainted(false);
        b.setBorderPainted(false);
        b.addActionListener(this);
        add(b);
        
        b.addMouseListener(new MouseAdapter() 
        {
            public void mouseClicked(MouseEvent me)
            {
                
                p2.setVisible(true);
                
                
            }
        });
        
        
        
        Container c=getContentPane();
        c.setBackground(Color.gray);
        setBounds(170,100,1250,500);
        
        setLayout(null);
        setVisible(true);
    }


    public static void main(String[] args) 
    {
        
        new JFrameBackground();
        

    }

    public void actionPerformed(ActionEvent arg0) 
    {
        
        
    }

}

解决方法

左侧的组件太大,因此与复选框重叠。这就是为什么它们没有正确显示的原因。所以像这样移动正确的组件:

 r1.setBounds(170,150,70,30);
 chk1.setBounds(170,200,30);              
 chk1.setBounds(240,30);

它会起作用。

,

这似乎效果更好。

  • 我修正了单选按钮和复选框的位置。

  • 我对复选框和单选按钮对象(r1、r2、chk1、chk2、chk3)使用了不同的引用

  • 我为单选按钮添加了一个按钮组。

     import java.awt.Color;
     import java.awt.Container;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import java.awt.event.MouseAdapter;
     import java.awt.event.MouseEvent;
    
     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.JRadioButton;
     import javax.swing.JTextField;
    
     import javax.swing.ButtonGroup;
    
     public class JFrameBackground extends JFrame implements ActionListener
     {
    
    
         private static final long serialVersionUID = 1L;
    
    
         JLabel l1,l2,l3,l4;
         JTextField t1;
         JButton b1;
         JRadioButton r1,r2;
         JComboBox com1;
         JCheckBox chk1,chk2,chk3;
    
         JPanel p2;
         JButton b;
    
    
    
         public JFrameBackground() 
         {
    
             p2=new JPanel();
             p2.setLayout(null);
             p2.setBounds(250,400,400);
             add(p2);
    
    
             l1 = new JLabel("Name:");
             l1.setBounds(100,10,30);
             p2.add(l1);
    
             t1 = new JTextField();
             t1.setBounds(100,50,30);
             p2.add(t1);
    
             l2 = new JLabel("Gender:");
             l2.setBounds(100,130,30);
             p2.add(l2);
    
             r1 = new JRadioButton("Male");
             r1.setBounds(100,30);
             p2.add(r1);
    
             r2 = new JRadioButton("Female");
             r2.setBounds(180,30);
             p2.add(r2);
    
             ButtonGroup bg=new ButtonGroup();    
             bg.add(r1);
             bg.add(r2); 
    
             l3 = new JLabel("Course:");
             l3.setBounds(100,180,30);
             p2.add(l3);
    
             chk1= new JCheckBox("Bca");
             chk1.setBounds(100,30);
             p2.add(chk1);
    
             chk2= new JCheckBox("BBA");
             chk2.setBounds(180,30);
             p2.add(chk2);
    
             chk3= new JCheckBox("BCOM");
             chk3.setBounds(260,30);
             p2.add(chk3);
    
             l4 = new JLabel("Country:");
             l4.setBounds(100,220,30);
             p2.add(l4);
    
             String name[] = {"India","USA","UK","Rus"};
    
             com1=new JComboBox(name);
             com1.setBounds(100,250,30);
             p2.add(com1);
    
             b1= new JButton("Submit");
             b1.setBounds(140,300,90,30);
             p2.add(b1);
    
    
    
    
             b =new JButton("Show form");
             b.setBounds(0,4,190,40);
             b.setFocusPainted(false);
             b.setBorderPainted(false);
             b.addActionListener(this);
             getContentPane().add(b);
    
             b.addMouseListener(new MouseAdapter() 
             {
                 public void mouseClicked(MouseEvent me)
                 {
    
                     p2.setVisible(true);
    
    
                 }
             });
    
    
    
             Container c=getContentPane();
             c.setBackground(Color.gray);
             setBounds(170,100,1250,500);
    
             setLayout(null);
             setVisible(true);
         }
    
    
         public static void main(String[] args) 
         {
    
             new JFrameBackground();
    
    
         }
    
         public void actionPerformed(ActionEvent arg0) 
         {
    
    
         }
    
     }
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。