0

这按预期工作,但我只是想知道如何清理它,以便我可以将三个面板(inputPanel、buttonPanel 和 outputPanel)中的每一个放入它们自己的方法中,然后从那里调用它们。例如,我不想在最后 4 个方法中的每一个中重新创建 outputPanel。

提前感谢您的帮助。

public static void main(String[] args) {
    int inputMin = 0, inputMax = 0;

    JPanel inputPanel = new JPanel();
    JTextField Min = new JTextField(5);
    JTextField Max = new JTextField(5);
    inputPanel.add(new JLabel("Min: "));
    inputPanel.add(Min);
    inputPanel.add(new JLabel("Max: "));
    inputPanel.add(Max);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    JRadioButton Primes = new JRadioButton("Primes");
    JRadioButton Palindromes = new JRadioButton("Palindromes");
    JRadioButton Emirps = new JRadioButton("Emirps");
    JRadioButton Twins = new JRadioButton("Twin Primes");
    JRadioButton All = new JRadioButton("All");
    buttonPanel.add(Primes);
    buttonPanel.add(Palindromes);
    buttonPanel.add(Emirps);
    buttonPanel.add(Twins);
    buttonPanel.add(All);

    for (int i = 0; i >= 0; i++) {
        JOptionPane.showConfirmDialog(null, inputPanel,
                "Enter Range 1 - 10000", JOptionPane.PLAIN_MESSAGE);
        inputMin = Integer.parseInt(Min.getText());
        inputMax = Integer.parseInt(Max.getText());

        if ((inputMin >= 1) && (inputMax <= 10000) && 
                (inputMax > inputMin)) {
            break;
        } else {
            JOptionPane.showMessageDialog
            (null, "Invalid input. Please try again.", 
                    "Error", JOptionPane.ERROR_MESSAGE);
            i++;
        }
    }

    JOptionPane.showConfirmDialog(null, buttonPanel,
            "Choose Output", JOptionPane.PLAIN_MESSAGE);

    if (Primes.isSelected()) {
        primes(inputMin, inputMax);
    } else if (Palindromes.isSelected()) {
        palindromes(inputMin, inputMax);
    } else if (Emirps.isSelected()) {
        emirps(inputMin, inputMax);
    } else if (Twins.isSelected()) {
        twins(inputMin, inputMax);
    } else if (All.isSelected()) {
        primes(inputMin, inputMax);
        palindromes(inputMin, inputMax);
        emirps(inputMin, inputMax);
        twins(inputMin, inputMax);
    }
System.exit(0);
}

public static void primes(int inputMin, int inputMax) {
    JPanel outputPanel = new JPanel();
    JTextArea outputText = new JTextArea();
    outputText.setPreferredSize(new Dimension(400, 100));
    outputText.setLineWrap(true);
    outputText.setWrapStyleWord(true);
    JScrollPane thing = new JScrollPane(outputText);
    outputPanel.add(thing);

    int i, j, run;
    boolean prime;
    outputText.setText("The first 50 prime numbers between "
            + inputMin + " and " + inputMax + " are:\n");
    for (i = inputMin, run = 0; i <= inputMax; i++) {
        prime = true;
        if (i == 1) {
            i++;
        }
        if (i == 2) {
            outputText.append(i + ", ");
            i++;
        }
        for (j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                prime = false;
            }
        }
        if (prime) {
            outputText.append(i + ", ");
            i++;
            run++;
            if (run == 50) {
                break;
            }
        }
    }
    JOptionPane.showConfirmDialog(null, outputPanel,
            "Primes", JOptionPane.PLAIN_MESSAGE);
}

public static void palindromes(int inputMin, int inputMax) {
    JPanel outputPanel = new JPanel();
    JTextArea outputText = new JTextArea();
    outputText.setPreferredSize(new Dimension(400, 100));
    outputText.setLineWrap(true);
    outputText.setWrapStyleWord(true);
    JScrollPane thing = new JScrollPane(outputText);
    outputPanel.add(thing);

    int i, j, run;
    boolean prime;
    outputText.setText("The first 20 palindromic prime numbers between "
            + inputMin + " and " + inputMax + " are:\n");
    for (i = inputMin, run = 0; i <= inputMax; i++) {
        prime = true;
        if (i == 1) {
            i++;
        }
        if (i == 2) {
            outputText.append(i + ", ");
            i++;
        }
        for (j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                prime = false;
            }
        }
        if (prime) {
            String number = Integer.toString(i);
            int reverse = Integer.parseInt
            (new StringBuffer(number).reverse().toString());
            if (reverse == i) {
                outputText.append(i + ", ");
                i++;
                run++;
                if (run == 20) {
                    break;
                }
            }
        }
    }
    JOptionPane.showConfirmDialog(null, outputPanel,
            "Palindromic Primes", JOptionPane.PLAIN_MESSAGE);
}

public static void emirps(int inputMin, int inputMax) {
    JPanel outputPanel = new JPanel();
    JTextArea outputText = new JTextArea();
    outputText.setPreferredSize(new Dimension(400, 100));
    outputText.setLineWrap(true);
    outputText.setWrapStyleWord(true);
    JScrollPane thing = new JScrollPane(outputText);
    outputPanel.add(thing);

    int i, j, run;
    boolean prime;
    outputText.setText("The first 20 emirps between "
            + inputMin + " and " + inputMax + " are:\n");
    for (i = inputMin, run = 0; i <= inputMax; i++) {
        prime = true;
        for (j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                prime = false;
            }
        }
        if (prime) {
            String number = Integer.toString(i);
            int reverse = Integer.parseInt
            (new StringBuffer(number).reverse().toString());
            for (j = 2; j <= Math.sqrt(reverse); j++) {
                if (reverse % j == 0) {
                    prime = false;
                }
            }
            if ((prime) && (i != reverse)
                    && (i < reverse) && (reverse <= inputMax)) {
                outputText.append(i + " and " + reverse + ", ");
                run++;
                if (run == 20) {
                    break;
                }
            }
        }
    }
    JOptionPane.showConfirmDialog(null, outputPanel,
            "Emirps", JOptionPane.PLAIN_MESSAGE);
}

public static void twins(int inputMin, int inputMax) {
    JPanel outputPanel = new JPanel();
    JTextArea outputText = new JTextArea();
    outputText.setPreferredSize(new Dimension(400, 100));
    outputText.setLineWrap(true);
    outputText.setWrapStyleWord(true);
    JScrollPane thing = new JScrollPane(outputText);
    outputPanel.add(thing);

    int i, j, run;
    boolean prime;
    outputText.setText("The first 20 twin primes between "
            + inputMin + " and " + inputMax + " are:\n");
    for (i = inputMin, run = 0; i <= inputMax; i++) {
        prime = true;
        if (i == 1) {
            i++;
        }
        for (j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                prime = false;
            }
        }
        if (prime) {
            int twin = i + 2;
            for (j = 2; j <= Math.sqrt(twin); j++) {
                if (twin % j == 0) {
                    prime = false;
                }
            }
            if (prime) {
                outputText.append(i + " and " + twin + ", ");
                run++;
                if (run == 20) {
                    break;
                }
            }
        }
    }
    JOptionPane.showConfirmDialog(null, outputPanel,
            "Twin Primes", JOptionPane.PLAIN_MESSAGE);
}

}

4

1 回答 1

1

你可以

创建一个“帮助”方法来创建相关面板,例如......

public static void main(String[] args) {
    //..
    JPanel inputPanel = createInputPane();
    //...
}

public JPanel createInputPane() {
    JPanel inputPanel = new JPanel();
    JTextField Min = new JTextField(5);
    JTextField Max = new JTextField(5);
    inputPanel.add(new JLabel("Min: "));
    inputPanel.add(Min);
    inputPanel.add(new JLabel("Max: "));
    inputPanel.add(Max);
    return inputPanel;
}

这样做的问题是,如果您需要访问面板上包含的字段,它会变得很麻烦......

你可以

创建一个自定义面板,提供所需的基本控件和访问功能...

public class InputPane extends JPanel {

    private final JTextField minField;
    private final JTextField maxField;

    public InputPane() {
        minField = new JTextField(5);
        maxField = new JTextField(5);
        add(new JLabel("Min: "));
        add(minField);
        add(new JLabel("Max: "));
        add(maxField);
    }

    public String getMaxValue() {
        return maxField.getText();
    }

    public String getMinValue() {
        return minField.getText();
    }

    public void setMaxValue(String value) {
        maxField.setText(value);
    }

    public void setMinValue(String value) {
        minField.setText(value);
    }

}

然后,您可以在需要时随时创建一个新实例。

于 2013-10-30T03:16:44.430 回答