所有按钮都不显示

时间:2014-06-13 21:24:39

标签: java swing

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中显示

1 个答案:

答案 0 :(得分:3)

JFrame默认情况下使用BorderLayout,您只能在北,南,西,东和中心的每个部分添加单个组件。

您正在中心添加两个组件,只有最后一个组件可见。

add(pane); // Added in center
...
add(noo);  // Added in center and replaced last one     <<-- Here is the problem

将其添加到不同的部分(片段)或使用符合您需要的其他布局。


了解更多