我希望有人能够帮助我解决以下问题。我正在创建一个 Java 桌面应用程序,其中有一个 JPanel topicPanel,其中包含两个 JPanel;topicButtonsPanel 包含 JButton createEntryButton 和实现 CardLayout 的 topicTabCardsPanel。单击 createEntryButton 时,我试图动态创建并添加一个新的 JPanel entryPanel,其中包含 JTextlabels、JTextFIelds 和 JButtons。在 topicPanel 类中执行的操作方法中使用的代码是:
private void createEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {
EntryPanel entryPanel = new EntryPanel();
add(entryPanel);
CardLayout cards = (CardLayout) topicTabCardsPanel.getLayout();
cards.addLayoutComponent(entryPanel, "entryPanelCard");
cards.show(topicTabCardsPanel, "entryPanelCard");
}
但是,尽管 entryPanel 已添加到 topicPanel,但 topicPanel 中的组件并未正确显示。只有 JTextFields 似乎正确显示,JLabels 根本不显示,JButtons 只在悬停时出现?我还有一个 JTextArea,当鼠标悬停在上面时,除了滚动条之外不显示。
以下是 topicPanel 和 entryPanel 类的代码:
import java.awt.CardLayout;
public class TopicTab extends javax.swing.JPanel {
private ProjectPanel projectPanel;
/**
* Creates new form TopicTab
*/
public TopicTab(ProjectPanel projectPanel) {
initComponents();
this.projectPanel = projectPanel;
}
public TopicTab() {
initComponents();
}
public static void main(String args[]) {
TopicTab topicTab = new TopicTab();
topicTab.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
topicButtonsPanel = new javax.swing.JPanel();
createEntryButton = new javax.swing.JButton();
importEntryButton = new javax.swing.JButton();
topicTabCardsPanel = new javax.swing.JPanel();
timelinePanel = new Timeline.TimelinePanel();
setMinimumSize(new java.awt.Dimension(1275, 672));
setPreferredSize(new java.awt.Dimension(1275, 672));
setLayout(new java.awt.BorderLayout());
topicButtonsPanel.setBackground(new java.awt.Color(255, 255, 255));
createEntryButton.setText("Create Entry");
createEntryButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createEntryButtonActionPerformed(evt);
}
});
importEntryButton.setText("Import Entry");
javax.swing.GroupLayout topicButtonsPanelLayout = new javax.swing.GroupLayout(topicButtonsPanel);
topicButtonsPanel.setLayout(topicButtonsPanelLayout);
topicButtonsPanelLayout.setHorizontalGroup(
topicButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(topicButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(createEntryButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(importEntryButton)
.addContainerGap(1071, Short.MAX_VALUE))
);
topicButtonsPanelLayout.setVerticalGroup(
topicButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(topicButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(topicButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(createEntryButton)
.addComponent(importEntryButton))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
add(topicButtonsPanel, java.awt.BorderLayout.PAGE_START);
topicTabCardsPanel.setLayout(new java.awt.CardLayout());
javax.swing.GroupLayout timelinePanelLayout = new javax.swing.GroupLayout(timelinePanel);
timelinePanel.setLayout(timelinePanelLayout);
timelinePanelLayout.setHorizontalGroup(
timelinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1275, Short.MAX_VALUE)
);
timelinePanelLayout.setVerticalGroup(
timelinePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 627, Short.MAX_VALUE)
);
topicTabCardsPanel.add(timelinePanel, "card2");
add(topicTabCardsPanel, java.awt.BorderLayout.CENTER);
}// </editor-fold>
private void createEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {
EntryPanel entryPanel = new EntryPanel();
add(entryPanel);
CardLayout cards = (CardLayout) topicTabCardsPanel.getLayout();
cards.addLayoutComponent(entryPanel, "entryPanelCard");
cards.show(topicTabCardsPanel, "entryPanelCard");
}
// Variables declaration - do not modify
private javax.swing.JButton createEntryButton;
private javax.swing.JButton importEntryButton;
private Timeline.TimelinePanel timelinePanel;
private javax.swing.JPanel topicButtonsPanel;
private javax.swing.JPanel topicTabCardsPanel;
// End of variables declaration
}
public class EntryPanel extends javax.swing.JPanel {
/**
* Creates new form EntryViewPanel
*/
public EntryPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialise the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
createEntryDetails = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
entryTitleLabel = new javax.swing.JLabel();
entryDateLabel = new javax.swing.JLabel();
entryNotesLabel = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
entryNotesTextArea = new javax.swing.JTextArea();
entryDayTextField = new javax.swing.JTextField();
entryTitleTextField = new javax.swing.JTextField();
entryMonthTextField = new javax.swing.JTextField();
entryYearTextField = new javax.swing.JTextField();
newEntryDateBackslashLabel = new javax.swing.JLabel();
newEntryDateBackslashLabel1 = new javax.swing.JLabel();
sourceDetailsTabbedPane = new javax.swing.JTabbedPane();
addSourcePanel = new javax.swing.JPanel();
addSourceLabel = new javax.swing.JLabel();
buttonsPanel = new javax.swing.JPanel();
createEntryButtonsPanel = new javax.swing.JPanel();
createEntryButton = new javax.swing.JButton();
backButton = new javax.swing.JButton();
updateEntryButtonsPanel = new javax.swing.JPanel();
backButton1 = new javax.swing.JButton();
updateEntryButton = new javax.swing.JButton();
setMaximumSize(new java.awt.Dimension(1045, 591));
setMinimumSize(new java.awt.Dimension(1275, 672));
setPreferredSize(new java.awt.Dimension(1275, 672));
setLayout(new java.awt.BorderLayout());
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel2.setText("Enter new entry details:");
entryTitleLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
entryTitleLabel.setText("Title:");
entryDateLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
entryDateLabel.setText("Date (dd/mm/yy):");
entryNotesLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
entryNotesLabel.setText("Notes:");
entryNotesTextArea.setColumns(20);
entryNotesTextArea.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
entryNotesTextArea.setRows(5);
jScrollPane2.setViewportView(entryNotesTextArea);
entryDayTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
entryTitleTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
entryMonthTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
entryYearTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
newEntryDateBackslashLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
newEntryDateBackslashLabel.setText("/");
newEntryDateBackslashLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
newEntryDateBackslashLabel1.setText("/");
javax.swing.GroupLayout createEntryDetailsLayout = new javax.swing.GroupLayout(createEntryDetails);
createEntryDetails.setLayout(createEntryDetailsLayout);
createEntryDetailsLayout.setHorizontalGroup(
createEntryDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(createEntryDetailsLayout.createSequentialGroup()
.addContainerGap()
.addGroup(createEntryDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(createEntryDetailsLayout.createSequentialGroup()
.addComponent(entryTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(69, 69, 69)
.addComponent(entryTitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(54, 54, 54)
.addComponent(entryDateLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(entryDayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(newEntryDateBackslashLabel)
.addGap(9, 9, 9)
.addComponent(entryMonthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(newEntryDateBackslashLabel1)
.addGap(10, 10, 10)
.addComponent(entryYearTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(createEntryDetailsLayout.createSequentialGroup()
.addGap(118, 118, 118)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 348, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(entryNotesLabel))
.addContainerGap(437, Short.MAX_VALUE))
);
createEntryDetailsLayout.setVerticalGroup(
createEntryDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(createEntryDetailsLayout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(createEntryDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(entryTitleLabel)
.addComponent(entryTitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entryDateLabel)
.addComponent(entryDayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entryMonthTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entryYearTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(newEntryDateBackslashLabel)
.addComponent(newEntryDateBackslashLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(createEntryDetailsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(entryNotesLabel)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 9, Short.MAX_VALUE))
);
add(createEntryDetails, java.awt.BorderLayout.PAGE_START);
addSourceLabel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
addSourceLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
addSourceLabel.setText("Click here to add a new Source");
addSourceLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout addSourcePanelLayout = new javax.swing.GroupLayout(addSourcePanel);
addSourcePanel.setLayout(addSourcePanelLayout);
addSourcePanelLayout.setHorizontalGroup(
addSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(addSourcePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(addSourceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 1250, Short.MAX_VALUE)
.addContainerGap())
);
addSourcePanelLayout.setVerticalGroup(
addSourcePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(addSourcePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(addSourceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 437, Short.MAX_VALUE)
.addContainerGap())
);
addSourceLabel.getAccessibleContext().setAccessibleName("addSourcePanel");
sourceDetailsTabbedPane.addTab("add source", addSourcePanel);
add(sourceDetailsTabbedPane, java.awt.BorderLayout.CENTER);
sourceDetailsTabbedPane.getAccessibleContext().setAccessibleDescription("sourceDetailsTabbedPane");
buttonsPanel.setMaximumSize(new java.awt.Dimension(1045, 68));
buttonsPanel.setLayout(new java.awt.CardLayout());
createEntryButtonsPanel.setMaximumSize(new java.awt.Dimension(1045, 68));
createEntryButtonsPanel.setName("createEntryButtonsPanel");
createEntryButtonsPanel.setPreferredSize(new java.awt.Dimension(1045, 68));
createEntryButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
createEntryButton.setText("Create");
backButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
backButton.setText("Back");
javax.swing.GroupLayout createEntryButtonsPanelLayout = new javax.swing.GroupLayout(createEntryButtonsPanel);
createEntryButtonsPanel.setLayout(createEntryButtonsPanelLayout);
createEntryButtonsPanelLayout.setHorizontalGroup(
createEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(createEntryButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(createEntryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(971, Short.MAX_VALUE))
);
createEntryButtonsPanelLayout.setVerticalGroup(
createEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(createEntryButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(createEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(createEntryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(16, Short.MAX_VALUE))
);
buttonsPanel.add(createEntryButtonsPanel, "createEntryButtonsPanel");
updateEntryButtonsPanel.setMaximumSize(new java.awt.Dimension(1045, 68));
updateEntryButtonsPanel.setMinimumSize(new java.awt.Dimension(1045, 68));
updateEntryButtonsPanel.setName("updateEntryButtonsPanel");
updateEntryButtonsPanel.setPreferredSize(new java.awt.Dimension(1045, 68));
backButton1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
backButton1.setText("Back");
updateEntryButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
updateEntryButton.setText("Update");
javax.swing.GroupLayout updateEntryButtonsPanelLayout = new javax.swing.GroupLayout(updateEntryButtonsPanel);
updateEntryButtonsPanel.setLayout(updateEntryButtonsPanelLayout);
updateEntryButtonsPanelLayout.setHorizontalGroup(
updateEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(updateEntryButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(backButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(updateEntryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(971, Short.MAX_VALUE))
);
updateEntryButtonsPanelLayout.setVerticalGroup(
updateEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(updateEntryButtonsPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(updateEntryButtonsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(backButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(updateEntryButton, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(16, Short.MAX_VALUE))
);
buttonsPanel.add(updateEntryButtonsPanel, "updateEntryButtonsPanel");
add(buttonsPanel, java.awt.BorderLayout.PAGE_END);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JLabel addSourceLabel;
private javax.swing.JPanel addSourcePanel;
private javax.swing.JButton backButton;
private javax.swing.JButton backButton1;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JButton createEntryButton;
private javax.swing.JPanel createEntryButtonsPanel;
private javax.swing.JPanel createEntryDetails;
private javax.swing.JLabel entryDateLabel;
private javax.swing.JTextField entryDayTextField;
private javax.swing.JTextField entryMonthTextField;
private javax.swing.JLabel entryNotesLabel;
private javax.swing.JTextArea entryNotesTextArea;
private javax.swing.JLabel entryTitleLabel;
private javax.swing.JTextField entryTitleTextField;
private javax.swing.JTextField entryYearTextField;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JLabel newEntryDateBackslashLabel;
private javax.swing.JLabel newEntryDateBackslashLabel1;
private javax.swing.JTabbedPane sourceDetailsTabbedPane;
private javax.swing.JButton updateEntryButton;
private javax.swing.JPanel updateEntryButtonsPanel;
// End of variables declaration
}
我正在使用 NetBeans IDE 7.1.2 和 Java:1.7.0_04。
谢谢