gpt4 book ai didi

java - 在 JOptionPane 中重新绘制组件

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:17:22 26 4
gpt4 key购买 nike

我有部分 View 在 JOptionPane 中显示文本字段。还有一个按钮“其他”。单击它后,我需要 JOptionPane 重新绘制自身并显示一些隐藏的文本字段。

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

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

我把我的代码做的尽可能简单,只是 View 的一部分。

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

您能就如何进行正确的重绘给出建议吗?

最佳答案

也许是 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();
}
}

关于java - 在 JOptionPane 中重新绘制组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com