简体   繁体   English

移动JButton

[英]Moving JButtons

What would be the best way for me to move the buttons so they are under each other instead of beside each other (see image below)? 对我来说,移动按钮使其处于彼此下方而不是彼此相邻的最佳方式是什么(请参见下图)?

在此处输入图片说明

The code for this class is as follows. 此类的代码如下。 The Main method is in a different class. 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);            
    }




}

Don't put the JButtons in a container that uses FlowLayout but rather one that uses another layout that allows stacking of components. 不要将JButton放在使用FlowLayout的容器中,而要放在使用允许组件堆叠的另一种布局的容器中。 A GridLayout comes to mind if the buttons are to be the same size, or if they need to be different sizes, a BoxLayout. 如果按钮要具有相同的大小,或者如果它们需要具有不同的大小,则可以想到一个GridLayout。

Check out the Layout Manager Tutorial . 查看Layout Manager教程

You can use a BoxLayout as it aligns all elements either horizontally or vertically. 您可以使用BoxLayout因为它可以水平或垂直对齐所有元素。 Simply set BoxLayout 's axis to BoxLayout.Y_AXIS . 只需将BoxLayout的轴设置为BoxLayout.Y_AXIS

轴设置为BoxLayout.Y_AXIS的BoxLayout

Example: 例:

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);
    }

}

If you want to control the size so that they are similar to each other, you can use a grid layout by setting the JFrame 's content pane to a GridLayout : 如果要控制大小以使它们彼此相似,可以通过将JFrame的内容窗格设置为GridLayout来使用网格布局:

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

具有相同大小的按钮的GridLayout

You could try using a BoxLayout instead of a FlowLayout. 您可以尝试使用BoxLayout代替FlowLayout。 In that case, you could have: 在这种情况下,您可能会:

JPanel centerPanel = new JPanel(new BoxLayout());  
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); // Y_AXIS will cause the components to be added vertically
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
centerPanel.setMaximumSize(new Dimension(100, 60)); // Set the maximum horizontal and vertical distances used, as BoxLayouts expand to fill the provided area

Or as Hovercraft said, you could use a GridLayout, in which case you would specify it as follows: 或者像气垫船所说的那样,您可以使用GridLayout,在这种情况下,您可以按以下方式指定它:

JPanel centerPanel = new JPanel(new GridLayout(1,0); // The "0" parameter specifies as many rows as needed, but only one column
centerPanel.add(openReportSelection);
centerPanel.add(closeButton);
centerPanel.setMaximumSize(new Dimension(100, 60)); // GridLayouts will also expand to fill the entire area, so you'll probably want some size parameters.

You could also see this link for more on BoxLayouts, or this link for more on GridLayouts. 你也可以看到这个链接上BoxLayouts更多,或此链接以获得更多关于GridLayouts。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM