该应用程序不会调整其组件的大小

时间:2012-01-08 16:17:02

标签: java swing layout-manager

我有这个应用程序,但是,当我调整窗口大小,内部元素JTextArea时,它不会随窗口调整大小。为什么呢?

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class ExampleGUI {

private JTextArea text_area;
private JScrollPane scroll_bar;

private JFrame frame;
private JPanel panel;

public ExampleGUI(){

    frame = new JFrame("Example GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text_area = new JTextArea();
    scroll_bar = new JScrollPane(text_area);

    panel = new JPanel();
    panel.add(scroll_bar);

    frame.add(panel);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){public void run(){new ExampleGUI();}});
}
}

3 个答案:

答案 0 :(得分:3)

您需要将GridBagConstraint x和y权重(weightx和weighty - GridBagConstraint构造函数中的第5和第6个参数)设置为0.0以外的正值。你应该阅读关于GridBagLayout的教程,如果你打算使用它,因为它相当复杂。有些人已经成功地嵌套了更简单的布局或使用第三方布局,例如MigLayout

答案 1 :(得分:3)

您的框架布局是FlowLayout。这不会调整孩子的大小。来自the docs

  

流布局允许每个组件采用其自然(首选)大小。

最好使用BorderLayout并将窗格放在CENTER中。

替换它:

frame.setLayout(new FlowLayout());
frame.add(pane);

用这个:

frame.setLayout(new BorderLayout());
frame.add(pane, BorderLayout.CENTER);

另外,正如Hovercraft指出的那样,如果你需要在窗格调整大小时调整单个组件的大小,那么你需要在GridBagConstraints中有非零权重。

答案 2 :(得分:3)

这考虑到了Hovercraft Full Of Eels& amp; Ted Hopp还有其他一些调整。

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.util.Collection;

public class AziendaGUI implements ActionListener {

private JButton view_list;
private JButton save_list;
private JTextArea text_area;
private JScrollPane scrollpane;
private JPanel pane;

private JFrame frame;
private GridBagLayout grid;

public AziendaGUI() {

    frame = new JFrame("Immobiliari s.p.a");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setLayout(new BorderLayout());

    view_list = new JButton("View Property");
    view_list.setActionCommand("view_list");
    view_list.addActionListener(this);

    save_list = new JButton("Save List");
    save_list.setActionCommand("save_list");
    save_list.addActionListener(this);

    text_area = new JTextArea(10,22);
    text_area.setEditable(false);
    scrollpane = new JScrollPane(text_area);

    grid = new GridBagLayout();
    pane = new JPanel();
    pane.setLayout(grid);

    /* Set Constraints view_list button */
    grid.setConstraints(view_list, new GridBagConstraints(0,0,1,1,0.0,0.0,GridBagConstraints.WEST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(view_list);

    /* Set Constraints save_list button */
    grid.setConstraints(save_list,new GridBagConstraints(1,0,1,1,0.1,0.1,GridBagConstraints.EAST,GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));
    pane.add(save_list);

    frame.add(scrollpane);

    frame.add(pane, BorderLayout.NORTH);

    frame.pack();
    frame.setVisible(true);
}

private void store(){

    String file_name = JOptionPane.showInputDialog("Inserisci il nome del file");

}

@Override
public void actionPerformed(ActionEvent e){

    String s = e.getActionCommand();

    if(s.equals("view_list")){
    }
    if(s.equals("save_list")){

        store();
    }
}


public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable(){@Override
                                              public void run(){new AziendaGUI();}});
}
}