使用JavaSwing的JDialog后,如何更新JFrame main的值



我有一个名为MainFrame的主窗口,它是一个jForm,我根据计时器更新数据,但问题是,在使用jdialog后,我无法更新同一MainFrame中的数据,因为我最终创建了另一个重复的窗口,但数据发生了更改,一个是原始计时器,另一个是新计时器,我知道我可以用dispose()关闭第一个窗口,然后保留第二个窗口,但我想避免过多地更改窗口

按下jDialog按钮时创建另一个窗口的代码如下

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String textoFieldTimer = jTextField1.getText();
int timeUserConfig = Integer.parseInt(textoFieldTimer);

Timer timeDefault = new Timer(timeUserConfig, null);

TokenAccess token = new TokenAccess();
token.access_code = code;
MainFrame mainFrame = new MainFrame(token);
mainFrame.setVisible(true);
mainFrame.timeDefault.stop();
mainFrame.timeDefault = timeDefault;

mainFrame.setUpdateTime(timeUserConfig);
this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

有什么替代方案可以更新窗口吗?比如mainFrame.update();,或者可能将jDialog中的jTextField的值发送到mainFrame?因为前面的代码为我创建了另一个MainFrame。

方法主集合标签和计时器启动/停止

public void setUpdateTime(int timeUserConfig) {
this.timeUserConfig = timeUserConfig;
if (timeUserConfig == 0) {
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeDefaultInt);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
} else {          
timeDefault.stop();
timeDefault = new Timer(timeUserConfig, null);
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeUserConfig);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
}
}

setLabelText是标签的方法集

public void setLabelText() {
String humedadStr = String.valueOf(humedad);
String temperaturaStr = String.valueOf(temperatura);
String presionStr = String.valueOf(co2);
temporalHum.setText(humedadStr);
temporalTemperatura.setText(temperaturaStr);
temporalPresion.setText(presionStr);
}

如有任何帮助,我们将不胜感激。

感谢您的更新,我从这个问题中找到了另一个不使用OptionPane的解决方案:以编程方式关闭JDialog中显示的JPanel。

我无法复制你的编码

MainFrame开始,假设您通过单击按钮打开JDialog,并希望setText()标记lbSomething:

private void btInputActionPerformed(java.awt.event.ActionEvent evt) {
// Open new JDialog when button is clicked
NewJDialog dialog = new NewJDialog(new javax.swing.JFrame, true);
dialog.setVisible(true);
// Get user input from JDialog
String temp = dialog.getInput();
if (temp != null) {
/*
* Perform jButton1ActionPerformed() content here
* Including timeUserConfig, timeDefault and setUpdateTime() here
* so that you don't have to access mainFrame in the JDialog.
*/
lbSomething.setText(temp);
}
}

然后关于JDialog(具有简单的输入检测):

public class NewJDialog extends javax.swing.JDialog {
// Set the variable as class variable
private String textTOFieldTimer;
public NewJDialog(java.awt.Frame parent, boolean modal) {
// default contents
}
@SupressWarinings("unchecked")
private void initComponents() {
// default contents
}
private void btSaveAction Performed(java.awt.event.ActionEvent evt) {
// Check if input correct and whether to disable JDialog
if (tfInput.getText.length() != 0) {
input = tfInput.getText();
// Connect to the whole JDialog by getWindowAncestor()
Window window = SwingUtilities.getWindowAncestor(NewJDialog.this);
// Just setVisible(false) instead of dispose()
window.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "Wrong Input");
}
}
public String getInput() {
return textToFieldTimer;
}
// default variables declarations
}

希望这个答案对你有帮助。


如果显示源代码会更好,但将值更新到现有JFrame的简单解决方案是使用setText()getText()

例如:

String input = JOptionPane.showInputDialog(this, "Nuevo valor");
lbPresionActual.setText(input);

如果您创建了一个自定义的JDialog,它将在关闭JDialog时传递input值,这可能是另一个问题。

最新更新