gpt4 book ai didi

java - 更改 JxBrowser JFrame 中 JButton 大小的问题

转载 作者:行者123 更新时间:2023-11-30 05:38:34 25 4
gpt4 key购买 nike

所以我正在使用 JxBrowser 并将其添加到 JFrame 中。现在我想在浏览器的右侧添加多个按钮,但我尝试什么并不重要(test1.setSize 或 test1.setPreferredSize 等它不会改变大小)。

Picture of problem

这张图片中的红色矩形是我想要在 JFrame 右侧的 JButton 大小的示例。为什么 JButton 仍然那么大?

这是代码:

    public test() {
test1 = new JButton("test");
test1.addActionListener(this);
browser = new Browser();
view = new BrowserView(browser);

JFrame frame = new JFrame("FOEBot - Gemaakt door Gerrit");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.add(test1, BorderLayout.AFTER_LINE_ENDS);
frame.setSize(1500, 1000);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

browser.loadURL("https://www.google.nl/");
}

请帮帮我。

最佳答案

你真的应该看看Layout documentation .

这里有一个我如何解决你的问题的例子。我将 JButtons 放入另一个用作 BoxLayout 的容器中:

public static void main(String[] args) {
JPanel mainContainer = new JPanel();
mainContainer.setLayout(new BorderLayout());

JFrame jf = new JFrame();
jf.add(mainContainer);

JPanel browser = new JPanel();
JLabel browserDummy = new JLabel("Browser");
browser.add(browserDummy);
browserDummy.setFont(new Font("Arial", Font.BOLD, 200));

JPanel buttonContainer = new JPanel();
buttonContainer.setLayout(new BoxLayout(buttonContainer, BoxLayout.Y_AXIS));
buttonContainer.add(new JButton("Button one"));
buttonContainer.add(new JButton("Button two"));

mainContainer.add(browser, BorderLayout.WEST);
mainContainer.add(buttonContainer, BorderLayout.EAST);

jf.setVisible(true);
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

我从来没有在 Swing 中对浏览器编程做过任何事情,所以我只是使用了一些虚拟程序,但原理应该是相同的。

结果: enter image description here

关于java - 更改 JxBrowser JFrame 中 JButton 大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56158046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com