簡體   English   中英

僅當鼠標懸停在組件上時才會出現組件

[英]Components appear only when mouse hovers over them

我使用 Swing 框架創建了一個JFrame ,其中我添加了一個JButton ,它將顯示在框架上的JPanel上創建的表單。

我在按鈕上添加了動作偵聽器,它按預期顯示面板;

但是無法看到諸如單選按鈕和復選框之類的組件框。

只有當我將 hover 鼠標懸停在它們上面時,它們才會出現。

查看單選按鈕和復選框中的另一個選項:

這是代碼:

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) 
    {
        
        
    }

}

左側的組件太大,因此與復選框重疊。 這就是為什么它們沒有正確顯示。 所以像這樣移動正確的組件:

 r1.setBounds(170, 150, 70, 30);
 chk1.setBounds(170, 200, 70, 30);              
 chk1.setBounds(240, 200, 70, 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, 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) { } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM