简体   繁体   中英

Components appear only when mouse hovers over them

I created a JFrame with the Swing framework, in which I added a JButton that will display a form created on a JPanel on the frame.

I added the action listener on the button and it is displaying panel as I expected;

But the boxes of components like radio buttons and checkboxes cannot be seen.

They appear only when I hover mouse over them.

Look at the other option in radio button and checkboxes:

Here is the code:

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, 0, 400, 400);
        add(p2);
        
        
        l1 = new JLabel("Name:");
        l1.setBounds(100,10,70,30);
        p2.add(l1);
        
        t1 = new JTextField();
        t1.setBounds(100,50,70,30);
        p2.add(t1);
        
        l2 = new JLabel("Gender:");
        l2.setBounds(100,130,70,30);
        p2.add(l2);
        
        r1 = new JRadioButton("Male");
        r1.setBounds(100,150,70,30);
        p2.add(r1);
        
        r1 = new JRadioButton("Female");
        r1.setBounds(150,150,70,30);
        p2.add(r1);
        
        l3 = new JLabel("Course:");
        l3.setBounds(100,180,70,30);
        p2.add(l3);
        
        chk1= new JCheckBox("Bca");
        chk1.setBounds(100, 200, 70, 30);
        p2.add(chk1);
        
        chk1= new JCheckBox("BBA");
        chk1.setBounds(150, 200, 70, 30);
        p2.add(chk1);
        
        chk1= new JCheckBox("BCOM");
        chk1.setBounds(200, 200, 70, 30);
        p2.add(chk1);
        
        l4 = new JLabel("Country:");
        l4.setBounds(100,220,70,30);
        p2.add(l4);
        
        String name[] = {"India","USA","UK","Rus"};
        
        com1=new JComboBox(name);
        com1.setBounds(100, 250, 70, 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) 
    {
        
        
    }

}

The Components to the left are too large and therefore overlap the checkboxes. Thats why they are not shown correctly. So move the right components like that:

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

and it will work.

This seems to work better.

  • I fixed radiobutton and checkbox positions.

  • I used different references for checkbox and radiobutton objects (r1, r2, chk1, chk2, chk3)

  • I added a buttongroup for radiobuttons.

     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, 0, 400, 400); add(p2); l1 = new JLabel("Name:"); l1.setBounds(100,10,70,30); p2.add(l1); t1 = new JTextField(); t1.setBounds(100,50,70,30); p2.add(t1); l2 = new JLabel("Gender:"); l2.setBounds(100,130,70,30); p2.add(l2); r1 = new JRadioButton("Male"); r1.setBounds(100,150,70,30); p2.add(r1); r2 = new JRadioButton("Female"); r2.setBounds(180,150,70,30); p2.add(r2); ButtonGroup bg=new ButtonGroup(); bg.add(r1); bg.add(r2); l3 = new JLabel("Course:"); l3.setBounds(100,180,70,30); p2.add(l3); chk1= new JCheckBox("Bca"); chk1.setBounds(100, 200, 70, 30); p2.add(chk1); chk2= new JCheckBox("BBA"); chk2.setBounds(180, 200, 70, 30); p2.add(chk2); chk3= new JCheckBox("BCOM"); chk3.setBounds(260, 200, 70, 30); p2.add(chk3); l4 = new JLabel("Country:"); l4.setBounds(100,220,70,30); p2.add(l4); String name[] = {"India","USA","UK","Rus"}; com1=new JComboBox(name); com1.setBounds(100, 250, 70, 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) { } }

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