微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的按钮在布局中没有变宽?

如何解决为什么我的按钮在布局中没有变宽?

通常,当我看到 Windows 中放置的按钮并且该窗口被拉长时,窗口上的按钮也会被拉长。

这是一个示例程序。

import java.awt.*;  
import javax.swing.*;  
  
public class MultiLayoutDemo
{  
    JFrame f;  
    MultiLayoutDemo()
    {
        f=new JFrame();  
          
        f.setLayout(new BorderLayout());
                    
        ///Top Row
        FlowLayout layout = new FlowLayout();
        JPanel jp1 = new JPanel(layout);
        jp1.add(buildLayout1());
        jp1.add(buildLayout2());
        jp1.add(buildLayout3());
        f.add(jp1,BorderLayout.norTH);

        ///Bottom Row
        FlowLayout bottomLayout = new FlowLayout();
        JPanel jp2 = new JPanel(bottomLayout);
        jp2.add(buildLayout4());
        
        f.add(jp2,BorderLayout.soUTH);

        f.setSize(300,300);  
        f.setVisible(true);  
    }
    
    private JPanel buildLayout1() 
    {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);
        
        jp.add(new JLabel("Name"));
        jp.add(new JButton("First"));
        jp.add(new JButton("Last"));
        
        return jp;
    }

    private JPanel buildLayout2() 
    {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);
        
        jp.add(new JLabel("Age"));
        jp.add(new JButton("Young "));
        jp.add(new JButton("Old"));
        jp.add(new JButton("Really Old"));
        
        return jp;
    }

    private JPanel buildLayout3() 
    {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);
        
        jp.add(new JLabel("Location"));
        jp.add(new JButton("north Side "));
        jp.add(new JButton("South Side"));
        
        return jp;
    }

    private JPanel buildLayout4() 
    {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);
        
        jp.add(new JLabel("Hobbies"));
        jp.add(new JButton("Jogging"));
        jp.add(new JButton("Skydiving"));
        jp.add(new JButton("Racing"));
        jp.add(new JButton("TV"));
        jp.add(new JButton("N/A"));
        
        return jp;
    }

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

当前结果产生了这一点。

enter image description here

所需的结果是按钮展开如下所示,但显然文本不会变形。

enter image description here

我应该使用不同的布局吗??? 是否有设置允许动态调整按钮大小的属性

解决方法

我使用了您的一些代码来创建以下 GUI。

Stretch Flow Layout

如果您调整 WindowListener 的大小,我没有添加 JPanels 来调整 JFrame。我把它留给你作为练习。

我重新排列了代码,使其通常从上到下阅读,就像一篇文章。一般来说,最抽象的代码在前,详细的代码在后。这种顺序使阅读代码变得更加简单。

我添加了对 SwingUtilities invokeLater 方法的调用。此方法可确保在 Event Dispatch Thread 上创建和执行 Swing 组件。

我将 JFrame 方法与 JPanel 方法分开。这使代码更易于理解。

stretchPanel 方法是扩展较小的 JPanel 的代码。我在 JFrame pack 方法之后调用此方法,以便 Swing 进行所有布局计算。我计算较大的 JPanel 和较小的 JPanel 之间的差异,并将该差异分布在 Swing 组件之间。

正如我在评论中所说,我认为这种布局提供了糟糕的用户体验。您还必须自己处理 JFrame 调整大小,这使得这种类型的 Swing 代码非常脆弱。

这是完整的可运行代码。愿上帝怜悯你的灵魂。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StretchFlowLayoutExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new StretchFlowLayoutExample());
    }
    
    private JPanel lowerPanel;
    private JPanel upperPanel;

    @Override
    public void run() {
        JFrame frame = new JFrame("Stretch Flow Layout");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(),BorderLayout.CENTER);
        
        frame.pack();
        stretchPanel(lowerPanel,upperPanel);
        
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5,5,5));
        
        upperPanel = createUpperPanel();
        lowerPanel = createLowerPanel();
        
        panel.add(upperPanel);
        panel.add(lowerPanel);
        
        Dimension u = upperPanel.getPreferredSize();
        Dimension l = lowerPanel.getPreferredSize();
        panel.setPreferredSize(new Dimension(u.width,u.height + l.height + 25));
        
        return panel;
    }
    
    private JPanel createUpperPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5,5));
        panel.add(buildLayout1());
        panel.add(buildLayout2());
        panel.add(buildLayout3());
        
        return panel;
    }
    
    private JPanel buildLayout1() {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);

        jp.add(new JLabel("Name"));
        jp.add(new JButton("First"));
        jp.add(new JButton("Last"));

        return jp;
    }

    private JPanel buildLayout2() {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);

        jp.add(new JLabel("Age"));
        jp.add(new JButton("Young "));
        jp.add(new JButton("Old"));
        jp.add(new JButton("Really Old"));

        return jp;
    }

    private JPanel buildLayout3() {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);

        jp.add(new JLabel("Location"));
        jp.add(new JButton("North Side "));
        jp.add(new JButton("South Side"));

        return jp;
    }
    
    private JPanel createLowerPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5,5));
        panel.add(buildLayout4());
        
        return panel;
    }
    
    private JPanel buildLayout4() {
        FlowLayout flowLayout = new FlowLayout();
        JPanel jp = new JPanel(flowLayout);

        jp.add(new JLabel("Hobbies"));
        jp.add(new JButton("Jogging"));
        jp.add(new JButton("Skydiving"));
        jp.add(new JButton("Racing"));
        jp.add(new JButton("TV"));
        jp.add(new JButton("N/A"));

        return jp;
    }
    
    private void stretchPanel(JPanel smallPanel,JPanel largePanel) {
        Dimension l = largePanel.getSize();
        Dimension s = smallPanel.getSize();
        
        Component[] components = smallPanel.getComponents();
        components = ((JPanel) components[0]).getComponents();
        int count = components.length;
        int width = l.width - s.width;
        int extra = width / count;
        int remainder = width % count;
        
        for (int index = 0; index < count; index++) {
            Component component = components[index];
            Dimension d = component.getSize();
            
            int newWidth = d.width + extra;
            if (remainder > 0) {
                newWidth++;
                remainder--;
            }

            component.setSize(new Dimension(newWidth,d.height));
            component.setPreferredSize(new Dimension(newWidth,d.height));
        }
 
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。