这里是新手。
我正在尝试创建一个框架,其中有一个黑色面板用于在其中创建一些动画,还有一个 ui 面板用于放置控制按钮。但是当我创建 JButtons 并将它们添加到 ui 面板时,它们似乎在 ui 面板之外。
我认为这是因为布局,但我无法决定哪种布局最适合我的情况。尝试过 GridLayout、GridBoxLayout、BoxLayout...它们似乎都没有问题,我很确定我遗漏了什么。
Oracle 文档非常丰富,但我不能说它对我有很大帮助,很难理解布局。
这是我的代码: 感谢 Berger 的回答,我编辑了我的代码,但仍然遇到问题,所以我更新了代码和屏幕截图以使其清晰。
没关系,我忘了修复某些部分,Berger 的回答非常有效。谢谢!
import java.awt.*;
import javax.swing.*;
public class gui {
public static void InitializeGUI(){
//DEFINE MAIN FRAME
JFrame window = new JFrame("Simulator");
window.setSize(1000, 1000);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
//DEFINE UNIVERSE PANEL
JPanel universe = new JPanel(){
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, 3 * window.getSize().height / 4);
}
};
universe.setBackground(Color.BLACK);
//DEFINE UI PANEL
JPanel ui = new JPanel() {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, window.getSize().height / 4);
}
};
//DEFINE BUTTONS
JButton femalespawnbutton = new JButton("Spawn Female");
femalespawnbutton.setSize(150, 100);
JButton malespawnbutton = new JButton("Spawn Male");
malespawnbutton.setSize(150, 100);
//FILL
ui.add(femalespawnbutton, BorderLayout.CENTER);
ui.add(malespawnbutton, BorderLayout.CENTER);
window.add(universe, BorderLayout.NORTH);
window.add(ui, BorderLayout.SOUTH);
window.setVisible(true);
}
}
最佳答案
第一件事是将两个面板都添加到 window
,其内容 Pane 有一个 BorderLayout
.
所以
window.add(universe);
window.add(ui;
与
相同window.add(universe, BorderLayout.CENTER);
window.add(ui, BorderLayout.CENTER);
并且两个组件都被添加到同一个地方(按钮可能被隐藏了)
为避免这种情况,您可以在 BorderLayout
中给出指示位置的约束。你想添加你的组件,.eg :
window.add(universe, BorderLayout.CENTER);
window.add(ui, BorderLayout.SOUTH);
第二件事是您正在使用setSize()
,除非您没有布局管理器,否则不会被考虑在内。
您可以使用 setPreferredSize
相反,它的 height
某些布局管理器将尊重值(value),例如 BorderLayout
对于 SOUTH
或 NORTH
成分 。 :
ui.setPreferredSize(new Dimension(1000, 250));
现在,如果您希望它适应窗口的大小,请改写该方法:
JPanel ui = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, window.getContentPane().getSize().height / 4);
}
};
把它们放在一起(因为南组件的首选高度得到尊重,在你的情况下不需要给中心组件一个,它会占用剩余的空间):
//DEFINE MAIN FRAME
final JFrame window = new JFrame("Simulator");
window.setSize(1000, 1000);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
//DEFINE UNIVERSE PANEL
JPanel universe = new JPanel();
universe.setBackground(Color.BLACK);
//DEFINE UI PANEL
JPanel ui = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(window.getContentPane().getSize().width, window.getContentPane().getSize().height / 4);
}
};
//DEFINE BUTTONS
JButton femalespawnbutton = new JButton("Spawn Female");
femalespawnbutton.setSize(250, 100);
JButton malespawnbutton = new JButton("Spawn Male");
malespawnbutton.setSize(250, 100);
//FILL
ui.add(femalespawnbutton);
ui.add(malespawnbutton);
window.add(universe, BorderLayout.CENTER);
window.add(ui, BorderLayout.SOUTH);
window.setVisible(true);
最后的重要说明:
尽管这种方法在某些情况下有效,但不鼓励您自己设置组件的大小(顶级容器如 windows 除外):
参见:Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
关于java - JButtons 显示在父面板之外(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553712/