簡體   English   中英

添加到另一個 JPanel(在 JFrame 中)的 JPanel 未顯示

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM