加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 制作JPanel广场

发布时间:2020-12-15 05:00:34 所属栏目:Java 来源:网络整理
导读:如果我有一个带有多个子组件的JPanel,我怎么能这样做,以便JPanel保持正方形,尽管它的父级是如何调整大小的?我已经尝试了以下代码的变体,但它不会导致子组件也是正方形. public void paint(Graphics g){ if(this.isSquare()){ Dimension d = this.getSize();
如果我有一个带有多个子组件的JPanel,我怎么能这样做,以便JPanel保持正方形,尽管它的父级是如何调整大小的?我已经尝试了以下代码的变体,但它不会导致子组件也是正方形.

public void paint(Graphics g){
    if(this.isSquare()){
        Dimension d = this.getSize();
        if(d.height > d.width){
            this.setSize(d.width,d.width);
        } else {
            this.setSize(d.height,d.height);
    }
    super.paint(g);
}

这是一个SSCCE.

包含父母:

import javax.swing.JFrame;

public class TestFrame extends JFrame{

    public TestFrame(){
        this.add(new TestPanel());
    }

    public static void main(String[] args){
        TestFrame tf = new TestFrame();

        tf.setSize(500,500);
        tf.setVisible(true);
    }
}

什么应该是方形JPanel:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;


public class TestPanel extends JPanel{
    private boolean isSquare;

    public TestPanel(){
        this.setSquare(true);
        this.setLayout(new BorderLayout());

        JLabel panel1 = new JLabel();
        panel1.setBorder(new LineBorder(Color.RED,4));
        panel1.setBackground(Color.CYAN);

        JLabel panel2 = new JLabel();
        panel2.setBorder(new LineBorder(Color.BLUE,4));
        panel2.setBackground(Color.CYAN);


        this.setBorder(new LineBorder(Color.GREEN,4));
        this.setBackground(Color.CYAN);

        this.add(panel1,BorderLayout.WEST);
        this.add(panel2,BorderLayout.EAST);
    }

    public void paint(Graphics g){
        if(this.isSquare()){
            Dimension d = this.getSize();
            if(d.height > d.width){
                this.setSize(d.width,d.width);
            } else {
                this.setSize(d.height,d.height);
            }
            super.paint(g);
        }
    }

    private boolean isSquare() {
        return isSquare;
    }

    private void setSquare(boolean isSquare) {
        this.isSquare = isSquare;
    }
}

解决方法

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SoSquare {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                // A single component added to a GBL with no constraint
                // will be centered.
                JPanel gui = new JPanel(new GridBagLayout());

                gui.setBackground(Color.WHITE);

                SquarePanel p = new SquarePanel();
                p.setBackground(Color.red);
                gui.add(p);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloSEOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                f.setSize(400,100);
                // should be done last,to avoid flickering,moving,// resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

/**
 * A square panel for rendering. NOTE: To work correctly,this must be the only
 * component in a parent with a layout that allows the child to decide the size.
 */
class SquarePanel extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        Container c = getParent();
        if (c != null) {
            d = c.getSize();
        } else {
            return new Dimension(10,10);
        }
        int w = (int) d.getWidth();
        int h = (int) d.getHeight();
        int s = (w < h ? w : h);
        return new Dimension(s,s);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读