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

如何使用默认 FlowLayout 在 JPanel 中居中 JButton?

如何解决如何使用默认 FlowLayout 在 JPanel 中居中 JButton?

我正在尝试使用认 FlowLayout 在 JPanel 中居中放置 JButton。这是我的代码

import javax.swing.*;
import java.awt.*;
public class CoinFlip {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Coin Flip");
        frame.setSize(500,500);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        JButton coinFlipButton = new JButton("Flip Coin");
        coinFlipButton.setSize(100,100);
        JPanel coinFlipPanel = new JPanel();
        coinFlipPanel.setSize(500,100);
        coinFlipPanel.setBackground(Color.GRAY);
        coinFlipPanel.add(coinFlipButton);
        JTextArea coinFlipResults = new JTextArea();
        coinFlipResults.setSize(500,200);
        frame.getContentPane().add(coinFlipPanel);
        frame.getContentPane().add(coinFlipResults);
        frame.setVisible(true);
    }
}

如何将 JButton (coinFlipButton) 居中放置在 JPanel (coinFlipPanel) 中?我曾尝试使用 FlowLayout.CENTER,但这会使按钮的大小增加,从而占据所有空间。

解决方法

我仔细检查了文档,FlowLayout 确实有一个选项可以将其组件居中,所以我对此有过错。但除此之外,您只需要阅读有关各种组件及其布局的教程,并进行一些实验。

非常重要的一点是,默认 JFrame 的 jframe.getContentPane().add() 使用仅允许一个组件的布局。如果您想要多个组件,则必须使用第二个组件,例如带有 Grid 或 Flow 布局的 JPanel。通过两次调用该方法,您只是替换了之前的组件。请阅读有关 JFrames 和 BorderLayout 的教程。

无论如何,我制作了一个快速程序,可以执行您想要的操作。我将您的文本区域添加到 ScrollPane,因为这是处理 JFrame 的主要内容窗格的另一种方式。当我添加带有按钮的 JPanel 时,我将它添加到 BorderLayout 的 SOUTH 部分,因为这就是使用 BorderLayout 显示多个组件的方式。 (再次教程。)

public class JFrameBorderWithButton {
   public static void main( String[] args ) {
      SwingUtilities.invokeLater( JFrameBorderWithButton::newGui );
   }
   private static void newGui() {
      JFrame frame = new JFrame();
      
      JTextArea text = new JTextArea(5,40);
      JScrollPane scroll = new JScrollPane( text );
      frame.add( scroll );
      JPanel panel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
      JButton button = new JButton( "Press Me" );
      panel.add(  button );
      frame.add( panel,BorderLayout.SOUTH );
      
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.pack();
      frame.setLocationRelativeTo( null );
      frame.setVisible( true );
   }
}
,

根据我的经验,简单的布局很少值得。使用类似 GridBagLayout 的东西

public static void main(String [] args) throws IOException {
    JFrame frame = new JFrame("Coin Flip");
    JTextArea textArea = new JTextArea();
    JScrollPane textScroll = new JScrollPane(textArea);
    Action flipAction = new AbstractAction("Flip Coin!") {
        private Random random = new Random();
        
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!textArea.getText().isEmpty()) {
                textArea.append(System.lineSeparator());
            }
            
            if (random.nextBoolean()) {
                textArea.append("Heads!");
            } else {
                textArea.append("Tails!");
            }
        }
    };
            
    JButton flipButton = new JButton(flipAction);
    
    frame.setLayout(new GridBagLayout());
    frame.setPreferredSize(new Dimension(175,500));
    frame.add(flipButton,new GridBagConstraints(0,1,1.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(4,4,4),0));
    frame.add(textScroll,3,GridBagConstraints.BOTH,0));
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

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