gpt4 book ai didi

java - 刷新 JPanel 并切换 JTabbedPane 操作Perfmored

转载 作者:行者123 更新时间:2023-12-02 00:03:59 25 4
gpt4 key购买 nike

 private void buttonAddJobActionPerformed(java.awt.event.ActionEvent evt) {                                             

try {

retrieveID();
String sqlStm = "INSERT INTO Job (employerID,title,description,type,salary,benefits,vacancies,closing,requirement,placement,applyTo,status,posted,location) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";


pst = conn.prepareStatement(sqlStm);
pst.setInt(1,id);
pst.setString(2,txtTitle.getText());
pst.setString(3,areaDescription.getText());
pst.setString(4,comboType.getSelectedItem().toString());
pst.setString(5,txtSalary.getText());
pst.setString(6,areaBenefits.getText());
pst.setString(7,txtVac.getText());
Date close;
close = txtDate.getDate();
pst.setString(8,sdf.format(close));
pst.setString(9,areaReq.getText());
pst.setString(10,comboPlace.getSelectedItem().toString());
pst.setString(11,txtWeb.getText());
pst.setString(12,comboStatus.getSelectedItem().toString());
Date now = new Date();
pst.setString(13,sdf.format(now));
pst.setString(14,txtLoc.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"You have successfully added a job");
//empty all JTextfields
//switch to another

我正在尝试清空 JPanel 中的一组 JTextFields,但我可以刷新面板,而不是逐一清空它们吗?如果是这样,你该怎么做?我尝试了 repaint()revalidate() 这些不起作用。也许我错了。我还想将 JTabbedPane 切换到另一个 Pane ,但是当我尝试使用此方法时,这不起作用...

JTabbedPane sourceTabbedPane = (JTabbedPane) evt.getSource();
sourceTabbedPane.setSelectedIndex(0);

有人可以展示如何执行此操作的示例代码吗?

最佳答案

您可以循环遍历面板中包含的所有组件,如果它们是文本组件,则清除它们的值。代码会是这样的:

private void clearTextFields(Container container)
{
int count = container.getComponentCount();
for (int i = 0; i < count; i++)
{
Component component = container.getComponent(i);
if (component instanceof Container) {
clearTextFields((Container) component);
}
else if (component instanceof JTextComponent) {
((JTextComponent) component).setText("");
}
}
}

此方法以递归方式工作,并处理当您的面板包含另一个包含文本字段的面板时的情况。

关于java - 刷新 JPanel 并切换 JTabbedPane 操作Perfmored,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14257110/

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