[英]location and size of jtextarea in jscrollpane is not set
I am working on the editor. 我正在编辑。 I am using Java swing for it.
我正在使用Java swing。 I have embedded a
JTextArea
with JScrollPane
. 我在
JScrollPane
嵌入了一个JTextArea
。 i want to position the jtextarea
of particular size at the middle of JScrollPane
. 我想将特定大小的
jtextarea
在JScrollPane
的中间。 To do this I used setLocation
function. 为此,我使用了
setLocation
函数。 But this is not working? 但这行不通吗?
public class ScrollPaneTest extends JFrame {
private Container myCP;
private JTextArea resultsTA;
private JScrollPane scrollPane;
private JPanel jpanel;
public ScrollPaneTest() {
resultsTA = new JTextArea(50,50);
resultsTA.setLocation(100,100);
jpanel=new JPanel(new BorderLayout());
jpanel.add(resultsTA,BorderLayout.CENTER);
scrollPane = new JScrollPane(jpanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(800, 800));
scrollPane.setBounds(0, 0, 800, 800);
setSize(800, 800);
setLocation(0, 0);
myCP = this.getContentPane();
myCP.setLayout(new BorderLayout());
myCP.add(scrollPane);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new ScrollPaneTest();
}
}
You simply have to add the JTextArea
to the JScrollPane
, and add it to the CENTER
of the JPanel
having BorderLayout
. 您只需将
JTextArea
添加到JScrollPane
并将其添加到具有BorderLayout
的JPanel
的CENTER
。
Don't use AbsolutePositioning. 不要使用AbsolutePositioning。 Add a proper LayoutManager , and let LayoutManager do the rest for positioning and sizing your components on the screen.
添加适当的LayoutManager ,让LayoutManager进行其余操作,以在屏幕上定位和调整组件大小。
In order to use the setBounds(...)
method you have to use a null
Layout for your component, which is not worth using, provided the perspective, as mentioned in the first paragraph of the AbsolutePositioning . 为了使用
setBounds(...)
方法,您必须为组件使用null
布局,这是不值得使用的,提供透视,如AbsolutePositioning第一段中所述。 Though in the code example provided by you, you are doing both the thingies together ie using Layout and using AbsolutePositioning, which is wrong in every way. 尽管在您提供的代码示例中,您将同时进行这两个事情,即使用Layout和AbsolutePositioning,但这在所有方面都是错误的。 My advice STOP DOING IT :-)
我的建议停止这样做 :-)
In the example provided the ROWS and COLUMNS provided by you are sufficient to size the JTextArea
by the Layout concern. 在提供的示例中,您提供的ROWS和COLUMNS足以根据布局关注来调整
JTextArea
的大小。
Code Example : 代码示例:
import java.awt.*;
import javax.swing.*;
public class Example
{
private JTextArea tarea;
private void displayGUI()
{
JFrame frame = new JFrame("JScrollPane Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(5, 5));
JScrollPane textScroller = new JScrollPane();
tarea = new JTextArea(30, 30);
textScroller.setViewportView(tarea);
contentPane.add(textScroller);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new Example().displayGUI();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.