import java.awt.*;
import javax.swing.*;
public class Crisis extends JFrame {
public Crisis() {
super("Crisis");
setLookAndFeel();
setSize(348, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panicButton = new JButton("Panic");
dontPanicButton = new JButton("Don't Panic");
blameButton = new JButton("Blame Others");
mediaButton = new JButton("Notify the Media");
saveButton = new JButton("Save Yourself");
JPanel pane = new JPanel();
BorderLayout moo = new BorderLayout();
pane.setLayout(moo);
pane.add(panicButton, BorderLayout.NORTH);
pane.add(dontPanicButton, BorderLayout.SOUTH);
add(pane);
FlowLayout flo = new FlowLayout(FlowLayout.CENTER,10,10);
JPanel noo = new JPanel();
noo.setLayout(flo);
noo.add(blameButton);
noo.add(mediaButton);
noo.add(saveButton);
add(noo);
setVisible(true);
}
public static void main(String[] arguments) {
new Crisis();
}
}
panicButton 和 dontPanicButton 不显示在 GUI 中
最佳答案
JFrame默认情况下使用 BorderLayout并且您只能在北、南、西、东和中心的每个部分添加单个组件。
您要在中心添加两个组件,只有最后一个可见。
add(pane); // Added in center
...
add(noo); // Added in center and replaced last one <<-- Here is the problem
将其添加到不同的部分(段)或根据您的需要使用其他适合的布局。
<小时/>了解更多
关于java - 所有按钮均不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24213993/