如何在现有面板的特定位置添加按钮?

时间:2016-10-25 11:34:09

标签: java swing

我是java中UI的新手。

向现有面板添加按钮时遇到问题:

public void run() {
    frame.setPreferredSize(new Dimension(1200, 756));
    frame.setResizable(false);
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() / 2) - 600);
    int y = (int) ((dimension.getHeight() / 2) - 378);
    frame.setLocation(x, y);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfxPanel = new JFXPanel();
    createScene();
    panel.add(jfxPanel, BorderLayout.CENTER);

    //start_button
    start_button.setBounds(200, 550, 230, 69);
    panel.add(start_button);

    frame.getContentPane().add(panel);
    File a = new File("src/images/connection_error.png");
    File parentFolder = new File(a.getParent());
    File b = new File(parentFolder, "../images/connection_error.png");

    try {
        loadURL("file:/" + b.getCanonicalPath());
    } catch (IOException ex) {
        Logger.getLogger(SimpleSwingBrowser.class.getName()).log(Level.SEVERE, null, ex);
    }
    startLoader("ftp://31.130.206.175/client_content/index.html");

    frame.pack();
    frame.setVisible(true);
}

这个 start_button 只是覆盖了整个面板,但我需要将它放在具有特定大小的特定位置。它不应该弄乱面板上的任何项目(图像和加载的http)。我该怎么办?

谢谢。

1 个答案:

答案 0 :(得分:0)

您必须跳过使用BorderLayout panel - 导致按钮填满整个容器 - 并设置jfxPanel的边界,就像您之前使用按钮一样将它添加到`panel。

有关布局管理器的更多信息,通常可以在此处找到Swing组件展示位置:https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html