简体   繁体   English

如何在JFrame的窗口顶部设置Button面板?

[英]How to set Button panel at the top of the window in JFrame?

In my mini browser program my buttonPanel contains backButton, forwardButton and locationTextField. 在我的小型浏览器程序中,我的buttonPanel包含backButton,forwardButton和locationTextField。 fPane contains buttonPanel and jfxPanel. fPane包含buttonPanel和jfxPanel。 The browser window should be look like the buttonPanel on the top and the other things cover the rest of the window. 浏览器窗口应该看起来像顶部的buttonPanel,其他内容覆盖了窗口的其余部分。

Here is my constructor portion: 这是我的构造器部分:

private JButton backButton = new JButton(), forwardButton = new JButton();

private final JFXPanel jfxPanel = new JFXPanel();

private JTextField locationTextField = new JTextField(40);

private JPanel fPane = new JPanel();

public Browser(){
createScene();
setPreferredSize(new Dimension(1024,600));
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel(new BorderLayout(5,0));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
buttonPanel.setBackground(Color.darkGray);
backButton.setIcon(new ImageIcon(Hello.class.getResource("/left.png")));
backButton.setBackground(Color.DARK_GRAY);
buttonPanel.add(backButton,BorderLayout.NORTH);
forwardButton.setIcon(new ImageIcon(Hello.class.getResource("/forward.png")));
forwardButton.setBackground(Color.DARK_GRAY);
buttonPanel.add(forwardButton,BorderLayout.NORTH);
buttonPanel.add(locationTextField,BorderLayout.CENTER);
fPane.add(buttonPanel,BorderLayout.NORTH);
fPane.add(jfxPanel, BorderLayout.CENTER);
getContentPane().setLayout(new BorderLayout(5,0));
getContentPane().add(fPane, BorderLayout.WEST);
pack();
}

When i compile it,it looks like this: 当我编译它时,它看起来像这样:

在此处输入图片说明

the buttonPanel goes aside instead of going top of the window, back button disappears and extra space is created below the page. buttonPanel放在一边而不是放在窗口顶部,后退按钮消失,并且在页面下方创建了额外的空间。

I assumed the buttons are placed inside buttonPanel , then you might try 我假设按钮位于buttonPanel ,那么您可以尝试

getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(fPane, BorderLayout.CENTER);

before the pack() pack()之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM