在JOptionPane中重新绘制组件

时间:2015-10-28 16:46:21

标签: java swing joptionpane repaint invalidation

我有一些视图的一部分显示JOptionPane中的文本字段。还有一个按钮"其他"。点击它后,我需要JOptionPane重绘自己并显示一些隐藏的文本字段。

我将GridLayout用于我的文本字段。我在按钮侦听器中尝试了revalidate()repaint()方法,但它们没有做任何更改。

validate()方法有效,但所有组件的大小都太小。似乎在网格面板中调整大小错误。在我的原始代码中,一些组件只是隐藏。我没有setPrefferedSize()getPrefferedSize(),因为我认为这是错误的,但无法弄清楚如何正确调整大小。

我接受了我的代码并且尽可能简单,只是视图的一部分。

public class AddView {

    private JFrame parentFrame;
    private JPanel addPanel;
    private JPanel gd;

    private JLabel nameLabel;
    private JLabel surName;
    private JLabel skype;

    private JTextField nameTField;
    private JTextField surNameTField;
    private JTextField skypeTField;

    private JButton otherButton;

    public AddView(JFrame parent) {
        this.parentFrame = parent;
        initComponents();
    }

    private void initComponents() {
        addPanel = new JPanel(new BorderLayout());
        gd = new JPanel(new GridLayout(2, 2, 0, 5));

        nameLabel = new JLabel("Name");
        surName = new JLabel("Surname");
        skype = new JLabel("Skype");

        nameTField = new JTextField();
        surNameTField = new JTextField();
        skypeTField = new JTextField();

        otherButton = new JButton("Other");

        gd.add(nameLabel);
        gd.add(nameTField);
        gd.add(surName);
        gd.add(surNameTField);

        addPanel.add(gd, BorderLayout.CENTER);
        addPanel.add(otherButton, BorderLayout.SOUTH);

        otherButton.addActionListener(new OtherFieldsAction());
    }

    public int showAddPane() {
        return JOptionPane.showConfirmDialog(parentFrame, addPanel, "Add Contact", JOptionPane.OK_CANCEL_OPTION);
    }

    private class OtherFieldsAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            gd.setLayout(new GridLayout(3, 2, 0, 5));
            gd.add(skype);
            gd.add(skypeTField);
            // gd.revalidate(); not work
            // gd.repaint();
            gd.validate();

        }
    }
}

这是JOptionPane

enter image description here

当我点击"其他"按钮我有这个结果

enter image description here

但我需要像这样正确调整大小来获得它

enter image description here

您能就如何正确重画提出建议。

2 个答案:

答案 0 :(得分:2)

也许Window#pack()方法可行:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AddView2 {
  private JFrame parentFrame;
  private JPanel addPanel;
  private JPanel gd;

  private JLabel nameLabel;
  private JLabel surName;
  private JLabel skype;

  private JTextField nameTField;
  private JTextField surNameTField;
  private JTextField skypeTField;

  private JButton otherButton;

  public AddView2(JFrame parent) {
    this.parentFrame = parent;
    initComponents();
  }

  private void initComponents() {
    addPanel = new JPanel(new BorderLayout());
    gd = new JPanel(new GridLayout(2, 2, 0, 5));

    nameLabel = new JLabel("Name");
    surName = new JLabel("Surname");
    skype = new JLabel("Skype");

    nameTField = new JTextField();
    surNameTField = new JTextField();
    skypeTField = new JTextField();

    otherButton = new JButton("Other");

    gd.add(nameLabel);
    gd.add(nameTField);
    gd.add(surName);
    gd.add(surNameTField);

    addPanel.add(gd, BorderLayout.CENTER);
    addPanel.add(otherButton, BorderLayout.SOUTH);

    otherButton.addActionListener(new OtherFieldsAction());
  }

  public int showAddPane() {
    return JOptionPane.showConfirmDialog(parentFrame, addPanel, "Add Contact", JOptionPane.OK_CANCEL_OPTION);
  }

  private class OtherFieldsAction implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      gd.setLayout(new GridLayout(3, 2, 0, 5));
      gd.add(skype);
      gd.add(skypeTField);
      // gd.revalidate(); not work
      // gd.repaint();
      // gd.validate();
      SwingUtilities.getWindowAncestor((Component) e.getSource()).pack();
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    new AddView2(f).showAddPane();
  }
}

答案 1 :(得分:0)

您可能需要调用添加组件的setPreferredSize()以获得所需的正确尺寸。