gpt4 book ai didi

java - 无法查看添加到框架中心的第一个 JPanel

转载 作者:行者123 更新时间:2023-12-01 14:35:40 26 4
gpt4 key购买 nike

看看我的代码,当我尝试在安装程序屏幕中央添加一个阶段时,我在打开 JFrame 时什么也没有得到。我通过将 add() 方法放置在重写的 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);
}
}
}
}
}

最佳答案

这是重写方法 protected void addImpl(Component comp, Object Constraints, int index) 给您带来了问题。如果你想向 JFrame centerView 添加一些东西,只需添加到它即可,就像添加 JButtons exit、back next 和 finish 到 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 而不是框架中。尝试删除覆盖的方法,您会发现它全部可见,除了您必须在其他地方实现的中心面板。

关于java - 无法查看添加到框架中心的第一个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16509488/

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