java - 除了使用 GridBagConstraints 之外,还有更精确的方法来放置 JButton 吗?

标签 java swing jbutton layout-manager gridbaglayout

package swingtraining;

import static java.awt.Color.BLACK;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.CENTER;
import static java.awt.GridBagConstraints.NORTH;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameTest extends JFrame {

    public JFrameTest() {
        setSize(500, 500);
        setTitle("Hello :D");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }

    public static class JPanelTest extends JPanel {

        public JPanelTest() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.ipadx = 100;
            gbc.ipady = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            setBackground(BLACK);
            setOpaque(true);
            add(new JButton("Hello"), gbc);
        }
    }

    public static class JButtonTest extends JButton {

        JButtonTest() {
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrameTest T = new JFrameTest();
                JPanelTest Jp1 = new JPanelTest();
                T.add(Jp1);
            }
        });
    }
}

这段代码工作正常,但我不喜欢我的 Button 恰好放置在左侧,只能更改 bButton 的大小(宽度、高度)。所以我想知道是否有更精确的方法来放置东西,比如也许只是告诉 Java 使用 X Y 坐标或其他东西将其放置在 Panel 上,例如;

JButton.setLocation(200,200);

有什么建议吗?

最佳答案

So I was wondering if there was a more precise way to place things, like maybe just telling Java to place it on the Panel using X Y coordinates or something, like;

你可能不喜欢它,但是布局管理器非常强大并且非常擅长他们的工作,这很大程度上减少了你的工作量,让你的生活变得更加轻松

GridBagConstraints#insets

所以我真正做的就是将 anchor 约束更改为 GridBagConstraints.NORTHWEST 并添加 gbc.insets = new Insets(200, 200, 0, 0 );

Example

红色来自自定义 glassPane,它正在绘制一个以 200x200 为中心的点,但因为 JButton 没有填充其整个区域,所以我添加了一个 ComponentListener ,当 componentMoved 事件发生时,它打印出 location ,打印出 200x20

import java.awt.Color;
import static java.awt.Color.BLACK;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class JFrameTest extends JFrame {

    public JFrameTest() {
        setSize(500, 500);
        setTitle("Hello :D");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        JPanel pane = new JPanel() {
            @Override
            public boolean isOpaque() {
                return false;
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.RED);
                g2d.fillOval(195, 195, 10, 10);
                g2d.dispose();
            }

        };
        setGlassPane(pane);
        pane.setVisible(true);
        setVisible(true);
    }

    public static class JPanelTest extends JPanel {

        public JPanelTest() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.ipadx = 100;
            gbc.ipady = 0;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.insets = new Insets(200, 200, 0, 0);
            gbc.weightx = 1.0;
            gbc.weighty = 1.0;
            setBackground(BLACK);
            setOpaque(true);
            JButton btn = new JButton("Hello");
            btn.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentMoved(ComponentEvent e) {
                    System.out.println(btn.getLocation());
                }
            });
            add(btn, gbc);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrameTest T = new JFrameTest();
                JPanelTest Jp1 = new JPanelTest();
                T.add(Jp1);
            }
        });
    }
}

关于java - 除了使用 GridBagConstraints 之外,还有更精确的方法来放置 JButton 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242542/

相关文章:

java - 为 JButton 定义背景颜色和前景图标?

java - 在 Windows 操作系统上,FrameView 中不显示 JDesktopPane 和 JinternalFrame

java - JButton 动态工具提示文本未显示

java - 向由另一个 JButton 创建的 JButton 添加操作

java - 在 Android 应用程序中添加 Java 部分的 Intent 时出错

java - Android - 在文件中查找字符串,如果存在则使用 eclipse 获取该行

java - 具有单独的 CommandLineRunner 的 SpringBoot 应用程序不应在应用程序启动时运行

Java - Swing 中的监听器

Java:获取时间间隔

java - JButton 不改变大小