gpt4 book ai didi

java - JPanels : One with a JTextArea and another with a JLabel

转载 作者:行者123 更新时间:2023-12-01 08:05:42 25 4
gpt4 key购买 nike

我已经从事这个工作有一段时间了,但我似乎无法掌握它。我正在尝试生成一个 JPanel,上面有一个 JTextArea,下面有两个 JLabel,但我的 JLabel 最终位于 JTextArea 的左侧,我无法让另一个出现。

这是我的代码(抱歉显示内容 - 只是填充):

public JPanel contentPane() {
JPanel something = new JPanel();

String information = "Please";

info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);

JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);

JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);

something.setOpaque(true);
return something;
}

public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");

DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}

请并感谢所有花时间提供帮助的人。

最佳答案

当您创建某物时,您没有指定任何布局管理器,但稍后您尝试使用以下命令将一个添加到某物 BorderLayout 常量——这不起作用,因为 JPanel 的默认布局管理器是 FlowLayout。

试试这个;

JPanel something = new JPanel(new BorderLayout());

关于java - JPanels : One with a JTextArea and another with a JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864729/

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