gpt4 book ai didi

java - BoxLayout 无法共享

转载 作者:行者123 更新时间:2023-12-01 22:37:52 26 4
gpt4 key购买 nike

是的,我用谷歌搜索了大约 30 分钟。是的,stackoverflow 中有 2 个关于该主题的不同帖子,但这些帖子并没有为我的问题提供任何解决方案。

我正在使用相当多的带有 BoxLayout 的面板来定位一些东西。当我尝试将最后一个内容添加到我的主面板时,我收到“BoxLayout 无法共享”。

代码:

private void open(int i) {
JLabel titelLabel = new JLabel("Aufgabenblatttitel: ");
JTextField titelTextField = new JTextField();
JLabel dozentLabel = new JLabel("Dozent: ");
JTextField dozentTextField = new JTextField();
JLabel beschreibungLabel = new JLabel("Aufgabenblattbeschreibung: ");
JTextField beschreibungTextField = new JTextField();
JLabel studiengangLabel = new JLabel("Studiengang: ");
JTextField studiengangTextField = new JTextField();
JLabel dateLabel = new JLabel("Erstellt am: ");

for(Aufgabe aufgabe : data.get(i).getAufgaben()) {
JPanel aufgabenPanel = new JPanel();

JLabel aufgabeTitelLabel = new JLabel("Titel: ");
JTextField aufgabeTitelTextField = new JTextField();
aufgabeTitelTextField.setText(aufgabe.getTitel());
JPanel aufgabeTitelPanel = new JPanel();
aufgabeTitelPanel.add(aufgabeTitelLabel);
aufgabeTitelPanel.add(aufgabeTitelTextField);
aufgabeTitelPanel.setLayout(new BoxLayout(aufgabeTitelPanel, BoxLayout.LINE_AXIS));

JLabel aufgabeBeschreibungLabel = new JLabel("Beschreibung: ");
JTextField aufgabeBeschreibungTextField = new JTextField();
aufgabeBeschreibungTextField.setText(aufgabe.getBeschreibung());
JPanel aufgabeBeschreibungPanel = new JPanel();
aufgabeBeschreibungPanel.add(aufgabeBeschreibungLabel);
aufgabeBeschreibungPanel.add(aufgabeBeschreibungTextField);
aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS));

JLabel aufgabeLoesungLabel = new JLabel("Lösung: ");
JTextField aufgabeLoesungTextField = new JTextField();
aufgabeLoesungTextField.setText(aufgabe.getLoesung());
JPanel aufgabeLoesungPanel = new JPanel();
aufgabeLoesungPanel.add(aufgabeLoesungLabel);
aufgabeLoesungPanel.add(aufgabeLoesungTextField);
aufgabeLoesungPanel.setLayout(new BoxLayout(aufgabeLoesungPanel, BoxLayout.LINE_AXIS));

aufgabenPanel.add(aufgabeTitelPanel);
aufgabenPanel.add(aufgabeBeschreibungPanel);
aufgabenPanel.add(aufgabeLoesungPanel);
aufgabenPanel.setLayout(new BoxLayout(aufgabenPanel, BoxLayout.PAGE_AXIS));

this.add(aufgabenPanel);
}
}

它是“AufgabeEditieren”类的一部分,定义为:

public class AufgabeEditieren extends JPanel { ... }

所以:AufgabeEditieren 构造函数在类初始化后调用 open()。它尝试创建一些面板和对象,并希望通过“this.add(aufgabenPanel);”将它们添加到类本身。这是指 AufgabeEditieren 类(它的对象)。那么为什么它不起作用呢?它是一个面板,应该能够获得那些元素?谢谢...

最佳答案

好吧,我花了一段时间,因为我真的不熟悉你的母语(如果你发布带有英文变量名称的代码,对每个人来说都会简单得多),但问题来自这里:

aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS));

您在 aufgabeBeschreibungPanel 上设置 BoxLayout,但提供 aufgabeBeschreibungLabel 作为 BoxLayout 的参数。你应该这样写:

aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungPanel, BoxLayout.LINE_AXIS));

看到此问题时,最常见的原因是您写道:

y.setLayout(new BoxLayout(x, BoxLayout.XXX));

其中 yx 不同。

关于java - BoxLayout 无法共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634819/

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