gpt4 book ai didi

java - JPanel 从窗口消失

转载 作者:行者123 更新时间:2023-11-29 03:36:49 26 4
gpt4 key购买 nike

下面的SSCCE 是一个扩展JPanel 的类。 JPanel 是日历的基本轮廓(为简单起见,我将其简化),它由 JButton 组件、一个 JLabel 和一个 JTable。当我将此框架添加到窗口(即 JDialog)时,它显示正常。但是,当我用它添加另一个组件时,它就消失了。为什么会发生这种情况,我怎样才能避免这种情况发生?

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class CalendarPanel extends JPanel {

private static JDialog dialog = new JDialog();

public static void main(String[] args) {

setDialogProperties();
addComponentsToDialog();
dialog.setVisible(true);
}

private static void setDialogProperties() {
dialog.setModal(true);
dialog.setResizable(false);
dialog.setSize(new Dimension(330, 400));
dialog.setDefaultCloseOperation(2);
dialog.setLocationRelativeTo(null);
}

private static void addComponentsToDialog() {
CalendarPanel calendar = new CalendarPanel();
JPanel panel = new JPanel();
panel.add(calendar);

dialog.add(panel);
//dialog.add(new JLabel("Hello"));
}

private static final long serialVersionUID = 1L;
private JLabel lblMonth;
private JButton btnPrev, btnNext;
private JTable tblCalendar;
private DefaultTableModel mtblCalendar;
private JScrollPane stblCalendar;
private static GridBagLayout gridBag = new GridBagLayout();
private GridBagConstraints constraints = new GridBagConstraints();

public CalendarPanel() {

super(gridBag);

createControls();
addControlsToPanel();
addHeaders();
setTableProperties();
}

private void createControls() {
lblMonth = new JLabel("January");
btnPrev = new JButton("<");
btnNext = new JButton(">");
mtblCalendar = new DefaultTableModel() {

public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
};
tblCalendar = new JTable(mtblCalendar);
stblCalendar = new JScrollPane(tblCalendar);
stblCalendar.setPreferredSize(new Dimension(300, 247));
}

private void addControlsToPanel() {
GridBagLayout topGridBag = new GridBagLayout();

JPanel topPanel = new JPanel(topGridBag);

JPanel labelPanel = new JPanel();
labelPanel.add(lblMonth);
labelPanel.setPreferredSize(new Dimension(180, 20));

constraints.gridx = 1;
topGridBag.setConstraints(labelPanel, constraints);

constraints.gridx = 2;
topGridBag.setConstraints(btnNext, constraints);

topPanel.add(btnPrev);
topPanel.add(labelPanel);
topPanel.add(btnNext);

gridBag.setConstraints(topPanel, constraints);

constraints.gridy = 1;
gridBag.setConstraints(stblCalendar, constraints);

this.add(topPanel);
this.add(stblCalendar);
}

private void addHeaders() {
String[] headers = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
for (int i = 0; i < 7; i++) {
mtblCalendar.addColumn(headers[i]);
}
}

private void setTableProperties() {
tblCalendar.getTableHeader().setReorderingAllowed(false);

tblCalendar.setRowHeight(38);
mtblCalendar.setColumnCount(7);
mtblCalendar.setRowCount(6);
}
}

最佳答案

默认情况下,JDialogs 和所有顶层窗口都使用 BorderLayout。当你在没有指定位置的情况下向它添加一个组件(实际上是它的 contentPane)时,你默认将它添加到 BorderLayout.CENTER 位置。每当您添加一个新组件时,您都会覆盖之前添加的组件。您需要了解可供您使用的布局,然后充分利用它们。

例如,

  dialog.add(panel);
dialog.add(new JLabel("Hello", SwingConstants.CENTER), BorderLayout.SOUTH);
}

接下来您要避免设置任何大小,并确保 pack() 您的顶层窗口允许这样做。

关于java - JPanel 从窗口消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com