无法查看在帧中心添加的第一个JPanel

时间:2013-05-12 16:24:08

标签: java swing jframe jpanel

当我尝试在安装程序屏幕的中心添加一个舞台时,看一下我的代码我在打开JFrame时什么也得不到。我通过将add()方法放在overriden setVisible()方法中来实现相同的代码,我故意不会放置工作代码,因为我想知道这段代码有什么问题。

public class InstallerFrameDemo extends JFrame {

    private static final long serialVersionUID = 2L;

    private JPanel masterPanel;
    private JPanel titlePanel;
    private JLabel title;
    private JPanel centerView;
    private JPanel currentProgress;
    private JPanel buttonPanel;

    private JButton next;
    private JButton back;
    private JButton exit;
    private JButton finished;
    private InstallerModel model;   
    private NavigationButtonsListener navButtonsListener;

    public InstallerFrameDemo(InstallerModel model) {
        if (model == null) {
            throw new NullPointerException();
        }

        this.model = model;

        masterPanel = new JPanel();
        titlePanel = new JPanel();
        title = new JLabel("Welcome to application installer");
        centerView = new JPanel();
        currentProgress = new JPanel();
        buttonPanel = new JPanel();

        next = new JButton("Next");
        back = new JButton("Back");
        exit = new JButton("Exit");
        finished = new JButton("Finished");

        navButtonsListener = new NavigationButtonsListener();

        add(model.getCurrentStage());

        back.setEnabled(false);
        finished.setEnabled(false);

        if (model.getStageCount() <= 1) {
            next.setEnabled(false);
            exit.setEnabled(false);
            finished.setEnabled(true);
        }

        exit.addActionListener(navButtonsListener);
        next.addActionListener(navButtonsListener);
        back.addActionListener(navButtonsListener);
        finished.addActionListener(navButtonsListener);

        masterPanel.setBackground(Color.WHITE);
        titlePanel.setBackground(new Color(230, 230, 230));
        centerView.setBackground(Color.WHITE);
        buttonPanel.setBackground(Color.WHITE);
        currentProgress.setBackground(Color.decode("#26b8ee"));

        title.setBorder(new EmptyBorder(15, 15, 15, 15));

        title.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));
        currentProgress.setPreferredSize(new Dimension(4, 4));

        masterPanel.setLayout(new BorderLayout());
        centerView.setLayout(new BorderLayout());
        buttonPanel.setLayout(new GridLayout());

        titlePanel.add(title);

        centerView.add(currentProgress, BorderLayout.PAGE_END);

        buttonPanel.add(exit);
        buttonPanel.add(back);
        buttonPanel.add(next);
        buttonPanel.add(finished);

        masterPanel.add(titlePanel, BorderLayout.PAGE_START);
        masterPanel.add(centerView, BorderLayout.CENTER);
        masterPanel.add(buttonPanel, BorderLayout.PAGE_END);

        setContentPane(masterPanel);

        setTitle("Welcome to application installer");
        setSize(800, 600);
    }

    protected void addImpl(Component comp, Object constraints, int index) {
        if (comp == null) {
            throw new NullPointerException();
        }
        if (centerView != null) {
            centerView.removeAll();

            // able to see
            centerView.add(currentProgress, BorderLayout.PAGE_END); // successfully added

            if (comp instanceof InstallerStage) {
                InstallerStage stage = (InstallerStage) comp;
                title.setText(stage.getTitle());
                this.centerView.add(comp, BorderLayout.CENTER, index);
                System.out.println(comp); // For first stage toString return - in first stage
                // Got the output - in first stage but no change in view

                repaint(); // Test 1
                revalidate(); // Test 2
            }
        } else {
            super.addImpl(comp, constraints, index);
        }
    }


    public InstallerModel getInstallerModel() {
        return model;
    }

    private class NavigationButtonsListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == exit) {
                System.exit(0);
            }
            if (ae.getSource() == back) {
                finished.setEnabled(false);
                exit.setEnabled(true);
                InstallerStage prevStage = model.getPreviousStage();
                if (prevStage != null) {
                    add(prevStage);
                    next.setEnabled(true);
                    if (model.getCurrentStageIndex() == 0) {
                        back.setEnabled(false);
                    }
                }
            }
            if (ae.getSource() == next) {
                InstallerStage currentStage = model.getCurrentStage();
                if (currentStage.process()) {
                    InstallerStage nextStage = model.getNextStage();
                    if (nextStage != null) {
                        add(nextStage);
                        back.setEnabled(true);
                        if (model.getCurrentStageIndex() == model
                                .getStageCount() - 1) {
                            next.setEnabled(false);
                            finished.setEnabled(true);
                            exit.setEnabled(false);
                        }
                    }
                }
            }
            if (ae.getSource() == finished) {
                InstallerStage currentStage = model.getCurrentStage();
                if (currentStage.process()) {
                    System.exit(0);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

它是覆盖方法protected void addImpl(Component comp, Object constraints, int index),它会给你一个问题。如果你想向JFrame centerView添加一些东西,只需添加它,就像你添加JButtons exit,然后返回next并完成buttonPanel;不要覆盖addImpl()方法。正如您在JavaDocs(http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#addImpl%28java.awt.Component,%20java.lang.Object,%20int%29)中所看到的那样,覆盖该方法时,会将子项添加到contentPane而不是框架中。 尝试删除覆盖的方法,除了您必须在其他地方实施的中心面板之外,您将看到它全部可见。