我希望有人能帮我解决以下问题。我正在创建一个Java桌面应用程序,其中有一个包含两个JPanel的JPanel topicPanel;TopicButton面板包含JButton createEntryButton和topicTabCardsPanel,后者实现CardLayout。单击createEntryButton时,我试图动态创建并添加一个新的JPanel entryPanel,其中包含JTextlabels、JTextFIelds和JButton。topicPanel类中的action performed方法中使用的代码为:
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根本不显示,JButton只在悬停时出现?我也有一个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.
谢谢
看来我已经解决了这个问题。而不是
add(entryPanel);
在将entryPanel添加到topicTab面板时,我应该使用
topicTabCardsPanel.add(entryPanel);
愚蠢的错误!
问题内容: 如何在jpanel中动态添加组件?当我单击按钮时,我应该有添加按钮,组件应该添加到JPanel。 我的问题是,当我单击添加按钮时,将文本字段和按钮添加到jpanel时,用户可以根据需要将它们添加到jpanel中的任意次数单击添加按钮。我已将scrollerpane添加到我的jpanel,并且jpanel布局管理器设置为null。 问题答案: 像往常一样,除了必须调用: 完成后,因为容器
因此,我的问题是面板被插入到JScrollPane中,这些面板的大小正在减小,但没有滚动条。 这是我的代码,我已经播种很远:
问题内容: 我是Java的新手,主要是CardLayout。我只想切换由JPanels表示的“窗口”。我在CardLayout的某处读过这份工作。但我的问题是,当到(这是CardLayout之一),它改变的内容几个像素顶端,从它的中心位置了。我在代码中跳过了它应该在的位置。 我有以下代码: 拜托,我搞砸了吗?谢谢。 问题答案: 由于您要在主JPanel中添加两个JPanel,因此这两个面板都需要容
问题内容: 我有一个和上。 在运行时添加到单击时。 我使用以下代码: 但没有显示任何在。 问题答案: 我看到您创建了一个名为: 但您永远不会将其添加到面板中。相反,您将没有文本的新内容添加到面板中: 当然,这将构造一个不可见的空标签。 添加类似后,还尝试在您的实例上调用和,如下所示: 这样,您可能还需要调用您的框架实例,以便调整大小以适合新组件。 另外,请不要使用/ 布局,这是非常糟糕的做法(除非
问题内容: 我对此有疑问。我有一个JPanel,通常我会像这样创建一个JLabel: 但是我希望每次单击一个按钮时,在该面板中创建一个新的JLabel,它的大小相同,但高度不同。我试过了: 但是这样一来,我就无法设定界限。我从JTextField获得的stringName。 问题答案: 首先,使用layout。正确完成布局后,组件将按照需要放置。其次,在向布局动态添加组件时,您需要告诉布局更新。这
问题内容: 在NetBeans中,我已经使用GUI编辑器制作了一个JFrame,并且在框架中放置了一个JPanel。目前,我正在尝试在类构造时在面板中创建一个新按钮。这是我的代码,但似乎无法正常工作。(第一行显示该按钮,其他行尝试显示该按钮。) 我整夜都在搜寻Google,但似乎无法正常运作。 问题答案: 有时候,您看不到按钮是布局管理器问题(因为您没有为布局管理器设置正确的属性)。您可以通过禁用