2

我正在使用 Swing 库向用户询问他们的邮政编码,但出现的只是一个框,没有文本框或我添加的任何其他元素(参见代码)。此外,您可能需要知道我正在尝试在我的 public static void main(String[] args) 方法中获取 int AskZip()。

    private static int zip;
    public static int AskZip() {
        JFrame zaWindow = new JFrame("What Zipcode");
        zaWindow.setSize(200, 300);
        JPanel jp = new JPanel();
        final JTextField tf = new JTextField("Enter Zip Here");
        JLabel label = new JLabel();
        JButton button = new JButton("Get Weather");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String sZip = tf.getText();
                int rZip = 0;
                try {
                    if (sZip.length() != 5) {
                        JOptionPane.showMessageDialog(null, "Invalid zipcode!", "Error", JOptionPane.ERROR_MESSAGE);
                    } else {
                        rZip = Integer.parseInt(sZip);
                    }
                } catch (NumberFormatException arg) {
                    JOptionPane.showMessageDialog(null, "Invalid zipcode!", "Error", JOptionPane.ERROR_MESSAGE);
                }
                zip = rZip;
            }
        });
        label.setText("What is your zipcode?");
        jp.add(label);
        jp.add(tf);
        jp.add(button);
        zaWindow.add(jp);
        return zip;
    }
4

1 回答 1

4

JFrame zaWindow..

这应该是模态对话框或JOptionPane. EG 这个国家有 3 个州,每个州有 10 个邮政编码。

输入邮政编号

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ChangeListener;

class ZipQuery {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                ZipNumberModel znm = new ZipNumberModel();
                JSpinner zip = new JSpinner(znm);

                JOptionPane.showMessageDialog(null, zip, "Enter Zipcode", JOptionPane.QUESTION_MESSAGE);
                System.out.println("User chose " + znm.getValue());
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

class ZipNumberModel extends SpinnerNumberModel {

    private ArrayList<Integer> zipCodes;
    private int index = 0;

    ZipNumberModel() {
        zipCodes = new ArrayList<Integer>();
        int zip = 10000;
        for (int jj = 1; jj < 4; jj++) {
            for (int ii = jj * zip; ii < jj * zip + 10; ii++) {
                zipCodes.add(new Integer(ii));
            }
        }
    }

    @Override
    public Object getValue() {
        return zipCodes.get(index);
    }

    @Override
    public Object getNextValue() {
        if (index < zipCodes.size()-1) {
            index++;
        } else {
            index = 0;
        }
        return zipCodes.get(index);
    }

    @Override
    public Object getPreviousValue() {
        if (index > 0) {
            index--;
        } else {
            index = zipCodes.size()-1;
        }
        return zipCodes.get(index);
    }
}
于 2013-06-09T04:02:34.937 回答