Swing 在JButton Actionlistener中重置JTextfield
我正在使用CardLayout开发一个预算应用程序。下面的代码创建了一个JPanel主面板,其中包含一个名为homePageCard的主页JPanel卡和另一个名为addItemCard的JPanel卡。当我单击名为Add Item的JButton AddItem按钮时,它会成功地将我带到addItemCard 我还插入了一个名为previous to my addItemCard的按钮,将我带回homePageCard,为用户添加了一个saveBudgetItem按钮以保存其预算项目数据,并在addItemCard中添加了两个JTextFields:budget_item_Title_字段和budget_Amount_字段。如果用户试图通过单击JButton saveBudgetItemButton来保存预算项目,而不填写这两个文本字段中的任何一个,或者如果这两个字段中的任何一个包含空字符串,则用户应收到一个JOptionPane错误消息对话框。如果保存成功,另一个JOptionPane对话框应告诉用户预算项目已成功保存 问题的症结在于:当我点击previous进入homepageCard并清除previous按钮的操作侦听器中的文本字段,然后按Add Item按钮返回addItemCard时,它通过使用budget_Item_Title_Field.setText和budget_Amount_Field.setText成功地清除了这两个文本字段 但是,当我为两个文本字段键入新文本并单击saveBudgetItem JButton时,我错误地得到下面详细说明的错误消息,即JOptionPane.showmessagedialog消息,并抱怨当两个字段都不为空时,两个文本字段中仍有空字符串 如何使我在单击“上一步”按钮并在各自的文本字段中键入新预算项目的标题和金额后返回addItemCard时,程序识别更新的文本字段不再包含空字符串 这是我的主要课程代码:Swing 在JButton Actionlistener中重置JTextfield,swing,user-interface,jbutton,actionlistener,jtextfield,Swing,User Interface,Jbutton,Actionlistener,Jtextfield,我正在使用CardLayout开发一个预算应用程序。下面的代码创建了一个JPanel主面板,其中包含一个名为homePageCard的主页JPanel卡和另一个名为addItemCard的JPanel卡。当我单击名为Add Item的JButton AddItem按钮时,它会成功地将我带到addItemCard 我还插入了一个名为previous to my addItemCard的按钮,将我带回homePageCard,为用户添加了一个saveBudgetItem按钮以保存其预算项目数据,并在
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Create main GUI to display and to use software, and add contents to it
HomePage h = new HomePage();
}});
}
}
这是我的主页类代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class HomePage extends JFrame {
// Panel to contain contents of JFrame
private static JPanel contentPanel = new JPanel(new BorderLayout());
private static Dimension screenDimensions = Toolkit.getDefaultToolkit().getScreenSize();
private static int screenWidth = screenDimensions.width;
private static int screenHeight = screenDimensions.height;
private static int frameWidth = 800;
private static int frameHeight = 510;
// the following are components for card layout and widgets for entering budget data
private static JPanel main_panel = new JPanel(new CardLayout());
private static JPanel homeCard = new JPanel(new BorderLayout());
private static JPanel addItemCard = new JPanel(new BorderLayout());
private static JPanel textfieldPanel = new JPanel(new FlowLayout());
private static JTextField budget_Title_Field;
private static JTextField amount_Field;
private static JButton previous = new JButton("<< previous");
private static JButton addItemButton = new JButton("Add Revenue or Expense");
private static JButton saveBudgetItemButton = new JButton("Save Budget Item");
public HomePage() {
// format frame
setSize(frameWidth, frameHeight);
setResizable(false);
setLocation((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// format home page and add all cards to main_panel to manipulate them in card layout
formatHomeCard(homeCard);
// add all cards to main_panel
main_panel.add(homeCard, "Home");
main_panel.add(addItemCard, "Add Item");
// add main_panel to home JFrame
this.add(main_panel);
}
public static void formatHomeCard(JPanel homeCard)
{
// Add functionality to addItemButton so it switches the card to the Add Item page
addItemButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Adds all of the contents of the add revenue or expense page to the
// addItemCard
formatAddItemCard(addItemCard);
// If the add item button is pressed, switch the JPanel so that the add item card
// shows
CardLayout cardLayout = (CardLayout) main_panel.getLayout();
cardLayout.show(main_panel, "Add Item");
}
});
// Add the addItemButton to the home page
homeCard.add(addItemButton, BorderLayout.CENTER);
}
// the purpose of this method is to add all contents to addItemCard
public static void formatAddItemCard(JPanel addItemCard)
{
// add 2 jtextfields, one for the budget title and the other for the budget amount
// I excluded irrelevant JLabels, etc. for the sake of avoiding unnecessary details
textfieldPanel.add(budget_Title_Field);
textfieldPanel.add(amount_Field);
addItemCard.add(textfieldPanel, BorderLayout.NORTH);
// add functionality to save budget item button and add it to addItemCard
saveBudgetItemButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// If the data fields are complete, create and serialize the budget item. Else,
// prompt the user for more information.
if (!(budget_Title_Field.getText().equals("")) && !(amount_Field.getText().equals("")))
{
// will save the budget data here. confirm the user saved the budget item.
JOptionPane.showMessageDialog(null, "Success! Your budget item was saved and has been integrated into your budget.");
}
// if either budget item title or amount is missing, complain and don't save budget item
if (budget_Title_Field.getText().equals("") || amount_Field.getText().equals(""))
{
// PROBLEMATIC CODE HERE: INCORRECTLY ASSUMES TEXTFIELDS ARE EMPTY WHEN THEY'RE
// NOT AFTER CLICKING ON "PREVIOUS" BUTTON, THEN RETURNING TO THIS PAGE BY
// CLICKING "ADD REVENUE OR EXPENSE" BUTTON
// print out error message that budget item title and amount is incomplete
JOptionPane.showMessageDialog(null, "Please enter in both the budget item title and dollar amount to continue.");
}
}
});
addItemCard.add(saveBudgetItemButton, BorderLayout.CENTER);
// Add functionality to previous button and add it to the addItemCard
previous.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// If the previous button is pressed, switch the JPanel so that the home page card shows
CardLayout cardLayout = (CardLayout) main_panel.getLayout();
cardLayout.show(main_panel, "Home");
budget_Title_Field.setText("");
amount_Field.setText("");
isExpenseButton.setSelected(true);
}
});
addItemCard.add(previous, BorderLayout.SOUTH);
}
}
如果我进行修改以使示例程序运行,它将按预期工作。您是否可能在真实代码中的某个地方创建新的文本字段,以便检查错误的字段?我在类的顶端初始化了它们,而不是在其他地方。