[英]Java swing layouts and/or labels not in order
I have got a window that should display the following: 我有一个应该显示以下内容的窗口:
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. 您需要使用嵌套面板。
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.