java - 移动 JButton

标签 java swing jbutton

移动按钮的最佳方式是什么,使它们位于彼此下方而不是彼此旁边(见下图)?

enter image description here

该类的代码如下。 Main 方法位于不同的类中。

package guiplay;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class MainGUI extends JFrame {

  private JButton openReportSelection = new JButton("Open Report Viewer");
  private JButton closeButton = new JButton("Close Program");



    private JButton getCloseButton(){
        return closeButton;
    }   
    private JButton getOpenReportSelection(){
        return openReportSelection;
}

    public MainGUI(){
        mainInterface(); 
    }

    private void mainInterface(){          
        setTitle("Program Information Application");   
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel centerPanel = new JPanel(new FlowLayout());        
        centerPanel.add(openReportSelection);
        openReportSelection.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JFrame reports = new JFrame();
                new ReportGUI();
            }
        });  
        centerPanel.add(closeButton);       
        getCloseButton().addActionListener(new Listener());
        add(centerPanel, BorderLayout.CENTER);
        setSize(700,200);
        setVisible(true);            
    }




}

最佳答案

您可以使用BoxLayout,因为它水平或垂直对齐所有元素。只需将 BoxLayout 的轴设置为 BoxLayout.Y_AXIS

BoxLayout with axis set to BoxLayout.Y_AXIS

示例:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JButton;

public class BoxLayoutExample extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BoxLayoutExample frame = new BoxLayoutExample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public BoxLayoutExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 180, 150);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JButton btnOpenReportViewer = new JButton("Open Report Viewer");
        contentPane.add(btnOpenReportViewer);

        JButton btnCloseProgram = new JButton("Close Program");
        contentPane.add(btnCloseProgram);
    }

}

如果要控制大小以使它们彼此相似,可以通过将 JFrame 的内容 Pane 设置为 GridLayout 来使用网格布局:

contentPane.setLayout(new GridLayout(0, 1, 0, 0)); // the value of 1 here means 1 column

GridLayout with buttons that are the same size

关于java - 移动 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24027629/

相关文章:

java - 在 Java 计算器的按钮按下时获得声音的最佳方法?

java - 如何获得不重复的随机整数?

java - Scala 相当于 JavaScript 数组扩展

java - 在每 x 位数字后拆分字符串

java - 这是什么方法途径?

java - 如何在Java中实现搜索 token (如mac Lion中)?

java - 如何在按 10 次时禁用 jbutton

java - Belisha Beacon 程序中的 JButton 查询

java - NullPointerException:尝试对空对象引用调用接口(interface)方法 'int java.util.List.size()'

java - 使用 Java Swing 进行纸牌游戏中的视觉反馈