使用Java布局定义容器的最小大小 [英] define container minimum size using java layouts

查看:244
本文介绍了使用Java布局定义容器的最小大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用几个布局不同的JPanel用Java编写一个独立的应用程序,以安排用户界面. 现在我的问题是,当我拿起窗口的上侧(它位于边框布局中的面板,该面板位于另一个使用边框布局的面板内)时,试图添加一个扩展面板的类是为了在面板上绘制图标我窗口的顶部(在面板上绘制).问题在于布局正在切割图标的一部分,或者换句话说,是将面板最小化到一定的尺寸. 我试图改变为flowlayout和其他人,但确实是一样的... 所以我想问一下是否有一个选项可以告诉布局容器(面板或其他容器)的大小不能小于给定的大小?其他建议也将有所帮助,但请记住,我正在尝试将带有微小更改的图标添加到GUI.

i am writing a stand alone app in java using a couple of JPanel with different layouts in order to arrange the user interface. now my problem is that when i take the upper side of the window (its a pannel in a border layout which is inside another panel which using border layout),im tring to add a class that extends panel is order to paint an icon on the top of my window (draw on the panel) . the problem is that the layout is cuting a part of the icon, or in other words, minimazing the panel to a certain size. i tried changing to flowlayout and others but is does the same... so i wanted to ask if an option which tells the layout that a container (panel or others) can not be set to a size lower then a given size exists? other suggestions will allso help but keep in mind that i am tring to add the icon with mininal change to the GUI.

感谢您阅读并提供帮助 莫什

thanks for reading this and helping moshe

推荐答案

容器可以容纳JComponent的MinimumSize,简单的示例

Container can hold MinimumSize for JComponent, simple example,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomComponent extends JFrame {

    private static final long serialVersionUID = 1L;

    public CustomComponent() {
        setTitle("Custom Component Graphics2D");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponents());//
        pack();
        // enforces the minimum size of both frame and component
        setMinimumSize(getSize());
        setVisible(true);
    }

    public static void main(String[] args) {
        CustomComponent main = new CustomComponent();
        main.display();
    }
}

class CustomComponents extends JPanel {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(100, 100);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        int margin = 10;
        Dimension dim = getSize();
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
    }
}

这篇关于使用Java布局定义容器的最小大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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