在流布局中更改面板尺寸 [英] Change panel size in flow layout

查看:126
本文介绍了在流布局中更改面板尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java开发GUI表单.在那我要在具有FlowLayout的主框架中添加两个面板.但是现在我想更改面板的大小,并尝试使用setsize()进行更改,但看不到任何区别.

I am developing a GUI form in java. In that I am adding two panels in main frame which has FlowLayout. But now I want to change the size of panels and I tried changing using setsize(), and I can see no difference.

这是我的代码:

public class Main_window extends JFrame implements ActionListener {

    JFrame MainFrm = new JFrame("MCQ Generator");
    JPanel LeftPanel = new JPanel();
    JPanel RightPanel = new JPanel();
    JButton BrowseButton = new JButton("Browse/Open");
    TextArea ta1 = new TextArea();
    TextArea ta2 = new TextArea();
    JButton ClearWindow = new JButton("Clear Window");
    JButton generateButton = new JButton("Generate MCQs");
    String fname;

    Main_window() {
        MainFrm.setLayout(new FlowLayout(FlowLayout.LEFT));
        MainFrm.setSize(1050, 400);
        MainFrm.add(LeftPanel, FlowLayout.LEFT);
        MainFrm.add(RightPanel);
        LeftPanel.setLayout(new BorderLayout(30, 30));
    //LeftPanel.setSize(10, 10);
        //RightPanel.setSize(10, 10);
        RightPanel.setLayout(new BorderLayout(40, 40));
        LeftPanel.setOpaque(true);
        LeftPanel.setBackground(Color.LIGHT_GRAY);
        LeftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        RightPanel.setOpaque(true);
        RightPanel.setBackground(Color.LIGHT_GRAY);
        RightPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        JPanel InnerPanel = new JPanel();
        InnerPanel.setLayout(new FlowLayout());
        InnerPanel.setBackground(Color.LIGHT_GRAY);
        InnerPanel.add(new JLabel("Choose File: "));
        LeftPanel.add(InnerPanel, BorderLayout.NORTH);
        LeftPanel.add(ta1, BorderLayout.CENTER);
        RightPanel.add(new JLabel("Questions Generated:"), BorderLayout.NORTH);
        RightPanel.add(ta2, BorderLayout.CENTER);
    //ta1.setSize(10, 100);
        //ta2.setSize(10, 100);
        BrowseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                JFileChooser chooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "Text Documents(*.txt)", "txt");
                chooser.setFileFilter(filter);
                int returnVal = chooser.showOpenDialog(LeftPanel);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("You choose to open this file: " + chooser.getCurrentDirectory() + "\\" + chooser.getSelectedFile().getName());
                    fname = new String(chooser.getCurrentDirectory() + "\\" + chooser.getSelectedFile().getName());
                    try {
                        Jdbc_conn.truncateInputTable(ta1);
                        displayInput(fname);
                        //Jdbc_conn.truncateInputTable(ta1);
                        Give_input.getInput(fname);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

        });
        InnerPanel.add(BrowseButton);
        LeftPanel.add(generateButton, BorderLayout.SOUTH);
        generateButton.addActionListener(this);
        RightPanel.add(ClearWindow, BorderLayout.SOUTH);
        ClearWindow.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                try {
                    Jdbc_conn.truncateMcqAndNew_nountab(ta2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        MainFrm.setVisible(true);
        MainFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

推荐答案

首先,看看在容器内布置组件以获取有关布局API的信息.

First, take a look at Laying Out Components Within a Container for information about the layout API.

您永远都不需要(好吧,很少)直接在组件上调用setSize.布局管理器管理下的任何组件的大小都将满足布局管理器的要求,因此,一般来说,调用setSize是没有意义的练习

You should never (okay, very rarely) need to call setSize on a component directly. Any component under the management of a layout manager will be sized to meet the requirements of the layout manager, so generally speaking, call setSize is a pointless exercise

使用EmptyBorder也许可以影响组件的大小,但这只是向组件中添加内部填充,这可能是不希望的.

You might be able effect the size of a component by using an EmptyBorder, but this is just adding internal padding to the component, which might not be desirable.

另一种选择是使用不同的布局管理器,该管理器为您提供更多控制权,例如MigLayoutGridBagLayout,但这会增加代码的一般复杂性.

Another choice is to use a different layout manager which gives you more control, something like MigLayout or GridBagLayout, but this will increase the general complexity of the code.

另一种选择是覆盖要调整大小的组件的getPreferredSize方法,并返回要使用的值.但是请注意,不要轻率地做这件事,或者不要过多地考虑为什么"要更改默认大小.主要原因是,不同的平台可以更改任何组件可能需要的空间量,以便正确布局.

The other choice would be to override the getPreferredSize method of the components you want to size and return the values you want to use. Be warned though, this is not to be done lightly or without a lot of consideration to "why" you want to change the default size. The main reason is, different platforms can change the amount of space any component might need in order to be laid out correctly.

这篇关于在流布局中更改面板尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆