Java Swing JFrame布局 [英] Java Swing JFrame Layout

查看:133
本文介绍了Java Swing JFrame布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚写了一个简单的代码,我希望在主框架上显示一个文本字段和一个按钮,但在运行后我看到的是文本字段。

I just wrote a simple code where I want a textfield and a button to appear on the main frame, but after running all I see is the textfield.

如果我在文本字段后面写了按钮的代码,然后只显示了按钮。

If I write the code of the button after the textfield then only the button is displayed.

知道为什么吗?

    JFrame mainframe=new JFrame();
    mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainframe.setBounds(0,0,200,200);
    JButton jb=new JButton();
    jb.setText("Leech");
    mainframe.add(jb);
    JTextField link=new JTextField(50);
    mainframe.add(link);
    mainframe.pack();
    mainframe.setVisible(true);


推荐答案

将您的组件添加到JPanel,然后添加该面板到JFrame的ContentPane。

Add your components to a JPanel and then add that panel to the ContentPane of JFrame.

JFrame window = new JFrame();
JPanel mainframe = new JPanel();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(0,0,200,200);

JButton jb = new JButton();
jb.setText("Leech");

mainframe.add(jb);

JTextField link = new JTextField(50);
mainframe.add(link);

window.getContentPane().add(mainframe);
window.pack();
window.setVisible(true);

这篇关于Java Swing JFrame布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆