移动JButtons

时间:2014-06-04 01:31:28

标签: 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);            
    }




}

3 个答案:

答案 0 :(得分:4)

不要将JButton放在使用FlowLayout的容器中,而是使用另一个允许堆叠组件的布局。如果按钮的大小相同,或者如果它们需要不同的尺寸,BoxLayout就会出现GridLayout。

查看the Layout Manager Tutorial

答案 1 :(得分:4)

您可以使用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的内容窗格设置为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

答案 2 :(得分:1)

您可以尝试使用BoxLayout而不是FlowLayout。在这种情况下,您可以:

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

或者正如Hovercraft所说,你可以使用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.

您还可以在BoxLayouts上查看this link以获取更多信息,或者在GridLayouts上查看更多this link