组件仅在鼠标悬停在其上时出现
Components appear only when mouse hovers over them
我用 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, 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)
{
}
}
我用 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, 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) { } }