我在NetBeans中使用Java GUI构建器(拖放),并在容器中添加了JLabels,Text Fields和JLabels。但是,只有Text Fields和JLabels显示,而不是JLabels。
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JPanel.java to edit this template
*/
package View;
/**
*
* @author Yash
*/
public class ModifyTransactionPanel extends javax.swing.JPanel {
private MainFrame mainFrame;
/**
* Creates new form HomePanel
*/
public ModifyTransactionPanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
initComponents();
}
/**
* 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() {
menuPanel = new javax.swing.JPanel();
menuItems = new javax.swing.JPanel();
backButton = new javax.swing.JButton();
logoutButton = new javax.swing.JButton();
exitButton = new javax.swing.JButton();
expenseTrackerLogoContainer = new javax.swing.JPanel();
expenseTrackerLogo = new javax.swing.JLabel();
modifyTransactionPanelContainer = new javax.swing.JPanel();
addIncomeTitleLabel = new javax.swing.JLabel();
searchBar = new javax.swing.JTextField();
searchButton = new java.awt.Button();
searchTransactionIDButton = new java.awt.Label();
searchResultContainer = new javax.swing.JPanel();
resultTransactionID = new java.awt.Label();
resultTransactionTypeLabel = new java.awt.Label();
resultTransactionIDLabel = new java.awt.Label();
resultNameLabel = new java.awt.Label();
resultName = new javax.swing.JTextField();
resultDateLabel = new java.awt.Label();
resultDate = new javax.swing.JTextField();
resultCategoryLabel = new java.awt.Label();
resultCategory = new javax.swing.JTextField();
resultAmountLabel = new java.awt.Label();
resultAmount = new javax.swing.JTextField();
updateTransactionButton = new java.awt.Button();
addIncomeButton1 = new java.awt.Button();
setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
menuPanel.setBackground(new java.awt.Color(0, 0, 0));
menuPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
menuItems.setBackground(new java.awt.Color(0, 0, 0));
menuItems.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
menuItems.setLayout(new java.awt.GridLayout(3, 0));
backButton.setBackground(new java.awt.Color(0, 0, 0));
backButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
backButton.setForeground(new java.awt.Color(255, 255, 255));
backButton.setText("Back");
backButton.setBorder(null);
backButton.setBorderPainted(false);
backButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
menuItems.add(backButton);
logoutButton.setBackground(new java.awt.Color(0, 0, 0));
logoutButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
logoutButton.setForeground(new java.awt.Color(255, 255, 255));
logoutButton.setText("Logout");
logoutButton.setBorder(null);
logoutButton.setBorderPainted(false);
logoutButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
menuItems.add(logoutButton);
exitButton.setBackground(new java.awt.Color(0, 0, 0));
exitButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
exitButton.setForeground(new java.awt.Color(255, 255, 255));
exitButton.setText("Exit");
exitButton.setBorder(null);
exitButton.setBorderPainted(false);
exitButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
menuItems.add(exitButton);
menuPanel.add(menuItems, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 270, 370, 730));
expenseTrackerLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/expenseTrackerLogo.jpg"))); // NOI18N
expenseTrackerLogo.setOpaque(true);
javax.swing.GroupLayout expenseTrackerLogoContainerLayout = new javax.swing.GroupLayout(expenseTrackerLogoContainer);
expenseTrackerLogoContainer.setLayout(expenseTrackerLogoContainerLayout);
expenseTrackerLogoContainerLayout.setHorizontalGroup(
expenseTrackerLogoContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(expenseTrackerLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
expenseTrackerLogoContainerLayout.setVerticalGroup(
expenseTrackerLogoContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(expenseTrackerLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
menuPanel.add(expenseTrackerLogoContainer, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 370, 270));
add(menuPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 370, 1000));
modifyTransactionPanelContainer.setBackground(new java.awt.Color(255, 255, 255));
modifyTransactionPanelContainer.setForeground(new java.awt.Color(0, 0, 0));
addIncomeTitleLabel.setBackground(new java.awt.Color(255, 255, 255));
addIncomeTitleLabel.setFont(new java.awt.Font("Arial", 1, 36)); // NOI18N
addIncomeTitleLabel.setForeground(new java.awt.Color(0, 0, 0));
addIncomeTitleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
addIncomeTitleLabel.setText("ModifyTransaction");
searchBar.setBackground(new java.awt.Color(255, 255, 255));
searchBar.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
searchButton.setBackground(new java.awt.Color(0, 0, 0));
searchButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
searchButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
searchButton.setForeground(new java.awt.Color(255, 255, 255));
searchButton.setLabel("Search");
searchButton.setName(""); // NOI18N
searchButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
searchButtonActionPerformed(evt);
}
});
searchTransactionIDButton.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
searchTransactionIDButton.setText("Search Transaction ID");
searchResultContainer.setBackground(new java.awt.Color(255, 255, 255));
searchResultContainer.setForeground(new java.awt.Color(255, 255, 255));
resultTransactionID.setAlignment(java.awt.Label.CENTER);
resultTransactionID.setFont(new java.awt.Font("Arial", 1, 20)); // NOI18N
resultTransactionID.setText("INC000001");
resultTransactionTypeLabel.setFont(new java.awt.Font("Arial", 1, 20)); // NOI18N
resultTransactionTypeLabel.setText("(Income)");
resultTransactionIDLabel.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
resultTransactionIDLabel.setText("Transaction ID:");
resultNameLabel.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
resultNameLabel.setText("Income Name");
resultName.setBackground(new java.awt.Color(255, 255, 255));
resultName.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
resultDateLabel.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
resultDateLabel.setText("Date (in dd/mm/yyyy format)");
resultDate.setBackground(new java.awt.Color(255, 255, 255));
resultDate.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
resultCategoryLabel.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
resultCategoryLabel.setText("Category");
resultCategory.setBackground(new java.awt.Color(255, 255, 255));
resultCategory.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
resultAmountLabel.setFont(new java.awt.Font("Arial", 0, 20)); // NOI18N
resultAmountLabel.setText("Amount");
resultAmount.setBackground(new java.awt.Color(255, 255, 255));
resultAmount.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
updateTransactionButton.setBackground(new java.awt.Color(0, 0, 0));
updateTransactionButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
updateTransactionButton.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
updateTransactionButton.setForeground(new java.awt.Color(255, 255, 255));
updateTransactionButton.setLabel("Update");
updateTransactionButton.setName(""); // NOI18N
updateTransactionButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateTransactionButtonActionPerformed(evt);
}
});
addIncomeButton1.setBackground(new java.awt.Color(0, 0, 0));
addIncomeButton1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
addIncomeButton1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
addIncomeButton1.setForeground(new java.awt.Color(255, 255, 255));
addIncomeButton1.setLabel("Delete");
addIncomeButton1.setName(""); // NOI18N
addIncomeButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addIncomeButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout searchResultContainerLayout = new javax.swing.GroupLayout(searchResultContainer);
searchResultContainer.setLayout(searchResultContainerLayout);
searchResultContainerLayout.setHorizontalGroup(
searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchResultContainerLayout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(updateTransactionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addIncomeButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(resultTransactionTypeLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(43, 43, 43))
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchResultContainerLayout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(searchResultContainerLayout.createSequentialGroup()
.addComponent(resultTransactionIDLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resultTransactionID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(resultAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultAmountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultCategory, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultCategoryLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultDate, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultDateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultName, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
.addComponent(resultNameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
.addContainerGap(47, Short.MAX_VALUE)))
);
searchResultContainerLayout.setVerticalGroup(
searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchResultContainerLayout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(resultTransactionTypeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(441, 441, 441)
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(updateTransactionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addIncomeButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30))
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(searchResultContainerLayout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(searchResultContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(resultTransactionIDLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resultTransactionID, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(28, 28, 28)
.addComponent(resultNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resultName, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(resultDateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resultDate, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
.addGap(24, 24, 24)
.addComponent(resultCategoryLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resultCategory, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
.addGap(23, 23, 23)
.addComponent(resultAmountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(resultAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
.addGap(121, 121, 121)))
);
javax.swing.GroupLayout modifyTransactionPanelContainerLayout = new javax.swing.GroupLayout(modifyTransactionPanelContainer);
modifyTransactionPanelContainer.setLayout(modifyTransactionPanelContainerLayout);
modifyTransactionPanelContainerLayout.setHorizontalGroup(
modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(modifyTransactionPanelContainerLayout.createSequentialGroup()
.addContainerGap(374, Short.MAX_VALUE)
.addGroup(modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modifyTransactionPanelContainerLayout.createSequentialGroup()
.addGroup(modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(searchTransactionIDButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modifyTransactionPanelContainerLayout.createSequentialGroup()
.addComponent(searchBar, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(24, 24, 24)
.addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(350, 350, 350))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modifyTransactionPanelContainerLayout.createSequentialGroup()
.addComponent(addIncomeTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(374, 374, 374))))
.addGroup(modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modifyTransactionPanelContainerLayout.createSequentialGroup()
.addContainerGap(327, Short.MAX_VALUE)
.addComponent(searchResultContainer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(304, Short.MAX_VALUE)))
);
modifyTransactionPanelContainerLayout.setVerticalGroup(
modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(modifyTransactionPanelContainerLayout.createSequentialGroup()
.addGap(90, 90, 90)
.addComponent(addIncomeTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(37, 37, 37)
.addComponent(searchTransactionIDButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(searchBar, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
.addComponent(searchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(713, 713, 713))
.addGroup(modifyTransactionPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, modifyTransactionPanelContainerLayout.createSequentialGroup()
.addContainerGap(309, Short.MAX_VALUE)
.addComponent(searchResultContainer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(130, Short.MAX_VALUE)))
);
add(modifyTransactionPanelContainer, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 0, 1130, 1000));
}// </editor-fold>
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
private void updateTransactionButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mainFrame.showTransactionPanel();
}
private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void addIncomeButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private java.awt.Button addIncomeButton1;
private javax.swing.JLabel addIncomeTitleLabel;
private javax.swing.JButton backButton;
private javax.swing.JButton exitButton;
private javax.swing.JLabel expenseTrackerLogo;
private javax.swing.JPanel expenseTrackerLogoContainer;
private javax.swing.JButton logoutButton;
private javax.swing.JPanel menuItems;
private javax.swing.JPanel menuPanel;
private javax.swing.JPanel modifyTransactionPanelContainer;
private javax.swing.JTextField resultAmount;
private java.awt.Label resultAmountLabel;
private javax.swing.JTextField resultCategory;
private java.awt.Label resultCategoryLabel;
private javax.swing.JTextField resultDate;
private java.awt.Label resultDateLabel;
private javax.swing.JTextField resultName;
private java.awt.Label resultNameLabel;
private java.awt.Label resultTransactionID;
private java.awt.Label resultTransactionIDLabel;
private java.awt.Label resultTransactionTypeLabel;
private javax.swing.JTextField searchBar;
private java.awt.Button searchButton;
private javax.swing.JPanel searchResultContainer;
private java.awt.Label searchTransactionIDButton;
private java.awt.Button updateTransactionButton;
// End of variables declaration
}
字符串
Here is the design:
Here is what it looks like when i run it:的
我试着检查所有事情的可信度是否正确,结果是正确的。
1条答案
按热度按时间avwztpqn1#
您不仅要添加JLabels;还要添加
java.awt.Label
组件:字符串
不要这样做。不要将轻量级AWT组件(如Labels)与轻量级Swing GUI混合使用。相反,每个
java.awt.Label
都应该声明并初始化为javax.swing.JLabel
。巴顿也是如此。
型
和
型
你应该做:
型
和
型
请注意,可以使用非组件
java.awt
类,例如用于字体、颜色、布局管理器等的类。这可能是因为您在NetBeans GUI构建器中选择了错误的组件,因此必须更加小心。或者更好的是,学习在不使用GUI构建器的情况下创建GUI。