java的Swing设计中,页面组件需要拖动才显示的问题

在学习到java的Swing程序设计写一个多选按钮时,发现如下代码写完后,swing页面无法显示组件,必须调节下页面大小才可以显示。

public class JRadioButtonTest extends JFrame{
    private static final long serialVersionUID = 1L;
    public JRadioButtonTest() {
        Container c = getContentPane();
        setSize(300,200);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
        JLabel jl = new JLabel("性别");
        jl.setFont(new Font("微软雅黑",Font.PLAIN,20));
        c.add(jl);
        JRadioButton JRB = new JRadioButton("男");
        JRB.setFont(new Font("微软雅黑",Font.PLAIN,20));
        c.add(JRB);
        
        JRadioButton JRB1 = new JRadioButton("女");
        JRB1.setFont(new Font("微软雅黑",Font.PLAIN,20));
        c.add(JRB1);
        
        ButtonGroup group = new ButtonGroup();
        group.add(JRB);
        group.add(JRB1);
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new JRadioButtonTest();

    }

}

效果如图:

 

于是又用了另一种方式来写,如下

public class JRadioButtonTest extends JFrame{
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    public JRadioButtonTest() {
        setBounds(100,100,300,200);
        contentPane = new JPanel();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        setContentPane(contentPane);
        contentPane.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
        JLabel jl = new JLabel("性别");
        jl.setFont(new Font("微软雅黑",Font.PLAIN,20));
        contentPane.add(jl);
        JRadioButton JRB = new JRadioButton("男");
        JRB.setFont(new Font("微软雅黑",Font.PLAIN,20));
        contentPane.add(JRB);
        
        JRadioButton JRB1 = new JRadioButton("女");
        JRB1.setFont(new Font("微软雅黑",Font.PLAIN,20));
        contentPane.add(JRB1);
        
        ButtonGroup group = new ButtonGroup();
        group.add(JRB);
        group.add(JRB1);
        
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                try {
                    JRadioButtonTest frame = new JRadioButtonTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
            }
        });

    }

}
改成这种方式后,可以正常显示了,不需要拖动窗口,运行程序即可显示组件。如图

通过比较与观察,得出结论是与setVisible的位置有关,这个方法是设置页面可见的,如果把它放在组件对象的前面,那么刚打开页面就无法显示组件。由于在写Swing程序时,习惯性的把页面必须的设置写完,就没注意到这点。

 

得出结论

这个问题的结论就是:setVisible必须放在最后,这样才可以把所有设置可以显示的组件在刚打开页面时,就显示出来。

  • 21
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,关于Java Swing实现组件拖动,我可以给您一些简洁的介绍和示例代码。在Java Swing,您可以通过MouseListener和MouseMotionListener接口来实现组件拖动。首先,我们需要组件添加MouseListener和MouseMotionListener,然后在MousePressed事件记录鼠标点击位置的相对坐标以及组件的位置,并在鼠标拖动时计算组件的新位置。下面是一个简单的示例代码: ``` import java.awt.Point; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class DraggingDemo extends JFrame { private JButton button; public DraggingDemo() { button = new JButton("Drag me!"); button.addMouseListener(new MouseAdapter() { private Point mouseClickPoint; @Override public void mousePressed(MouseEvent e) { mouseClickPoint = e.getPoint(); } }); button.addMouseMotionListener(new MouseAdapter() { private Point initialButtonLocation; @Override public void mouseDragged(MouseEvent e) { Point currentPoint = e.getLocationOnScreen(); int x = currentPoint.x - mouseClickPoint.x; int y = currentPoint.y - mouseClickPoint.y; button.setLocation(x, y); } }); JPanel panel = new JPanel(); panel.add(button); add(panel); setSize(400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new DraggingDemo(); } } ``` 您可以根据需要修改代码以适应您的项目要求。希望这可以帮助您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值