2

我有一个 Swing JFrame,它有一堆菜单项。下面的代码尝试但未能在选择“添加地址”菜单项时设置 JFrame 的内容。

        JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);

    JMenuBar menuBar = new JMenuBar();
    JMenu clientMenu = new JMenu("Client");
    JMenuItem address = new JMenuItem("Add Address");

    clientMenu.add(address);
    menuBar.add(clientMenu);

    address.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            setupAddressForm();
        }
    });

    private void setupAddressForm() {
        frame.getContentPane().removeAll();

        JPanel infoPanel = new JPanel();
        JLabel infoLabel = new JLabel();
        infoLabel.setText("<html><B>Please enter the address below</B></html>");
        infoPanel.add(infoLabel, SwingConstants.CENTER);
        frame.getContentPane().add(infoPanel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        JButton submitButton = new JButton("Submit Address");
        JButton resetButton = new JButton("Reset");

        submitButton.setToolTipText("Submit");
        resetButton.setToolTipText("Reset");

        buttonPanel.add(submitButton);
        buttonPanel.add(resetButton);

        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridLayout(10, 10));

        JLabel houseNumberLabel = new JLabel("House Number",
                SwingConstants.LEFT);
        final JTextField houseNumberField = new JTextField(3);

        JLabel streetNameLabel = new JLabel("Street Name", SwingConstants.LEFT);
        final JTextField streetNameField = new JTextField(10);

        JLabel localityLabel = new JLabel("Locality", SwingConstants.LEFT);
        final JTextField localityField = new JTextField(10);

        JLabel cityLabel = new JLabel("City", SwingConstants.LEFT);
        final JTextField cityField = new JTextField(10);

        JLabel stateLabel = new JLabel("State", SwingConstants.LEFT);
        final JTextField stateField = new JTextField(10);

        JLabel zipCodeLabel = new JLabel("Zip Code", SwingConstants.LEFT);
        final JTextField zipCodeField = new JTextField(10);

        fieldPanel.add(houseNumberLabel);
        fieldPanel.add(houseNumberField);

        fieldPanel.add(streetNameLabel);
        fieldPanel.add(streetNameField);

        fieldPanel.add(localityLabel);
        fieldPanel.add(localityField);

        fieldPanel.add(cityLabel);
        fieldPanel.add(cityField);

        fieldPanel.add(stateLabel);
        fieldPanel.add(stateField);

        fieldPanel.add(zipCodeLabel);
        fieldPanel.add(zipCodeField);

        frame.getContentPane().add(fieldPanel, BorderLayout.CENTER);

        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String houseNumber = houseNumberField.getText();
                String street = streetNameField.getText();
                String locality = localityField.getText();
                String city = cityField.getText();
                String state = stateField.getText();
                String zipCode = zipCodeField.getText();

                Address address = new Address();

                if (!"".equals(houseNumber) && !"".equals(street)
                        && !"".equals(locality) && !"".equals(city)
                        && !"".equals(state) && !"".equals(zipCode)) {
                    address.setHouseNumber(houseNumber);
                    address.setStreetName(street);
                    address.setLocality(locality);
                    address.setState(state);
                    address.setZipCode(zipCode);
                }

            }
        });

        frame.getContentPane().doLayout();
        frame.getContentPane().update(frame.getGraphics());

}

当我选择“添加地址”菜单项时,框架就变成了空白。不是我所期望的。我使用断点进行了调试,发现正在调用设置地址的方法。我不知道为什么它不起作用。如果我在这里做错了什么,有人可以指出吗?

4

1 回答 1

4

为什么要经历所有这些体操?取而代之的是简单地使用CardLayout来为您交换视图,而不会造成混乱或大惊小怪。

另请注意,您在方法或构造函数(您没有说)中在 (A) 行创建的 JFrame:

JFrame frame = new JFrame(); // (A)

与 (B) 和 (C) 行中使用的 JFrame 不同:

  private void setupAddressForm() {
      frame.getContentPane().removeAll(); // (B)

      JPanel infoPanel = new JPanel();
      JLabel infoLabel = new JLabel();
      infoLabel.setText("<html><B>Please enter the address below</B></html>");
      infoPanel.add(infoLabel, SwingConstants.CENTER);
      frame.getContentPane().add(infoPanel, BorderLayout.NORTH); // (C)

这是因为您在 (A) 行声明了 JFrame,因此它仅在该方法或构造函数中可见。

编辑 1
另外,这是不对的:

  frame.getContentPane().doLayout();
  frame.getContentPane().update(frame.getGraphics());

因为这不是让新组件显示在 Swing 容器中的方式。相反,我会这样做:

  JPanel contentPane = (JPanel) frame.getContentPane();
  contentPane.revalidate();
  contentPane.repaint();

但同样,我真的什么都不做,而是使用上面提到的 CardLayout。

最后,如果这些建议中的任何一个没有意义,请要求澄清。

于 2012-04-14T15:33:39.020 回答