Java Swing GUI BorderLayout:组件的位置 [英] Java Swing GUI BorderLayout: Location of components

查看:127
本文介绍了Java Swing GUI BorderLayout:组件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我在玩一个简单的GUI示例:

  import java.awt.BorderLayout;导入java.awt.Color;导入java.awt.Graphics;导入java.awt.Graphics2D;导入java.awt.geom.Rectangle2D;导入javax.swing.JButton;导入javax.swing.JFrame;导入javax.swing.JPanel;导入javax.swing.WindowConstants;公共类DrawTest {class DrawingPanel扩展JPanel {私人Rectangle2D形状;公共DrawingPanel(Rectangle2D形状){this.shape = 形状;}public void paintComponent(Graphics g){Graphics2D g2D =(Graphics2D)g;super.paintComponent(g2D);g2D.setColor(new Color(31,21,1));g2D.fill(shape);}}公共无效draw(){JFrame框架=新的JFrame();Rectangle2D shape = new Rectangle2D.Float();最终的DrawingPanel工程图=新的DrawingPanel(形状);shape.setRect(0,0,400,400);frame.getContentPane().add(BorderLayout.NORTH,new JButton("TestN"));frame.getContentPane().add(BorderLayout.SOUTH,new JButton("TestS")));frame.getContentPane().add(BorderLayout.EAST,new JButton("TestE")));frame.getContentPane().add(BorderLayout.WEST,new JButton("TestW")));frame.getContentPane().add(BorderLayout.CENTER,图);frame.pack();frame.setSize(500,500);frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.setVisible(true);}}公共类DrawMain {公共静态void main(String [] args){DrawTest测试=新的DrawTest();test.draw();}} 

如预期的那样,此代码将生成一个框架,该框架的中心是矩形,周围是按钮.但是,如果我这样更改代码:

  frame.getContentPane().add(BorderLayout.NORTH,图);frame.getContentPane().add(BorderLayout.SOUTH,new JButton("TestS")));frame.getContentPane().add(BorderLayout.EAST,new JButton("TestE")));frame.getContentPane().add(BorderLayout.WEST,new JButton("TestW")));frame.getContentPane().add(BorderLayout.CENTER,new JButton("TestC"))); 

"TestC"按钮在中间有一个很大的区域,而矩形没有足够的空间.如果我删除其他按钮(TestS,TestE,TestW),这甚至是正确的:我得到了一个巨大的TestC按钮,顶部有一个很小的矩形部分(甚至不是比例矩形).

为什么矩形在顶部(北)绘制时没有获得足够的空间,但是在CENTER处绘制却得到了?

解决方案

DrawingPanel 应该 @Override getPreferredSize()以返回适当的大小.

然后,布局管理器会将首选大小作为提示.一些布局管理器将根据布局和约束的逻辑来扩展组件的高度或宽度.例如. BorderLayout 会将 PAGE_START / PAGE_END 中的组件拉伸到内容窗格的宽度,然后将 LINE_START / LINE_END 到其中任何一个或 CENTER 的最高高度.一个 GridBagLayout OTOH将完全隐藏/删除一个组件,该组件的空间不足以显示首选大小的 ,这就是'pack'出现的地方.

因此将 frame.setSize(500,500); (这并不比猜测更好)更改为 frame.pack(); ,这将使框架的最小尺寸需要,以便显示其中包含的组件.

I'm new to Java and I'm playing around with a simple GUI example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class DrawTest {

    class DrawingPanel extends JPanel {

        private Rectangle2D shape;

        public DrawingPanel(Rectangle2D shape) {
            this.shape = shape;
        }

        public void paintComponent(Graphics g) {

            Graphics2D g2D = (Graphics2D) g;            
            super.paintComponent(g2D);  
            g2D.setColor(new Color(31, 21, 1));
            g2D.fill(shape);

        }

    }


    public void draw() {
        JFrame frame = new JFrame();
        Rectangle2D shape = new Rectangle2D.Float();
        final DrawingPanel drawing = new DrawingPanel(shape);

        shape.setRect(0, 0, 400, 400);
        frame.getContentPane().add(BorderLayout.NORTH, new JButton("TestN"));
        frame.getContentPane().add(BorderLayout.SOUTH, new JButton("TestS"));
        frame.getContentPane().add(BorderLayout.EAST, new JButton("TestE"));
        frame.getContentPane().add(BorderLayout.WEST, new JButton("TestW"));
        frame.getContentPane().add(BorderLayout.CENTER, drawing);
        frame.pack();
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);  
    }
}

public class DrawMain {
    public static void main(String[] args) {
        DrawTest test = new DrawTest();
        test.draw();

    }
}

As expected, this code produces a frame with the rectangle at the centre and buttons around it. However, if I change the code like this:

        frame.getContentPane().add(BorderLayout.NORTH, drawing);
        frame.getContentPane().add(BorderLayout.SOUTH, new JButton("TestS"));
        frame.getContentPane().add(BorderLayout.EAST, new JButton("TestE"));
        frame.getContentPane().add(BorderLayout.WEST, new JButton("TestW"));
        frame.getContentPane().add(BorderLayout.CENTER, new JButton("TestC"));

the "TestC" button gets a huge area in the middle while the rectangle doesn't get enough space. This is even true if I remove the other buttons (TestS, TestE, TestW): I get a huge TestC button and a tiny part of the rectangle (even not the scaled rectangle) at the top.

Why doesn't the rectangle get enough space when it's drawn at the top (NORTH) but does get it when it's drawn at the CENTER?

解决方案

The DrawingPanel should @Override getPreferredSize() to return an appropriate size.

The layout manager will then take that preferred size as a hint. Some layout managers will expand a component's height or width according to the logic of the layout and constraint. E.G. a BorderLayout will stretch components in the PAGE_START / PAGE_END to the width of the content pane, and LINE_START / LINE_END to the height of the tallest of either of those, or the CENTER. A GridBagLayout OTOH will completely hide / remove a component for which there is not enough space to display it at the preferred size, and that's where 'pack' comes in.

So change frame.setSize(500,500); (which is no better than a guess) to frame.pack();, which will make frame the minimum size it needs to be, in order to display the components it contains.

这篇关于Java Swing GUI BorderLayout:组件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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