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 );
红色来自自定义 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/