简体   繁体   English

Java摆动布局和/或标签不按顺序排列

[英]Java swing layouts and/or labels not in order

I have got a window that should display the following: 我有一个应该显示以下内容的窗口:

  • JLablel "Have you used GUI before?" JLablel“您以前使用过GUI吗?” on the top, centered 在顶部,居中
  • two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left 它下面有两个radioButtons“Yes”和“No”,有点在中间,有点向左
  • a JButton "NEXT" in the bottom-right corner 一个JButton“NEXT”在右下角

All three elements should have green font and darkGrey background. 所有三个元素都应该有绿色字体和darkGrey背景。 The problem is that the window which is showing up, does not look like I would like it to. 问题是出现的窗口看起来不像我想要的那样。 And this is my code: 这是我的代码:

        yesButton = new JRadioButton(yes);
        //yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
        yesButton.setActionCommand(yes);            

        noButton = new JRadioButton(no);
        // noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
        noButton.setActionCommand(no);

        ButtonGroup group = new ButtonGroup();
        group.add(yesButton);
        group.add(noButton);

        nextButton = new JButton("NEXT");
        nextButton.setActionCommand(next);

        yesButton.addActionListener(this);
        noButton.addActionListener(this);
        nextButton.addActionListener(this);

        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(yesButton);
        radioPanel.add(noButton);

        add(radioPanel, BorderLayout.WEST);
        // setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        // radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
            // there is no difference between the above two, right?

        String q = "Have you used GUI before?";
        JPanel area = new JPanel(new BorderLayout());

        area.setBackground(Color.darkGray);
        JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
                + q + "</html>", SwingConstants.CENTER);
        textLabel2.setForeground(Color.green);
        Font font2 = new Font("SansSerif", Font.PLAIN, 30);
        textLabel2.setFont(font2);
        //textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
        area.add(textLabel2, BorderLayout.NORTH);
        area.add(nextButton, BorderLayout.EAST);
        add(area, BorderLayout.CENTER);

I feel I'm nearly there, thanks for any help! 我觉得我快到了,谢谢你的帮助!

--EDIT-- A screenshot: --EDIT--截图: 截图

You need to use nested panels. 您需要使用嵌套面板。

  1. for the BorderLayout.NORTH you can add the JLabel directly. 对于BorderLayout.NORTH,您可以直接添加JLabel。 You will need to set the horizontal text alignment to center. 您需要将水平文本对齐设置为居中。
  2. for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER. 对于单选按钮,您可以使用FlowLayout创建JPanel,然后将按钮添加到面板并将面板添加到CENTER。
  3. for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH. 对于按钮,使用右对齐的FlowLayout将按钮添加到面板,然后将面板添加到SOUTH。

There are other choices. 还有其他选择。 You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it. 您还可以使用Vertical BoxLayout作为主面板的布局,然后将子面板添加到其中。

You won't be able to get much control with just a BorderLayout. 只使用BorderLayout,您将无法获得太多控制权。 Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc ). 尝试其他类似MigLayout或Java的其他许多布局管理器(GridBag,Box )。

In MigLayout it would look something like: MigLayout中,它看起来像:

area.setLayout(new MigLayout("fill"));

area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");

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

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