java—如何将多个元素相互对齐?

mm5n2pyu  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(441)

我有困难,在调整摆动元素流低于对方。使用 GridLayout 对我没有帮助,因为它将屏幕划分为大小相等的行。我需要把一个组件放到每一行,下一个组件应该在最后一个组件的底部。

mi的问题是,如果问题的选择不止一行,这种布局会将它们压缩到行中,并且图片和问题“3+3”之间存在巨大的差距

sbtkgmzw

sbtkgmzw1#

你可以用 GridBagLayout 为了这个。它很容易使用。下面是一个示例实现。

  1. import java.awt.GridBagConstraints;
  2. import java.awt.GridBagLayout;
  3. import javax.swing.ImageIcon;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JPanel;
  7. import javax.swing.JRadioButton;
  8. class MyFrame extends JFrame {
  9. public MyFrame() {
  10. JPanel panel = new JPanel(new GridBagLayout());
  11. JLabel image = new JLabel();
  12. image.setIcon(new ImageIcon(getClass().getResource("/image.png")));
  13. JLabel question = new JLabel("3 + 3 = ?");
  14. JRadioButton rb1 = new JRadioButton("5");
  15. JRadioButton rb2 = new JRadioButton("3");
  16. JRadioButton rb3 = new JRadioButton("6");
  17. JRadioButton rb4 = new JRadioButton("9");
  18. JRadioButton rb5 = new JRadioButton("23");
  19. GridBagConstraints c = new GridBagConstraints();
  20. c.gridx = 0;//set the x location of the grid for the next component
  21. c.gridy = 0;//set the y location of the grid for the next component
  22. panel.add(image,c);
  23. c.gridy = 1;//change the y location
  24. c.anchor=GridBagConstraints.WEST;//left align components after this point
  25. panel.add(question,c);
  26. c.gridy = 2;//change the y location
  27. panel.add(rb1,c);
  28. c.gridy = 3;//change the y location
  29. panel.add(rb2,c);
  30. c.gridy = 4;//change the y location
  31. panel.add(rb3,c);
  32. c.gridy = 5;//change the y location
  33. panel.add(rb4,c);
  34. c.gridy =6;//change the y location
  35. panel.add(rb5,c);
  36. this.getContentPane().add(panel);
  37. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  38. this.pack();
  39. }
  40. public static void main(String[] args) {
  41. MyFrame myFrame = new MyFrame();
  42. myFrame.setVisible(true);
  43. }
  44. }

参考文献:https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

展开查看全部
qzwqbdag

qzwqbdag2#

有许多可能的解决方案,但最有用的可能是 GridBagLayout ,虽然复杂,但非常灵活。

  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.Font;
  4. import java.awt.GridBagConstraints;
  5. import java.awt.GridBagLayout;
  6. import javax.swing.JCheckBox;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10. import javax.swing.UIManager;
  11. import javax.swing.UnsupportedLookAndFeelException;
  12. public class Test {
  13. public static void main(String[] args) {
  14. new Test();
  15. }
  16. public Test() {
  17. EventQueue.invokeLater(new Runnable() {
  18. @Override
  19. public void run() {
  20. try {
  21. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  22. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
  23. ex.printStackTrace();
  24. }
  25. JFrame frame = new JFrame("Testing");
  26. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. frame.add(new TestPane());
  28. frame.pack();
  29. frame.setLocationRelativeTo(null);
  30. frame.setVisible(true);
  31. }
  32. });
  33. }
  34. public class TestPane extends JPanel {
  35. public TestPane() {
  36. setLayout(new GridBagLayout());
  37. JLabel label = new JLabel("Cool");
  38. label.setFont(label.getFont().deriveFont(Font.BOLD, 48f));
  39. label.setOpaque(true);
  40. label.setBackground(Color.WHITE);
  41. GridBagConstraints gbc = new GridBagConstraints();
  42. gbc.gridwidth = GridBagConstraints.REMAINDER;
  43. add(label, gbc);
  44. add(new JLabel("3+5="), gbc);
  45. add(new JCheckBox("5"), gbc);
  46. add(new JCheckBox("3"), gbc);
  47. add(new JCheckBox("6"), gbc);
  48. add(new JCheckBox("9"), gbc);
  49. add(new JCheckBox("23"), gbc);
  50. }
  51. }
  52. }

你甚至可以使用 anchor 要将组件移动到这些单元格中的不同位置。。。

  1. gbc.anchor = GridBagConstraints.WEST;


显然,这些约束也可以应用于单个组件,因此每个组件都可以有自己的约束集
查看如何使用gridbaglayout了解更多详细信息

展开查看全部

相关问题