简体   繁体   English

如何使用 GridBagLayout 将组件放在右下角?

[英]How to put component in bottom-right corner with GridBagLayout?

I need to display a single component within a JPanel , and I want to keep that component in bottom right corner at all times.我需要在JPanel显示单个组件,并且我想始终将该组件保留在右下角。 I tried to do it with GridBagLayout:我尝试使用 GridBagLayout 来做到这一点:

val infoArea = new TextArea {
  text = "Hello!"
  border = Swing.EmptyBorder(30)
  background = Color.RED
  editable = false
}
val p = new JPanel
p.setLayout(new GridBagLayout)
val c = new GridBagConstraints
c.gridx = 0
c.gridy = 0
c.anchor = GridBagConstraints.LAST_LINE_END
p.add(infoArea.peer,c)
val f = new JFrame
f.setContentPane(p)
f.setVisible(true)

But the text area is at the center for some reason:但由于某种原因,文本区域位于中心:

在此处输入图片说明

What am I doing wrong here?我在这里做错了什么?

For example:例如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.*;

public class LayoutDemo {
   private static void createAndShowGui() {
      JLabel label = new JLabel("Hello");
      label.setOpaque(true);
      label.setBackground(Color.red);

      JPanel bottomPanel = new JPanel(new BorderLayout());
      bottomPanel.add(label, BorderLayout.LINE_END);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
      mainPanel.setPreferredSize(new Dimension(400, 400));


      JFrame frame = new JFrame("LayoutDemo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

在此处输入图片说明

final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
frame.add(Box.createGlue(), gbc);

final JTextArea textArea = new JTextArea("SE");
textArea.setPreferredSize(new Dimension(50, 50));
textArea.setOpaque(true);
textArea.setBackground(Color.RED);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
frame.add(textArea, gbc);

frame.setSize(640, 480);
frame.setVisible(true);

...if you realy want to use GridBagLayout ...如果你真的想使用 GridBagLayout

you have to put a dummy (use Box.createGlue() to make a dummy component) component on gridx = 0 and gridy = 0 and the component you want to put at the bottom right at gridx = 1 , gridy = 1 .您必须在gridx = 0gridy = 0上放置一个虚拟(使用Box.createGlue()制作一个虚拟组件)组件, gridx = 0要放置在gridx = 1 , gridy = 1 like this像这样

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

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