java - Java 中的居中菜单按钮

标签 java button menu center

我正在尝试用 Java 创建一个菜单。我有 4 个按钮。我想让菜单在 X 轴和 Y 轴上居中。我只需将它与 X 轴居中即可。 setAlignmentY()、setLocation() 不起作用。你可以帮帮我吗?谢谢。

 import javax.swing.*;
 import javax.swing.border.*;
 import java.awt.*;
 import java.awt.event.*;


public class Menu extends JFrame
{

private static JFrame frame;
private static JPanel myPanel;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;


public Menu()
{

    myPanel = new JPanel();

    button1 = new JButton("Read From File");  
    button1.setMaximumSize(new Dimension(122, 50));
    button1.setAlignmentX(JButton.CENTER_ALIGNMENT);

    button2 = new JButton("Start Animation");
    button2.setMaximumSize(new Dimension(122, 50));
    button2.setAlignmentX(JButton.CENTER_ALIGNMENT);

    button3 = new JButton("Help");
    button3.setMaximumSize(new Dimension(122, 50));
    button3.setAlignmentX(JButton.CENTER_ALIGNMENT);

    button4 = new JButton("Quit");
    button4.setMaximumSize(new Dimension(122, 50)); 
    button4.setAlignmentX(JButton.CENTER_ALIGNMENT);


    SimpleListener ourListener = new SimpleListener();
    button1.addActionListener(ourListener);
    button2.addActionListener(ourListener);
    button3.addActionListener(ourListener);
    button4.addActionListener(ourListener);


    myPanel.add(button1);
    myPanel.add(button2);
    button2.setEnabled(false);
    myPanel.add(button3);
    myPanel.add(button4);

    myPanel.setLayout(new BoxLayout(myPanel,BoxLayout.Y_AXIS));
    myPanel.setBorder(BorderFactory.createTitledBorder("OPTIONS"));
    myPanel.setBackground(Color.decode("#82CAFF"));


}

private class SimpleListener implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {

        String buttonName = e.getActionCommand();

        boolean readFile = false;
        if (buttonName.equals("Read From File"))
        {
            if( readFile == true ){ 
                button2.setEnabled(true); }
            else
            {
                JOptionPane.showMessageDialog(frame, "File couldn't be read!");
            }
        }

        else if (buttonName.equals("Start Animation"))
            JOptionPane.showMessageDialog(frame, "Nice Try!");

        else if (buttonName.equals("Help"))
            JOptionPane.showMessageDialog(frame, "Ontirismo maykilino cino! Ay em kino!" );

        else if (buttonName.equals("Quit"))
            System.exit(0);
    }
}
public static void main(String s[])
{

    Menu gui = new Menu();
    frame  = new JFrame("BILLIARDINATOR 5000 MENU");
    frame.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e)
        {System.exit(0);}});

        frame.getContentPane().add(myPanel);
        frame.setPreferredSize(new Dimension(400, 400));
        frame.setLocation(600,200);
        frame.setResizable(false);
        frame.pack();
        frame.setVisible(true);

}

最佳答案

您可以使用GridBagLayout。这很可怕,但它确实有效。

myPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;

// change setMaximumSize to setPreferredSize for all buttons
button1.setPreferredSize(new Dimension(122, 50)); // etc

// increase Y coordinate after adding a button
myPanel.add(button1, gbc);
++gbc.gridy;
myPanel.add(button2, gbc);
++gbc.gridy;
myPanel.add(button3,gbc);
++gbc.gridy;
myPanel.add(button4, gbc);
++gbc.gridy;

// And of course, remove the BoxLayout.

关于java - Java 中的居中菜单按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13681161/

相关文章:

java - 是否可以将 Jhipster 应用程序转变为移动应用程序?

jquery - 如何停止页面底部的 float 菜单

Android 在点击按钮时显示弹出消息或动画

Javascript,有不同的按钮调用不同的功能

jquery - 更改 jQuery UI 按钮的颜色

jquery - 为什么我的粘性菜单在更改到位置 :fixed 时向左跳转

css - 如何在菜单中创建随文本长度扩展的悬停图像?

java - 在 SearchView 中只允许输入一些想要的字符

java - 程序停止后如何保持 boolean 值的特定值?

java - Java中使用DOM读取XML数据