【发布时间】:2011-04-29 00:11:04
【问题描述】:
我搜索了几页,但大多数似乎只是对静态的含义有理解问题。我遇到的问题在于我们使用的是静态类、FocusListener 和 ActionListener。具有事件处理调用静态类的类,当一个 JTextfield 被填充并从 FocusListener 中删除时,会立即更新该静态变量。当所有的 JTextfields 都被填满并且 FocusListener 已经更新了变量时,就会出现一个提交 JButton。单击按钮后,将调用静态方法以完成使用先前更新的变量计算的任何变量。用户不知道这一点。但是变量不会更新,我很好奇我是否执行错了?提前致谢。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WellParameters extends JInternalFrame implements FocusListener, ActionListener {
JLabel measuredDepthL, ..., pitGainL, drillBitSizeL,
mudInActivePitsL, mainPanelLabel;
JTextField measuredDepthT, ..., pitGainT, drillBitSizeT, mudInActivePitsT;
JPanel mainPanel, firstPanel, secondPanel, thirdPanel, fourthPanel, submitButtonPanel;
JButton submitButton;
WellParameters() {
super("Well Parameters", true, true, false, true);
this.setBounds(0, 0, 600, 385);
this.setVisible(true);
this.setLayout(new BorderLayout());
...//GUI Stuff
this.add(submitButtonPanel, BorderLayout.SOUTH);
}
@Override
public void focusGained(FocusEvent e) {} //Ignore this!
@Override
public void focusLost(FocusEvent e) {
try {
if(e.getSource() == measuredDepthT) {
KillWellCalculations.measuredDepth = Integer.parseInt(measuredDepthT.getText());
...//Others
} else if(e.getSource() == mudInActivePitsT) {
KillWellCalculations.mudInActivePits = Double.parseDouble(mudInActivePitsT.getText());
}
} catch (Exception ignore) {}
}
@Override
public void actionPerformed(ActionEvent e) {
try {
if(e.getSource() == submitButton) {
System.out.println(KillWellCalculations.pumpEfficiency);
KillWellCalculations.setPressureBeforeCasingBurstAndFormationFracture(); //Doesn't work
KillWellCalculations.setCirculatingPressures();
KillWellCalculations.setTriplexPumpCapacity();
System.out.println(KillWellCalculations.mudInActivePits);
System.out.println(KillWellCalculations.pumpFactor);
System.out.println(KillWellCalculations.finalCirculatingPressure);}
}
catch(Exception ignore) {}
}
}
那是 GUI,这是静态类...它们是 2 个独立的类。不在同一个文件中。 打包 killwellsheet;
public class KillWellCalculations {
static int measuredDepth; //Total Depth from open hole to bottom
... //Tons of other variables
static double totalStrokes; //add strokes
//Used to set different circulating pressures for the well
public static void setCirculatingPressures() {
initialCirculatingPressure = circulatingPressureKillRate + shutInDrillPipePressure;
finalCirculatingPressure = circulatingPressureKillRate * (killMudWeight/currentMudWeight);
}
//Calculates capacity of anypipe
private static double pipeCapacity(double length, double insideDiameter) {
return length * ((insideDiameter*insideDiameter)/1029.4);
}
//Calculates capacity of the annulus/open hole
private static double annulusCapacity(double length, double insideDiameter, double outsideDiameter) {
return length * (((insideDiameter*insideDiameter)-(outsideDiameter*outsideDiameter))/1029.4);
}
... //Other functions
//Set the casing burst pressure
public static void setPressureBeforeCasingBurstAndFormationFracture() {
beforeCasingBurst = burstPressure*0.70;
beforeFormationFracture = (0.052*casingShoeDepth)*(fracGradientMWEquivalent - currentMudWeight);
}
public static void bariteNeedAndVolumeIncrease() {
bariteSacksRequired = (totalMudVolume/100)*((1099*(killMudWeight-currentMudWeight))/(28.35-killMudWeight));
increaseInMudVolume = 0.091*bariteSacksRequired;
}
public static void pumpStrokes() {
surfaceToBit = (mudInDrillString)/pumpFactor;
bitToSurface = (mudInAnnulus)/pumpFactor;
totalStrokes = surfaceToBit + bitToSurface;
}
}//end class
【问题讨论】:
-
我使用这些线条打印作为故障排除。他们告诉我这些方法是否有效。
-
为什么这里的变量和方法都是静态的?静态内部类只是意味着内部类不需要外部类的实例存在。
-
是的,正如 Hovercraft 所说,这里完全没有理由使用静态方法和静态变量。它只会使一切复杂化。
-
它们是两个独立的类。这些文件是 KillWellCalculations.java 和 WellParameters.java
-
您可能希望打印出堆栈跟踪以在您的 catch 子句中进行调试。忽略异常会使代码行为难以遵循。
标签: java static-methods