【发布时间】:2014-01-29 19:37:50
【问题描述】:
我有一个类,在类中我有三个方法,它们做同样的事情但提供不同的输入,所以我想知道是否有任何方法可以使这个调用更小。
我的代码;
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,所有这些都要求用户输入相同的输入,但是每个方法都会从不同的类调用不同的方法。
提前谢谢,我希望我已经清楚地解释了自己。
edit:我之前忘了提,我的主类中有三个按钮,它们分别调用这三个方法。比如我的buttonA调用methodclassA,buttonB调用methodclassB,buttonC调用methodclassC。
【问题讨论】:
-
你能先格式化你的代码吗?
标签: java swing methods refactoring