我有一個類,在類中,我有三種方法做同樣的事情,但提供不同的輸入,所以我想知道是否有任何方法可以使這個更小。有沒有什麼辦法可以讓這個java更小?
我的代碼;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
public class test {
public void methodclassA() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodA from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
public void methodclassB() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodB from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
public void methodclassC() {
int result = JOptionPane
.showOptionDialog(
null,
"How would you like you insert your data, manually or from a file? ",
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] {
"Manual", "From a File" },
JOptionPane.NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Going to call methodB from another class
}
if (result == JOptionPane.NO_OPTION) {
JTextField NameField = new JTextField();
Object[] message = { "Path location:", NameField };
int result2 = JOptionPane.showOptionDialog(null, message,
"Inserting data", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
"Locate the file" }, JOptionPane.NO_OPTION);
}
}
}
例如,我在課堂上的三種方法是; methodclassA,methodclassB,methodclassC,所有這些都要求用戶輸入相同的內容,但是每個方法都會調用另一個類的不同方法。
在此先感謝,我希望我已經清楚地解釋了我自己。
編輯:我忘了提及之前,我有三個按鈕在我的主類中調用這三種方法。例如我的buttonA調用methodclassA,buttonB調用methodclassB,buttonC調用methodclassC。
請問您能先格式化您的代碼嗎? – alex
格式化代碼 – user3248466