如何更改 BorderLayout 中的 JPanel 大小? Java 摇摆

问题描述 投票:0回答:0

我想为菜单栏添加一个最大化/最小化按钮。我使用 JLayeredPane 作为 contentPage,使用 BorderLayout 作为其布局。我为菜单栏内的最大/最小按钮添加了一个 JButton(菜单栏是绝对布局)并尝试使用 menuBar.setPreferredSize 但它不会改变它的大小。

        
        layeredPane.setLayer(menuBar, 1);
        menuBar.setOpaque(false);
        menuBar.setBackground(Color.CYAN);
        menuBar.setPreferredSize(new Dimension(75, 10));
        layeredPane.add(menuBar, BorderLayout.WEST);
        menuBar.setLayout(null);

                JButton menuBtn = new JButton();
        menuBtn.setText("BTN");
        menuBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                menuBar.setPreferredSize(new Dimension(200,10));
                pack();
            }
        });
        menuBtn.addMouseListener(new MouseAdapter() {
            
            @Override
            public void mouseEntered(MouseEvent e) {
                menuBtn.setBackground(Color.red);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                menuBtn.setBackground(Color.blue);
            }
        });
        menuBtn.setBounds(10, 38, 55, 44);
        menuBar.add(menuBtn);
        

这是框架的图像(未单击 menuBtn)

menuBtn点击后的图片

我试过拿包();声明,它不会改变任何东西。什么都不会发生。它不会改变任何事情。但在单击窗口最大化按钮后,它会改变菜单栏的大小。我的代码有什么问题?

package Project;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

    


    private JLayeredPane layeredPane = new jPanelGradient();
    private JPanel titleBar = new JPanel();
    private JPanel menuBar = new JPanel();
    private JPanel sidePanel = new JPanel();
    private final JPanel bottomPanel = new JPanel();
    private final JPanel mainPanel = new RoundedPanel(20, Color.decode("#cdcccb"));
    private Image maxbtn = new ImageIcon(MainFrame.class.getResource("/Project/icons/maxBtn.png")).getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
    private Image minbtn = new ImageIcon(MainFrame.class.getResource("/Project/icons/minBtn.png")).getImage().getScaledInstance(23, 23, Image.SCALE_SMOOTH);
    private final JPanel ExitPanel = new JPanel();
    private final JLabel lblExitbtn_1 = new JLabel("X");
    private final JButton MinBtn = new JButton("");
    private final JButton MaxBtn = new JButton("");
    private final JPanel toolBarSidePanel = new JPanel();
    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public MainFrame() {
        setResizable(true);
        setUndecorated(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(0, 0, 1200, 700);
        setShape(new RoundRectangle2D.Double(0, 0, 1200, 700, 15, 15));
        setContentPane(layeredPane);
        layeredPane.setLayout(new BorderLayout(0, 10));
        setLocationRelativeTo(null);
        
        layeredPane.setLayer(titleBar, 1);
        titleBar.setPreferredSize(new Dimension(10, 23));
        titleBar.setBackground(Color.decode("#042a45"));
        titleBar.setBounds(new Rectangle(0, 0, 100, 100));
        layeredPane.add(titleBar, BorderLayout.NORTH);
        titleBar.setLayout(new BorderLayout(0, 0));
        
        JLabel lblTitle = new JLabel("LearnLog");
        lblTitle.setForeground(Color.WHITE);
        lblTitle.setFont(new Font("Lucida Sans Unicode", Font.PLAIN, 13));
        titleBar.add(lblTitle, BorderLayout.CENTER);
        
        JPanel ToolBar = new JPanel();
        ToolBar.setOpaque(false);
        ToolBar.setPreferredSize(new Dimension(105, 10));
        ToolBar.setLayout(null);
        ToolBar.setBackground(new Color(4, 42, 69));
        titleBar.add(ToolBar, BorderLayout.EAST);
        ExitPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                System.exit(0);
            }
            @Override
            public void mouseEntered(MouseEvent e) {
                ExitPanel.setBackground(Color.red);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                ExitPanel.setBackground(new Color(4, 42, 69));
            }
        });
        ExitPanel.setLayout(null);
        ExitPanel.setBackground(new Color(4, 42, 69));
        ExitPanel.setBounds(70, -1, 35, 23);
        
        ToolBar.add(ExitPanel);
        lblExitbtn_1.setHorizontalTextPosition(SwingConstants.CENTER);
        lblExitbtn_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblExitbtn_1.setForeground(Color.WHITE);
        lblExitbtn_1.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
        lblExitbtn_1.setBounds(0, 6, 30, 13);
        ExitPanel.add(lblExitbtn_1);
        
        MinBtn.setOpaque(false);
        MinBtn.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
        MinBtn.setFocusPainted(false);
        MinBtn.setContentAreaFilled(false);
        MinBtn.setBorderPainted(false);
        MinBtn.setBorder(null);
        MinBtn.setAlignmentY(0.0f);
        MinBtn.setAlignmentX(0.5f);
        MinBtn.setBounds(0, 1, 35, 23);
        MinBtn.setIcon(new ImageIcon(minbtn));
        MinBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MinBtnActionPerformed(e);
            }
        });
        MinBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                MinBtn.setBackground(Color.red);
                MinBtn.setOpaque(true);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                MinBtn.setOpaque(false);
            }
        });
        ToolBar.add(MinBtn);

        MaxBtn.setOpaque(false);
        MaxBtn.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
        MaxBtn.setFocusPainted(false);
        MaxBtn.setContentAreaFilled(false);
        MaxBtn.setBorderPainted(false);
        MaxBtn.setBorder(null);
        MaxBtn.setAlignmentY(0.0f);
        MaxBtn.setAlignmentX(0.5f);
        MaxBtn.setBounds(35, 1, 35, 23);
        MaxBtn.setIcon(new ImageIcon(maxbtn));
        MaxBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MaxBtnActionPerformed(e);
            }
        });
        MaxBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                MaxBtn.setBackground(Color.red);
                MaxBtn.setOpaque(true);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                MaxBtn.setOpaque(false);
            }
        });
        
        ToolBar.add(MaxBtn);
        toolBarSidePanel.setOpaque(false);
        
        titleBar.add(toolBarSidePanel, BorderLayout.WEST);
        
        layeredPane.setLayer(menuBar, 1);
        menuBar.setOpaque(false);
        menuBar.setBackground(Color.CYAN);
        menuBar.setPreferredSize(new Dimension(75, 10));
        layeredPane.add(menuBar, BorderLayout.WEST);
        menuBar.setLayout(null);
        
        JButton menuBtn = new JButton();
        menuBtn.setText("BTN");
        menuBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                menuBar.setPreferredSize(new Dimension(200,10));
                //pack();
            }
        });
        menuBtn.addMouseListener(new MouseAdapter() {
            
            @Override
            public void mouseEntered(MouseEvent e) {
                menuBtn.setBackground(Color.red);
            }
            @Override
            public void mouseExited(MouseEvent e) {
                menuBtn.setBackground(Color.blue);
            }
        });
        menuBtn.setBounds(10, 38, 55, 44);
        menuBar.add(menuBtn);
        
        sidePanel.setOpaque(false);
        layeredPane.setLayer(sidePanel, 1);
        layeredPane.add(sidePanel, BorderLayout.LINE_END);
        bottomPanel.setPreferredSize(new Dimension(10, 3));
        bottomPanel.setOpaque(false);
        
        layeredPane.add(bottomPanel, BorderLayout.SOUTH);
        mainPanel.setOpaque(false);
        
        layeredPane.add(mainPanel, BorderLayout.CENTER);
        mainPanel.setLayout(null);
        

    }
    
    private void MinBtnActionPerformed(ActionEvent evt) {
        this.setExtendedState(MainFrame.ICONIFIED); 
    }
    
    private void MaxBtnActionPerformed(ActionEvent evt) {
        if (this.getExtendedState() != MainFrame.MAXIMIZED_BOTH) {
            this.setExtendedState(MainFrame.MAXIMIZED_BOTH); 
            setShape(null);
        } else { 
            this.setExtendedState(MainFrame.NORMAL);
            setShape(new RoundRectangle2D.Double(0, 0, 1200, 700, 15, 15));
        }
     }
    

}

class RoundedPanel extends JPanel {
    private Color backgroundColor;
    private int cornerRadius = 15;
    
    public RoundedPanel(int radius) {
        super();
        cornerRadius = radius;
    }

    public RoundedPanel(int radius, Color bgColor) {
        super();
        cornerRadius = radius;
        backgroundColor = bgColor;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension arcs = new Dimension(cornerRadius, cornerRadius);
        int width = getWidth();
        int height = getHeight();
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        //Draws the rounded panel with borders.
        if (backgroundColor != null) {
            graphics.setColor(backgroundColor);
        } else {
            graphics.setColor(getBackground());
        }
        graphics.fillRoundRect(0, 0, width-1, height-1, arcs.width, arcs.height); //paint background
        graphics.setColor(getForeground());
        //graphics.drawRoundRect(0, 0, width-1, height-1, arcs.width, arcs.height); //paint border
    }
}

class RoundBorderedPanel extends JPanel {
    private Color backgroundColor;
    private int cornerRadius = 15;
    
    public RoundBorderedPanel(int radius) {
        super();
        cornerRadius = radius;
    }

    public RoundBorderedPanel(int radius, Color bgColor) {
        super();
        cornerRadius = radius;
        backgroundColor = bgColor;
    }
    
    public void setBackground(Color bgColor) {
        backgroundColor = bgColor;
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension arcs = new Dimension(cornerRadius, cornerRadius);
        int width = getWidth();
        int height = getHeight();
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //Draws the rounded panel with borders.
        if (backgroundColor != null) {
            graphics.setColor(backgroundColor);
        } else {
            graphics.setColor(getBackground());
        }
        graphics.fillRoundRect(0, 1, width-2, height-2, arcs.width, arcs.height); //paint background
        graphics.setColor(getForeground());
        graphics.drawRoundRect(0, 1, width-2, height-2, arcs.width, arcs.height); //paint border
    }
}

class jPanelGradient extends JLayeredPane {
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        int width = getWidth();
        int height = getHeight();
        
        Color color1 = new Color(14,64,91,255);
        Color color2 = new Color(21,82,116,200);
        GradientPaint gp = new GradientPaint(0,0,color1,150,height,color2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, width, height);
        
    }
}

java swing resize jpanel border-layout
© www.soinside.com 2019 - 2024. All rights reserved.