简体   繁体   English

如何定位按钮?

[英]How to position buttons?

How do I position buttons exactly where I want them to be. 如何将按钮准确定位在我想要的位置。 I have a background image set to the main panel. 我在主面板上设置了背景图片。 then the 2 buttons are on top next to each other But I want them to be in the center . 然后2个按钮彼此相邻在顶部,但我希望它们在中间。 on top of each other like in a main menu. 就像在主菜单中一样

I have tried all ways nothing works the closest I am getting is by using Box Layout. 我已经尝试了所有方法,但都无法使用Box Layout。 Here is the code and image of how it looks. 这是其外观的代码和图像。 but I need the buttons to be in center. 但我需要将按钮置于中间。

http://puu.sh/gTZgQ/c06f09416c.jpg

public Menu() {
    JFrame frame = new JFrame("Fruit Catcher");
    JPanel panel = new JPanel();

    frame.add(panel);


    ImageIcon junglebackground = new ImageIcon("junglebackground.jpg");
    JLabel backgroundimage = new JLabel(junglebackground);

    frame.add(backgroundimage);
    frame.setSize(700,470);
    frame.setResizable(false);
    frame.setVisible(true);


    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));        

    JButton Play = new JButton("Play");
    JButton Scoreboard = new JButton("Scoreboard");
    Play.setAlignmentX(Component.CENTER_ALIGNMENT);
    JLabel gap = new JLabel("\n");
    Scoreboard.setAlignmentX(Component.CENTER_ALIGNMENT);


    buttonPanel.add(Play);
    buttonPanel.add(gap);
    buttonPanel.add(Scoreboard);



    frame.add(buttonPanel);


}

Nest JPanels Nest JPanels

  • Place the buttons in a JPanel that uses GridLayout(0, 1, 0, vertGap) which stands for a grid with a variable number of rows, 1 column, 0 horizontal gaps (since there is only one column) and vertGap vertical gap -- an int value that you must decide on. 将按钮放置在使用GridLayout(0, 1, 0, vertGap)的JPanel中,该代表具有可变行数,1列,0个水平间距(因为只有一列)和vertGap垂直间距的网格- 必须确定的int值。
  • Then place this JPanel into your main JPanel. 然后,将此JPanel放入主JPanel中。
  • If you want it at the top and centered, you could have the main JPanel simply use its default FlowLayout. 如果您希望它位于顶部并居中,则可以使主JPanel仅使用其默认的FlowLayout。
  • If you want the image to show through the GridLayout using JPanel, be sure to set it to non-opaque by calling, setOpaque(false) . 如果要使用JPanel通过GridLayout显示图像,请确保通过调用setOpaque(false)将其设置为不透明。
  • Most importantly, read up on how to use layout managers in the Swing tutorials. 最重要的是,在Swing教程中阅读如何使用布局管理器。 You can find links to the Swing tutorials and to other Swing resources here: Swing Info . 您可以在此处找到Swing教程和其他Swing资源的链接: Swing信息

You should further read whatever introduction to swing you had. 您应该进一步阅读任何有关挥杆的介绍。 Later on, they are bound to talk about Layouts , which are ways to define how buttons should be layed out in your window. 稍后,他们必然会谈论Layouts ,这是定义如何在窗口中布置按钮的方式。

What you're describing sounds like you could solve it with some grid layout, but it's hard to guess what exactly you need, so it might be best if you find out yourself. 您所描述的内容听起来似乎可以通过一些网格布局来解决,但是很难猜测出您到底需要什么,因此,如果您发现自己,那可能是最好的。

Since you know the background image is larger that the buttons you want to disply, you can use the JLabel as a Container for your buttons. 由于您知道背景图片要大于要显示的按钮,因此可以将JLabel用作按钮的容器。 The basic code would be: 基本代码为:

JFrame frame = new JFrame("Fruit Catcher");

ImageIcon jungleBackground = new ImageIcon("junglebackground.jpg");
JLabel backgroundImage = new JLabel(junglebackground);
frame.add( backgroundImage)

Now you need to add the buttons to the label. 现在,您需要将按钮添加到标签。 Two options: 两种选择:

Use a BoxLayout on the label: 在标签上使用BoxLayout

backgroundImage.setLayout(new BoxLayout(backgroundImage, BoxLayout.Y_AXIS));  
backgroundImage.add( Box.createVerticalGlue() );
backgroundImage.add( new JButton("Play") );
backgroundImage.add( Box.createVerticalStrut(20) );
backgroundImage.add( new JButton("Scoreboard") );
backgroundImage.add( Box.createVerticalGlue() );

Read the section from the Swing tutorial on How to Use Box Layout for more information and examples. 阅读Swing教程中有关如何使用Box布局的部分, 获取更多信息和示例。

Another choice is to use a GridBagLayout on the label: 另一个选择是在标签上使用GridBagLayout

backgroundImage.setLayout( new GridBagLayout() );
GridBagConstraints gbc = new GridBagConstraints();

backgroundImage.add(new JButton("Play"), gbc);
gbc.gridy = 1;
gbc.insets = new Insets(10, 0, 0, 0);
backgroundImage.add(new JButton("Scoreboard"), gbc);

Don't forget to read the tutorial for more information about the GridBagConstraints . 不要忘记阅读本教程,以获取有关GridBagConstraints更多信息。

Finally, once all the components are added to the frame you would do: 最后,将所有组件添加到框架后,您将执行以下操作:

frame.pack();
frame.setResizable(false);
frame.setVisible(true);

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

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