我是靠谱客的博主 怡然可乐,这篇文章主要介绍java中frame布局,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.

Any idea why?

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);

解决方案

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中frame布局,Java Swing JFrame布局的全部内容,更多相关java中frame布局,Java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(128)

评论列表共有 0 条评论

立即
投稿
返回
顶部