我有一個名為 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();或者可能將 jTextField 的值從 jDialog 發送到 mainFrame?因為前面的代碼為我創建了另一個 MainFrame。
方法主 setLabel 和 Timer.start/stop
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);
}
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
感謝您的更新,我找到了另一個解決方案,而沒有使用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()and getText()。
例如:
String input = JOptionPane.showInputDialog(this, "Nuevo valor");
lbPresionActual.setText(input);
如果您創建了一個自定義的,它將在關閉 時JDialog傳遞值,這可能是一個不同的問題。inputJDialog
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/475925.html
