JPanels添加到另一个JPanel(在JFrame中)后未显示出来。

huangapple go评论61阅读模式
英文:

JPanels added to another JPanel (in a JFrame) not showing up

问题

// 以下为翻译内容:

// 创建主面板和窗口的代码;
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();

// 主面板代码;
public void init() {
    // 文本区域
    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 = 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();
        }
    });

    // 面板
    topPanel = new JPanel();
    topPanel.setBackground(BACKGROUND_COLOR);
    topPanel.setLayout(new GridLayout(0, 3, 10, 10));

    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);
}
英文:

So I have a custom JPanel with BorderLayout with other JPanels inside. The Main JPanel is added to a JFrame with a null layout. The JPanels inside the Main JPanel are not showing up unless I specify a size even though it is BorderLayout. I tried changing the BorderLayout to other layouts but they still do not show up.

Code creating Main JPanel and 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(&quot;Staines Counter v&quot; + 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();

Main JPanel;

public void init() {
//Text Area
textArea = new JTextArea();
textArea.setFont(new Font(&quot;Dialog&quot;, 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&lt;&gt;();
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(&quot;Copy Text&quot;);
copyButton.setForeground(BUTTON_COLOR_FOREGROUND);
copyButton.setBackground(BUTTON_COLOR_BACKGROUND);
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
copyAction();
}
});
reportButton = new JButton(&quot;Get Report&quot;);
reportButton.setForeground(BUTTON_COLOR_FOREGROUND);
reportButton.setBackground(BUTTON_COLOR_BACKGROUND);
reportButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
reportAction();
}
});
settingsButton = new JButton(&quot;&quot;);
settingsButton.setForeground(BACKGROUND_COLOR);
settingsButton.setBackground(BACKGROUND_COLOR);
settingsButton.setIcon(new ImageIcon(&quot;src/main/resources/settings.png&quot;));
settingsButton.setBounds(80, 0, 35, 35);
settingsButton.setToolTipText(&quot;Settings&quot;);
settingsButton.setBorderPainted(false);
settingsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
settingsAction();
}
});
saveButton = new JButton(&quot;Save&quot;);
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);
}

答案1

得分: 0

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);

这些约束条件无意义因为在执行这些语句时布局管理器尚未设置
英文:
MainPanel mPanel = new MainPanel(tempTheme, phrases);
mPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
mPanel.setLayout(new BorderLayout(0, 0));

You need to set the layout in the constructor of your class BEFORE you add components to the panel.

add(topPanel, BorderLayout.NORTH);
add(middlePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);

Those constraints mean nothing, since at the time the statements are executed the layout manager has not been set.

huangapple
  • 本文由 发表于 2020年10月15日 03:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64360553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定