具有GridBagLayout的JPanel中具有FlowLayout的JPanel无法正确锚定

时间:2018-10-12 03:39:08

标签: java swing layout-manager gridbaglayout flowlayout

我正在为角色扮演游戏制作角色创建面板。它有一个JPanel使用GridBagLayout,其中一个JPanel使用FlowLayout

最初,当我不使用FlowLayout时,它看起来像这样:

Panel with only GridBagLayout

我需要在x轴上添加另一个组件。 weightx值将增加并变为偶数。这会使我所有的头衔都偏离中心,我不喜欢那样。我当时想我可以使用JPanel将我的某些组件放入FlowLayout内,或者将其包装在JScrollPane或类似的东西中:

Panel with GridBagLayout and FlowLayout

不幸的是,使用JPanel的{​​{1}}并没有被使用网格网格布局的FlowLayout锚定。尽管已设置为锚点JPanel,但它还是转到了显示屏的侧面。

我的代码:

GridBagConstraints.BASELINE

其他:

  • 我尝试做SSCCE。我不介意有人在没有颜色和字体的情况下运行我的代码!
  • 有彩色的public class TheLifeOfErnestRhodes extends JFrame { private static JFrame frame = new JFrame("The Life of Ernest Rhodes"); private static Color black = new Color(40,40,40); private static Color gold = new Color(255,223,0); private static Color sienna = new Color(255,82,45); private static Color stone = new Color(119,136,153); private static Font titleFont = new Font("Serif", Font.BOLD + Font.ITALIC, 48); private static Font textFont = new Font("Serif", Font.PLAIN, 20); private static Font buttonFont = new Font("Serif", Font.BOLD + Font.ITALIC, 20); private static void setFrame() { frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setResizable(false); Container pane = frame.getContentPane(); JPanel backPanel = new JPanel(); backPanel.setBackground(black); backPanel.setVisible(true); pane.add(backPanel); pane.add(charScreen()); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { setFrame(); } }); } private static JPanel charScreen() { GridBagLayout layout = new GridBagLayout(); JPanel charScreen = new JPanel(layout); charScreen.setBackground(black); charScreen.setVisible(true); //Static labels for the two headings/titles on this screen. GridBagConstraints c = new GridBagConstraints(); c.gridx = 1; c.gridy = 0; c.anchor = GridBagConstraints.NORTH; c.insets = new Insets(80,0,20,0); JLabel heading = new JLabel("THE LIFE OF ERNEST RHODES"); heading.setForeground(gold); heading.setFont(titleFont); charScreen.add(heading, c); c.gridy++; c.anchor = GridBagConstraints.BASELINE; c.insets = new Insets(0,0,10,0); JLabel title = new JLabel("CHARACTER CREATION"); title.setForeground(stone); title.setFont(titleFont); charScreen.add(title, c); c.gridy++; c.insets = new Insets (10, 20, 10, 20); JPanel namePanel = new JPanel(new FlowLayout()); namePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); JLabel labelName = new JLabel("FULL NAME:"); labelName.setForeground(stone); labelName.setFont(textFont); labelName.setVisible(true); namePanel.add(labelName, c); JTextField firstName = new JTextField("Taylor", 20); firstName.setMinimumSize(firstName.getPreferredSize()); firstName.setBackground(black.darker()); firstName.setForeground(stone); firstName.setFont(textFont); firstName.setHorizontalAlignment(JTextField.CENTER); firstName.setBorder(null); firstName.setVisible(true); namePanel.add(firstName); JTextField lastName = new JTextField("Woodhouse", 20); lastName.setMinimumSize(lastName.getPreferredSize()); lastName.setBackground(black.darker()); lastName.setForeground(stone); lastName.setFont(textFont); lastName.setHorizontalAlignment(JTextField.CENTER); lastName.setBorder(null); lastName.setVisible(true); namePanel.add(lastName); JLabel displayName = new JLabel("TYPE A NAME & PRESS 'ENTER'"); displayName.setForeground(stone); displayName.setFont(textFont); displayName.setVisible(true); namePanel.add(displayName, c); charScreen.add(namePanel); c.gridx = 0; c.gridy++; c.fill = GridBagConstraints.NONE; JLabel availablePts = new JLabel("AVAILABLE POINTS: 5"); availablePts.setForeground(stone); availablePts.setFont(textFont); availablePts.setVisible(true); charScreen.add(availablePts, c); c.gridx++; JLabel allocatedPts = new JLabel("ALLOCATED POINTS:"); allocatedPts.setForeground(stone); allocatedPts.setFont(textFont); allocatedPts.setVisible(true); charScreen.add(allocatedPts, c); c.gridx++; JLabel selectTrait = new JLabel("SELECT TRAITS:"); selectTrait.setForeground(stone); selectTrait.setFont(textFont); selectTrait.setVisible(true); charScreen.add(selectTrait, c); c.gridx = 0; c.gridy++; JLabel labelInv = new JLabel("Investigation"); labelInv.setForeground(gold); labelInv.setFont(textFont); labelInv.setVisible(true); charScreen.add(labelInv, c); c.gridy++; JLabel labelPers = new JLabel("Persuasion"); labelPers.setForeground(gold); labelPers.setFont(textFont); labelPers.setVisible(true); charScreen.add(labelPers, c); c.gridy++; JLabel LabelStl = new JLabel("Stealth"); LabelStl.setForeground(gold); LabelStl.setFont(textFont); LabelStl.setVisible(true); charScreen.add(LabelStl, c); c.gridx++; c.gridy = 4; JLabel allocatedInt = new JLabel("0"); allocatedInt.setForeground(stone); allocatedInt.setFont(textFont); allocatedInt.setVisible(true); charScreen.add(allocatedInt, c); c.gridy++; JLabel allocatedPers = new JLabel("0"); allocatedPers.setForeground(stone); allocatedPers.setFont(textFont); allocatedPers.setVisible(true); charScreen.add(allocatedPers, c); c.gridy++; JLabel allocatedAth = new JLabel("0"); allocatedAth.setForeground(stone); allocatedAth.setFont(textFont); allocatedAth.setVisible(true); charScreen.add(allocatedAth, c); c.gridx++; c.gridy = 4; JLabel keenEye = new JLabel("Keen Eye"); keenEye.setForeground(sienna); keenEye.setFont(textFont); keenEye.setVisible(true); charScreen.add(keenEye, c); c.gridy++; JLabel interrogator = new JLabel("Interrogator"); interrogator.setForeground(gold); interrogator.setFont(textFont); interrogator.setVisible(true); charScreen.add(interrogator, c); c.gridy++; JLabel sleuth = new JLabel("Sleuth"); sleuth.setForeground(gold); sleuth.setFont(textFont); sleuth.setVisible(true); charScreen.add(sleuth, c); c.gridx = 1; c.gridy++; JLabel reset = new JLabel("Reset Stats"); reset.setForeground(gold); reset.setFont(textFont); reset.setVisible(true); charScreen.add(reset, c); c.gridy++; JLabel gender = new JLabel("GENDER:"); gender.setForeground(stone); gender.setFont(textFont); gender.setVisible(true); charScreen.add(gender, c); c.gridx = 0; c.gridy++; JLabel male = new JLabel("Male"); male.setForeground(gold); male.setFont(textFont); male.setVisible(true); charScreen.add(male, c); c.gridx++; JLabel female = new JLabel("Female"); female.setForeground(gold); female.setFont(textFont); female.setVisible(true); charScreen.add(female, c); c.gridx++; JLabel other = new JLabel("Other"); other.setForeground(sienna); other.setFont(textFont); other.setVisible(true); charScreen.add(other, c); c.gridx = 1; c.gridy++; c.anchor = GridBagConstraints.PAGE_END; JLabel clickNext = new JLabel("Continue"); clickNext.setForeground(stone); clickNext.setFont(titleFont); clickNext.setVisible(true); charScreen.add(clickNext, c); } } 用作“按钮”。 Mac的按钮图标真的很丑,所以我流氓了。上面的代码已修改为仅显示相关组件。在我的程序中,JLabel中添加了MouseListener。
  • 如果您想了解这款游戏,那是一个谋杀之谜,玩家就是调查员!显然,他们正在调查“欧内斯特·罗德斯”的生死。

谢谢大家!

干杯, 阿阿迪

1 个答案:

答案 0 :(得分:1)

<meta name="viewport" content="width=device-width,initial-scale=1"> 是没有意义的,因为namePanel.add(labelName, c);使用的是namePanel,将FlowLayout传递给它是没有意义的,因为它对GridBagLayoutConstraint毫无意义

FlowLayout有效地传递了“默认值” charScreen.add(namePanel);,这意味着GridBagLayoutConstraint会自行决定是否进行布局,这对您没有帮助。

也许您是说GridBagLayout,其中charScreen.add(namePanel, c);是您传递给c的约束

例如...

For example

namePanel