簡體   English   中英

如何設置JScrollPane的大小和位置?

[英]How do I set the size and location of a JScrollPane?

我正在嘗試在JFrame中設置JScrollPane的大小和位置,它的JViewportJTextArea

以下是我正在使用的代碼:

private static JFrame frame = new JFrame();

public static void main(String... args) {
    frame.setTitle("Friend App");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setLayout(null);
    scrollPane.setLocation(30, 30);
    scrollPane.setSize(new Dimension(100, 100));
    frame.add(scrollPane);
    textArea.append("test");
    frame.setVisible(true);

}

我還應該做什么,以便可以調整此JScrollPane的大小並設置其位置? 我在這里使用最佳方法嗎?

注意: scrollPane.setLayout(null)什么也不做, frame.pack()不是我想要的。

將scrollPane添加到JFrame的內容窗格中,該窗格默認情況下具有BorderLayout。 BorderLayout忽略組件的位置和大小。

您應該使用另一個LayoutManager(或者,您可以在內容窗格中使用null布局,但這不是好方法)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM