簡體   English   中英

設置大小在java中不起作用

[英]set size wont work in java

public void start_Gui() {

    JFrame window = new JFrame("Client Program");
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    window.setContentPane(panel);
    panel.setLayout(new GridLayout(1,2));

    JLabel leftside = new JLabel();
    leftside.setLayout(new GridLayout(2, 1));

    JTextArea rightside = new JTextArea();
    rightside.setEditable(false);   //add scroll pane.
    rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    rightside.setLayout(new FlowLayout());

    JTextArea client_text_input = new JTextArea();
    client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    leftside.add(client_text_input);

    JLabel buttons_layer = new JLabel();
    JButton login = new JButton("Login");
    JButton logout = new JButton("Logout");
    buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    buttons_layer.setLayout(new GridLayout(2, 1));
    buttons_layer.add(login);
    buttons_layer.add(logout);
    leftside.add(buttons_layer);

    panel.add(leftside);
    panel.add(rightside);

    window.setSize(300, 400);
    window.setResizable(false);
    window.setVisible(true);
}

我正在開發一個簡單的 java 聊天客戶端 gui 應用程序。 (服務器等,由其他人完成)。

這不是一個大項目,但我唯一的問題是,無論我嘗試調整上述 GUI 上任何組件的大小,都將不起作用。

例如:

JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);

不會工作。

謝謝您的幫助。

在 Swing 中,您有兩種布局選項:手動完成所有操作或讓LayoutManager為您處理。

調用setSize()僅在您不使用LayoutManager時才有效。 由於您使用的是GridLayout您必須使用其他方式來指定您想要的內容。

嘗試調用setPreferredSize()setMinimumSize()

兩件事 - 首先,您應該設置滾動窗格的 preferredSize,但其次,嘗試在 componentResized 處理程序中調整它的大小並不是一種非常有效的技術,因為 'resized' 事件不是連續的。

檢查調整 JFrame 中文本區域的大小

但是如果您從setSize() (對於 TopLayoutContainer)更改為setPreferredSize()並且您必須在setVisible()之前調用pack() ,則setXxxSize (對於 ContainersChilds)將用作 chaims

暫無
暫無

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

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