gpt4 book ai didi

java - 更新 JDialog 时出现奇怪/不同的行为

转载 作者:行者123 更新时间:2023-12-01 12:13:02 24 4
gpt4 key购买 nike

在一个项目中,我有一个 JFrame A 启动一个 JDialog B,它本身启动一个 JDialog C(全部使用按钮)。但是,当执行以下步骤之一时:

  • 我点击A启动B;或
  • 我点击 A 启动 B,然后点击 B 启动 C,然后点击 C 的取消按钮,

B 中显示的内容不一样(第二个过程给出了一个奇怪的丑陋的东西)。

我不明白为什么会这样,因为这两种方式都会调用我的 updateAll 方法。

我尝试用一​​个小程序重新创建这个,以便更容易看到发生了什么。解决这个问题可能会也可能不会解决我的实际项目,但它肯定会有所帮助。

因为我不知道它来自哪里,所以这是我的(测试)程序的完整代码。做好准备。

“A”框架

public class MyFrame extends JFrame {
private static final long serialVersionUID = 7073064926636937881L;
public MyFrame() {
this.setSize(200, 300);
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new MyDialog1().setVisible(true);
}
});
this.getContentPane().add(button);
}

public static void main(String args[]) {
new MyFrame().setVisible(true);
}
}

“B”对话框

 public class MyDialog1 extends JDialog {
private static final long serialVersionUID = 9181006217120036637L;
private JScrollPane scrollPane;
public String text = "aaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaa";

public MyDialog1() {
this.setVisible(false);
this.setSize(800, 600);

this.initComponent();
this.updateAll();
}

private void initComponent() {
this.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

this.scrollPane = new JScrollPane();

c.gridx = 0;
c.gridy = 0;
this.getContentPane().add(this.scrollPane, c);

c.gridx = 0;
c.gridy = 1;
JButton b = new JButton("Supposedly edit stuff");
final MyDialog1 caller = this;
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new MyDialog2(caller).setVisible(true);
}
});
this.getContentPane().add(b, c);

c.gridx = 1;
c.gridy = 1;
b = new JButton("Leave");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
}
});
this.getContentPane().add(b, c);
}

public void updateAll() {
JPanel mainPanel = new JPanel();
for (int i = 0 ; i < 5 ; i++) {
JPanel subPanel = new JPanel();
JTextArea t = new JTextArea(this.text);
t.setSize(60, 30);
t.setVisible(true);// Useful ? What about setSize ?
subPanel.add(t);
mainPanel.add(subPanel);
}
this.scrollPane.setSize(150, 150); // FIXME When in initComponent, doesn't do anything, and when in updateAll, behavior is inconsistent
this.scrollPane.setViewportView(mainPanel); // Replacing previous JPanel

}
}

“C”对话框

public class MyDialog2 extends JDialog {
private static final long serialVersionUID = 5676648412234106581L;
private MyDialog1 caller;

public MyDialog2(MyDialog1 c) {
this.setSize(100, 150);
this.caller = c;
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
setVisible(false);
caller.text += "\nbbbbbbbbbbbbb\nbbbbbbbbbbbbbbbbbb\nbbbbbbbbbbb\nbbbbbbbbbbbbbb\ncccccccccccccccccccccccccccccccccccccccccccc\ncccccccccc";
caller.updateAll();
}
});
this.getContentPane().add(cancelButton);
}
}

感谢您的帮助。

最佳答案

我建议使用

c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH; // make the component fill its display area entirely
c.ipady = 150; //height
c.anchor = GridBagConstraints.FIRST_LINE_START; // component start from the left top corner
this.getContentPane().add(this.scrollPane, c);

用于 JScrollPane 约束的定义。

另外,修改元素后添加validate()和repaint()

this.scrollPane.setViewportView(mainPanel); // Replacing previous JPanel
this.validate();
this.repaint();

关于java - 更新 JDialog 时出现奇怪/不同的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27175921/

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