是否有比使用 GridBagConstraints 更精确的方式来放置我的 JButton?
Is there a more precise way to place my JButtons other than using GridBagConstraints?
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);
有什么建议吗?
import javax.swing.*;
import java.awt.*;
public class JFrameTest extends JFrame{
JPanel pnl = new JPanel();
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrameTest myFrame = new JFrameTest();
myFrame.setTitle("Hello :D");
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setResizable(false);
myFrame.setVisible(true);
}});
}
public JFrameTest(){
MyCustomLayout customLayout = new MyCustomLayout();
Container c = getContentPane();
c.setLayout(customLayout);
pnl.setBackground(Color.BLACK);
c.add(pnl);
c.add(new JButton("Hello"));
}
}
class MyCustomLayout implements LayoutManager {
public MyCustomLayout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 500;
dim.height = 500;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{c.setBounds(insets.left+1,insets.top+8,930,720);}
c = parent.getComponent(1);
if (c.isVisible())
{c.setBounds(insets.left+250,insets.top+250,80,25);}
//Here just set left value and top value for xy positions
}
}
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);
}
});
}
}
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);
有什么建议吗?
import javax.swing.*;
import java.awt.*;
public class JFrameTest extends JFrame{
JPanel pnl = new JPanel();
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrameTest myFrame = new JFrameTest();
myFrame.setTitle("Hello :D");
myFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setResizable(false);
myFrame.setVisible(true);
}});
}
public JFrameTest(){
MyCustomLayout customLayout = new MyCustomLayout();
Container c = getContentPane();
c.setLayout(customLayout);
pnl.setBackground(Color.BLACK);
c.add(pnl);
c.add(new JButton("Hello"));
}
}
class MyCustomLayout implements LayoutManager {
public MyCustomLayout() {}
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
Insets insets = parent.getInsets();
dim.width = 500;
dim.height = 500;
return dim;
}
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
return dim;
}
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Component c;
c = parent.getComponent(0);
if (c.isVisible())
{c.setBounds(insets.left+1,insets.top+8,930,720);}
c = parent.getComponent(1);
if (c.isVisible())
{c.setBounds(insets.left+250,insets.top+250,80,25);}
//Here just set left value and top value for xy positions
}
}
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);
}
});
}
}