[英]Set JLabel to top of JTextArea and move down JTextArea
I have a small GUI contains JPanel in which have got JTextArea and JLabel, when I make 我有一个包含JPanel的小GUI,当我制作时,其中包含JTextArea和JLabel
panel1.setLayout(null);
I can get the required position done but JTextArea disappearing! 我可以完成所需的位置,但JTextArea消失了! when I make
当我做
panel1.setLayout((LayoutManager) new FlowLayout(FlowLayout.LEFT));
I can get both Jlabel and JTextArea visible but couldn't able to get the position, How to get the required position set, am newbie! 我可以同时看到Jlabel和JTextArea,但无法获取位置,如何获得所需的位置集,是新手! help please!
请帮助! thanks
谢谢
Code: 码:
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);
}
For this type of program, file content display and a label, the BorderLayout suites better. 对于此类程序,文件内容显示和标签,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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.