[英]JPanels added to another JPanel (in a JFrame) not showing up
所以我有一个带有 BorderLayout 的自定义 JPanel,里面有其他 JPanel。 Main JPanel 被添加到具有空布局的 JFrame。 除非我指定大小,否则 Main JPanel 中的 JPanel 不会显示,即使它是 BorderLayout。 我尝试将 BorderLayout 更改为其他布局,但它们仍然没有显示。
创建主 JPanel 和 JFrame 的代码;
MainPanel mPanel = new MainPanel(tempTheme, phrases);
mPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
mPanel.setLayout(new BorderLayout(0, 0));
JFrame frame = new JFrame("Staines Counter v" + version);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(iconImagePath));
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setSize(new Dimension(1000, 900));
frame.setBackground(tempTheme.getBackground());
frame.setLocationRelativeTo(null);
frame.setFocusable(true);
frame.setContentPane(mPanel);
frame.setLayout(new BorderLayout(0, 0));
frame.setVisible(true);
frame.requestFocus();
主JPanel;
public void init() {
//Text Area
textArea = new JTextArea();
textArea.setFont(new Font("Dialog", Font.PLAIN, 14));
textArea.setRows(20);
textArea.setWrapStyleWord(true);
textArea.setForeground(TEXT_COLOR_FOREGROUND);
textArea.setBackground(TEXT_COLOR_BACKGROUND);
textArea.setEditable(false);
textArea.setLineWrap(true);
//Buttons
buttons = new ArrayList<>();
for (Phrase p : phrases) {
CustomButton temp = new CustomButton(p);
temp.setBackground(BUTTON_COLOR_BACKGROUND);
temp.setForeground(BUTTON_COLOR_FOREGROUND);
temp.addActionListener(new CustomActionListener(this, temp, textArea));
buttons.add(temp);
}
copyButton = new JButton("Copy Text");
copyButton.setForeground(BUTTON_COLOR_FOREGROUND);
copyButton.setBackground(BUTTON_COLOR_BACKGROUND);
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
copyAction();
}
});
reportButton = new JButton("Get Report");
reportButton.setForeground(BUTTON_COLOR_FOREGROUND);
reportButton.setBackground(BUTTON_COLOR_BACKGROUND);
reportButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reportAction();
}
});
settingsButton = new JButton("");
settingsButton.setForeground(BACKGROUND_COLOR);
settingsButton.setBackground(BACKGROUND_COLOR);
settingsButton.setIcon(new ImageIcon("src/main/resources/settings.png"));
settingsButton.setBounds(80, 0, 35, 35);
settingsButton.setToolTipText("Settings");
settingsButton.setBorderPainted(false);
settingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
settingsAction();
}
});
saveButton = new JButton("Save");
saveButton.setForeground(BUTTON_COLOR_FOREGROUND);
saveButton.setBackground(BUTTON_COLOR_BACKGROUND);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveAction();
}
});
//Panels
topPanel = new JPanel();
topPanel.setBackground(BACKGROUND_COLOR);
topPanel.setLayout(new GridLayout(0, 3, 10, 10));
//topPanel.setSize(300, 400);
for (CustomButton b : buttons) {
topPanel.add(b);
}
middlePanel = new JPanel();
middlePanel.setBorder(new CompoundBorder(new EmptyBorder(10, 10, 10, 10),
new LineBorder(BACKGROUND_COLOR)));
middlePanel.setBackground(BACKGROUND_COLOR);
middlePanel.setLayout(new CardLayout(10, 10));
middlePanel.add(textArea);
bottomLeftPanel = new JPanel();
bottomLeftPanel.setBackground(BACKGROUND_COLOR);
bottomLeftPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 0));
bottomLeftPanel.add(copyButton);
bottomLeftPanel.add(reportButton);
bottomLeftPanel.add(saveButton);
bottomRightPanel = new JPanel();
bottomRightPanel.setBackground(BACKGROUND_COLOR);
bottomRightPanel.setLayout(null);
bottomRightPanel.add(settingsButton);
bottomPanel = new JPanel();
bottomPanel.setBackground(BACKGROUND_COLOR);
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
bottomPanel.add(bottomLeftPanel);
bottomPanel.add(bottomRightPanel);
add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
}
MainPanel mPanel = new MainPanel(tempTheme, phrases);
mPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
mPanel.setLayout(new BorderLayout(0, 0));
在向面板添加组件之前,您需要在类的构造函数中设置布局。
add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
这些约束没有任何意义,因为在执行语句时还没有设置布局管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.