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

发布于 2024-12-26 03:06:14 字数 757 浏览 1 评论 0原文

我有这个应用程序,但是,当我调整窗口大小时,其中的元素 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();}});
}
}

I have this app, but, when I resize the window, the element JTextArea inside, it doesn't resize with the window. Why?

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();}});
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴情换悲伤 2025-01-02 03:06:14

您需要将 GridBagConstraint x 和 y 权重(weightx 和weighty——GridBagConstraint 构造函数中的第 5 个和第 6 个参数)设置为除 0.0 之外的正值。如果您要使用 GridBagLayout,您应该阅读有关 GridBagLayout 的教程,因为它相当复杂。有些在嵌套更简单的布局或使用第三方布局(例如 MigLayout)方面取得了巨大成功。

You need to set your GridBagConstraint x and y weights (weightx and weighty -- the 5th and 6th parameters in the GridBagConstraint constructor) to a positive value other than 0.0. You should read tutorials on GridBagLayout if you're going to use it as it is fairly complex. Some have had great success nesting simpler layouts or using 3rd party layouts such as MigLayout.

谢绝鈎搭 2025-01-02 03:06:14

您的框架布局是 FlowLayout。这不会调整子项的大小。来自文档

流式布局让每个组件呈现其自然(首选)大小。

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

将其替换

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

为:

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

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

Your frame layout is a FlowLayout. This does not resize children. From the docs:

A flow layout lets each component assume its natural (preferred) size.

You will be better off using a BorderLayout and putting the pane in the CENTER.

Replace this:

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

with this:

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

Also, as Hovercraft points out, if you need the individual components to resize when the pane resizes, then you need to have non-zero weights in the GridBagConstraints.

孤蝉 2025-01-02 03:06:14

这考虑到了 Hovercraft Full Of Eels & 的建议。特德·霍普 (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();}});
}
}

This takes into account the advice of Hovercraft Full Of Eels & Ted Hopp with a few other tweaks.

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();}});
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文