gpt4 book ai didi

java - 将 JLabel 设置为 JTextArea 顶部并向下移动 JTextArea

转载 作者:行者123 更新时间:2023-11-30 03:31:16 33 4
gpt4 key购买 nike

当我制作时,我有一个包含 JPanel 的小型 GUI,其中有 JTextArea 和 JLabel

panel1.setLayout(null);

我可以完成所需的位置,但 JTextArea 消失了!当我做时

panel1.setLayout((LayoutManager) new FlowLayout(FlowLayout.LEFT));

我可以同时获得 Jlabel and JTextArea可见但无法获取位置,如何获取所需的位置集,我是新手!请帮助!谢谢

代码:

private void createPageScreen1() {
panel1 = new JPanel();
panel1.setLayout((LayoutManager) new FlowLayout(FlowLayout.LEFT));
//panel1.setLayout(null);
//panel1.setLayout(new BorderLayout());


JLabel lblProcessingData = new JLabel();
//lblProcessingData.setBounds(5, 5, 15, 10);
lblProcessingData.setLocation(5, 5);
lblProcessingData.setText("label for Title");
panel1.add(lblProcessingData);

String fileName = "loadFiles\\testFile.txt";

try {
textArea = new JTextArea(40, 37);
//textArea.setBounds(5, 20, 40, 37);
textArea.setBorder (new TitledBorder (new EtchedBorder(), fileName));
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);

FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
textArea.read(br, null);
br.close();
textArea.requestFocus();

JScrollPane scroll1 = new JScrollPane(textArea);
scroll1.setVerticalScrollBarPolicy (ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel1.add(scroll1);
} catch (Exception e) {

e.printStackTrace();
}
panel1.setVisible(true);
}

最佳答案

对于这种类型的程序,文件内容显示和标签,BorderLayout比较适合。

private void createPageScreen1()
{
panel1 = new JPanel();
// Use BorderLayout
panel1.setLayout(new BorderLayout());

JLabel lblProcessingData = new JLabel();
lblProcessingData.setText("label for Title");
// Add Label to top
lblProcessingData.setBorder(new EmptyBorder(5, 5, 5, 5));
panel1.add(lblProcessingData, BorderLayout.NORTH);

String fileName = "loadFiles\\testFile.txt";

try
{
textArea = new JTextArea(40, 37);
textArea.setBorder(new TitledBorder(new EtchedBorder(), fileName));
textArea.setLineWrap(true);
textArea.setEditable(false);
textArea.setVisible(true);

FileReader reader = new FileReader(fileName);
BufferedReader br = new BufferedReader(reader);
textArea.read(br, null);
br.close();
textArea.requestFocus();

JScrollPane scroll1 = new JScrollPane(textArea);
scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// Fill remaining space with TextArea
panel1.add(scroll1, BorderLayout.CENTER);
}
catch (Exception e)
{

e.printStackTrace();
}
panel1.setVisible(true);
}

关于java - 将 JLabel 设置为 JTextArea 顶部并向下移动 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28963099/

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