简体   繁体   English

Java Swing响应式绝对定位

[英]Java Swing Responsive Absolute Positioning

I am newby to java swing gui and I cannot figure out this problem Lets assume I Have a Jframe size 100,100 I have 4 button positions 我是java swing gui的新手,我无法弄清楚这个问题,假设我的Jframe大小为100,100,我有4个按钮位置

        left top widht height
button1 0    0   10    10 // left up
button2 90   0   10    10 // right up
button3 45   45  10    10 // middle
button4 0    90  10    10 // left down
button5 90   90  10    10 // right down

All of left top width height sized scaled from 100,100. 所有左上角宽度高度的大小均从100,100缩放。

I need using Absolute positioning (Becaouse my real situation different left,top,width,height) But responsiveness I consider maybe I can use factors to multiple the left,right,width,height values 我需要使用绝对定位(因为我的实际情况是不同的left,top,width,height),但我认为响应能力可能是我可以使用多个left,right,width,height值的因子

For example after running gui if I change frame to 400,200 then new positions should be 例如,运行gui后,如果我将框架更改为400,200,则应将新位置

button1 0    0   40   20 // left up
button2 360  0   40   20 // right up
button3 180  90  40   20 // middle
button4 0    180 40   20 // left down
button5 360  180 40   20 // right down

x factor = 400/100=4 and y factor = 200/100=2 x因子= 400/100 = 4和y因子= 200/100 = 2

How can I listen Jframe's changes from 100,100 to 400,200 and develop above approach. 我该如何聆听Jframe从100,100到400,200的变化并开发上述方法。 Any information appreciated. 任何信息表示赞赏。

EDIT 编辑

I get left top width height from different service and my example could be like 我从其他服务获得顶部宽度的高度,我的示例可能像

        left top widht height
button1 01   04  11    14  
button2 91   0   10    10    
button3 44   45  9     14  
button4 0    90  10    10 
button5 90   90  1     1 

I need using Absolute positioning 我需要使用绝对定位

When ever you think you need "absolute positioning", you don't. 每当您认为需要“绝对定位”时,就不需要。 Seriously, I've never had a need to use one which wasn't solved through some other means, either an existing layout or a custom layout. 认真地说,我从来不需要使用一种无​​法通过其他方式(现有布局或自定义布局)解决的方法。

The layout management API is designed to do exactly what you're asking, better to make use of it and save your hair. 布局管理API旨在完全满足您的要求,更好地利用它并节省头发。

This is a pretty simple example using GridBagLayout and yes, if the buttons had different heights, it would still cope 这是一个使用GridBagLayout的非常简单的示例,是的,如果按钮的高度不同,它仍然可以应对

小 大

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            // Common properties...
            gbc.weightx = 0.5;
            gbc.weighty = 0.333;
            // Top/left
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            add(new JButton("Top left is all mine"), gbc);
            // Top/right
            gbc.gridx = 2;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTHEAST;
            add(new JButton("Top right mine, keep out"), gbc);

            // middle
            gbc.gridx = 1;
            gbc.gridy = 1;
            gbc.anchor = GridBagConstraints.CENTER;
            add(new JButton("My precious"), gbc);

            // bottom/left
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.anchor = GridBagConstraints.SOUTHWEST;
            add(new JButton("Got this space covered"), gbc);
            // bottom/right
            gbc.gridx = 2;
            gbc.gridy = 2;
            gbc.anchor = GridBagConstraints.SOUTHEAST;
            add(new JButton("Why is every one so unfriendly?"), gbc);
        }

    }

}

Got high buttons... 高按钮...

大按钮

Got that covered, automatically 自动覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM