添加到另一个JPanel(在JFrame中)的JPanel没有出现



所以我有一个带有BorderLayout的自定义JPanel,里面有其他JPanel。Main JPanel被添加到具有null布局的JFrame中。主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);

这些约束毫无意义,因为在执行语句时,还没有设置布局管理器。

最新更新