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

时间:2016-12-01 03:20:22

标签: java jframe jbutton

在我的迷你浏览器程序中,我的buttonPanel包含backButton,forwardButton和locationTextField。 fPane包含buttonPanel和jfxPanel。浏览器窗口应该看起来像顶部的buttonPanel,其他东西覆盖窗口的其余部分。

这是我的构造函数部分:

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

当我编译它时,它看起来像这样:

enter image description here

buttonPanel转到一边而不是在窗口顶部,后退按钮消失,并在页面下方创建额外的空间。

1 个答案:

答案 0 :(得分:1)

我认为按钮位于buttonPanel内,然后您可以尝试

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

之前