BorderLayout中嵌套JPanel无法显示组件

流光_ 2016-07-30 01:26:18
在JFrame的子类中使用了CardLayout来管理四个JPanel子类,而在其中一个子类中,用了BorderLayout来布局,并在BorderLayout中的每一个部分使用了JPanel;这样设置后所有的组件都无法显示。

JFrame 子类:
public class Frame extends JFrame implements PanelSwitch {

private static final long serialVersionUID = -1629925106866926779L;

private static final String CARD_HOME = "home";
private static final String CARD_ROLECALL = "roleCall";
private static final String CARD_SESSION = "session";
private static final String CARD_SETTING = "setting";

private JPanel homePanel;
private JPanel rolecallPanel;
private JPanel sessionPanel;
private JPanel settingPanel;

private JPanel mainPanel = new JPanel();

private CardLayout layout = new CardLayout();

private Conference conference;

public Frame() {

homePanel = new HomePanel(this);
rolecallPanel = new RolecallPanel(this);
sessionPanel = new SessionPanel(this);
settingPanel = new SessionPanel(this);

initFrame();

}

private void initFrame() {
setLayout(layout);

add(homePanel, CARD_HOME);
add(rolecallPanel, CARD_ROLECALL);
add(sessionPanel, CARD_SESSION);
add(settingPanel, CARD_SETTING);

setVisible(true);
}

public void switch_rolecallPanel() {
layout.show(this, CARD_ROLECALL);
}

public void switch_sessionPanel() {
layout.show(this, CARD_SESSION);
}

public void switch_homePanel() {
layout.show(this, CARD_HOME);
}

public void switch_settingPanel() {
layout.show(this, CARD_SETTING);
}

public static void main(String [] args) {
SwingConsole.run(new Frame());
// 一个辅助类,用于设置JFrame的title, height, width等属性
}
}


JPanel 子类
public class HomePanel extends JPanel {

private static final long serialVersionUID = -2977633553899021068L;

private JLabel la_ConfName;
private JLabel la_ConfTopic;
private PanelSwitch frame;

private JButton bt_roleCallStart;
private JButton bt_settings;

private ActionListener lis_roleCallStart = new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.switch_rolecallPanel();
}
};

private ActionListener lis_settings = new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.switch_settingPanel();
}
};

private Conference conferenceSetting;

private BorderLayout layout = new BorderLayout();

public HomePanel(PanelSwitch frame) {
this.frame = frame;

initTextField();
initButtons();
initProductInfo();

setLayout(layout);
setVisible(true);
validate();
}

private void initTextField() {
la_ConfName = new JLabel();
la_ConfName.setFont(GraphicsConfig.getTitleFont());
la_ConfName.setVisible(true);

la_ConfTopic = new JLabel();
la_ConfTopic.setFont(GraphicsConfig.getTitleFont());
la_ConfTopic.setVisible(true);

//// FIXME: 7/30/16
// la_ConfName.setText(conferenceSetting.getName());
la_ConfName.setText("Title");
// la_ConfTopic.setText(conferenceSetting.getTopic());
la_ConfTopic.setText("subTitle");

JPanel confInfo = new JPanel();
confInfo.setLayout(new FlowLayout());
confInfo.add(la_ConfName);
confInfo.add(la_ConfTopic);
confInfo.setVisible(true);
confInfo.validate();
add(confInfo, BorderLayout.NORTH);
}

private void initButtons() {
bt_roleCallStart = new JButton("Start Session");
bt_roleCallStart.addActionListener(lis_roleCallStart);
// bt_roleCallStart.setVisible(true);

bt_settings = new JButton("Settings");
bt_settings.addActionListener(lis_settings);
// bt_roleCallStart.setVisible(true);

JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
buttons.add(bt_settings);
buttons.add(Box.createVerticalStrut(12));
buttons.add(bt_roleCallStart);
buttons.setVisible(true);
buttons.validate();
add(buttons, BorderLayout.EAST);
}

private void initProductInfo() {
JLabel groupInfo = new JLabel("Product designed by");
JLabel groupPhoto = new JLabel(new ImageIcon(getClass().getResource("/logo/logo.jpg")));

JPanel info = new JPanel();
info.setLayout(new BoxLayout(info, BoxLayout.Y_AXIS));
info.add(groupInfo);
info.add(Box.createVerticalGlue());
info.add(groupPhoto);
info.setVisible(true);
info.validate();
add(info, BorderLayout.WEST);
}
}
...全文
135 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧