为什么不是';在java GUI中,我的JLab中是否居中于JPAND(GRIDBAGHORD)的中间?
我正在尝试将以下为什么不是';在java GUI中,我的JLab中是否居中于JPAND(GRIDBAGHORD)的中间?,java,swing,user-interface,graphics,layout-manager,Java,Swing,User Interface,Graphics,Layout Manager,我正在尝试将以下JLabel添加到JPanel的中心: import java.awt.Color; import java.awt.Graphics; import javax.swing.JLabel; public class DrawingPanel extends JLabel { protected void paintComponent(Graphics g){ super.paintComponent(g); int[] xpoin
JLabel
添加到JPanel
的中心:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
public class DrawingPanel extends JLabel {
protected void paintComponent(Graphics g){
super.paintComponent(g);
int[] xpoints = {230, 270, 290, 290, 270, 230, 210, 210};
int[] ypoints = {37, 37, 87, 115, 165, 165, 115, 87};
g.setColor(Color.white);
g.fillPolygon(xpoints, ypoints, 8 );
}
}
JPanel jp = new JPanel(new GridBagLayout());
DrawingPanel dp = new DrawingPanel();
jp.add(dp);
到以下JPanel
:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JLabel;
public class DrawingPanel extends JLabel {
protected void paintComponent(Graphics g){
super.paintComponent(g);
int[] xpoints = {230, 270, 290, 290, 270, 230, 210, 210};
int[] ypoints = {37, 37, 87, 115, 165, 165, 115, 87};
g.setColor(Color.white);
g.fillPolygon(xpoints, ypoints, 8 );
}
}
JPanel jp = new JPanel(new GridBagLayout());
DrawingPanel dp = new DrawingPanel();
jp.add(dp);
但是绘图面板
JPanel
甚至不会显示。怎么了?多谢各位
0x0
,这是默认大小0x0
JLabel
扩展<代码>JLabel具有足够复杂的功能,您无需尝试添加它import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new DrawingPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawingPanel extends JPanel {
public DrawingPanel() {
setBorder(new EmptyBorder(8, 8, 8, 8));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Insets insets = getInsets();
g2d.translate(insets.top, insets.left);
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int vHalf = height / 2;
int hHalf = width / 2;
int vPos = vHalf / 4;
int hPos = hHalf / 4;
int[] xpoints = {
0,
hHalf - hPos,
hHalf + hPos,
width,
width,
hHalf + hPos,
hHalf - hPos,
0
};
int[] ypoints = {
vHalf - vPos,
0,
0,
vHalf - vPos,
vHalf + vPos,
height,
height,
vHalf + vPos,
};
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
g2d.dispose();
}
}
}
0x0
,这是默认大小0x0
JLabel
扩展<代码>JLabel具有足够复杂的功能,您无需尝试添加它import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new DrawingPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawingPanel extends JPanel {
public DrawingPanel() {
setBorder(new EmptyBorder(8, 8, 8, 8));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Insets insets = getInsets();
g2d.translate(insets.top, insets.left);
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int vHalf = height / 2;
int hHalf = width / 2;
int vPos = vHalf / 4;
int hPos = hHalf / 4;
int[] xpoints = {
0,
hHalf - hPos,
hHalf + hPos,
width,
width,
hHalf + hPos,
hHalf - hPos,
0
};
int[] ypoints = {
vHalf - vPos,
0,
0,
vHalf - vPos,
vHalf + vPos,
height,
height,
vHalf + vPos,
};
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
g2d.dispose();
}
}
}
0x0
,这是默认大小0x0
JLabel
扩展<代码>JLabel具有足够复杂的功能,您无需尝试添加它import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new DrawingPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawingPanel extends JPanel {
public DrawingPanel() {
setBorder(new EmptyBorder(8, 8, 8, 8));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Insets insets = getInsets();
g2d.translate(insets.top, insets.left);
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int vHalf = height / 2;
int hHalf = width / 2;
int vPos = vHalf / 4;
int hPos = hHalf / 4;
int[] xpoints = {
0,
hHalf - hPos,
hHalf + hPos,
width,
width,
hHalf + hPos,
hHalf - hPos,
0
};
int[] ypoints = {
vHalf - vPos,
0,
0,
vHalf - vPos,
vHalf + vPos,
height,
height,
vHalf + vPos,
};
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
g2d.dispose();
}
}
}
0x0
,这是默认大小0x0
JLabel
扩展<代码>JLabel具有足够复杂的功能,您无需尝试添加它import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(new DrawingPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawingPanel extends JPanel {
public DrawingPanel() {
setBorder(new EmptyBorder(8, 8, 8, 8));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Insets insets = getInsets();
g2d.translate(insets.top, insets.left);
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int vHalf = height / 2;
int hHalf = width / 2;
int vPos = vHalf / 4;
int hPos = hHalf / 4;
int[] xpoints = {
0,
hHalf - hPos,
hHalf + hPos,
width,
width,
hHalf + hPos,
hHalf - hPos,
0
};
int[] ypoints = {
vHalf - vPos,
0,
0,
vHalf - vPos,
vHalf + vPos,
height,
height,
vHalf + vPos,
};
g2d.setColor(Color.BLACK);
g2d.drawPolygon(xpoints, ypoints, xpoints.length);
g2d.dispose();
}
}
}
考虑提供一个说明你的问题的方法。这将减少混乱和更好的响应
GridBagLayout
使用GridBagConstraints
管理定位。创建一个GridBagConstraints
对象,并在添加面板时添加它:jp.add(dp,gbc)
您的DrawingPanel
没有定义的大小细节,因此布局管理器首先将其大小调整为0x0
。您的“形状”不会居中,因为……它不是。您的坐标应该基于已知值(如组件的宽度和高度)计算,而不是您编的一些幻数…还有,为什么从JLabel
…?@MadProgrammer扩展?为什么没有定义大小?它有坐标。这些不是尺寸吗?考虑提供一个说明你的问题的方法。这将减少混乱和更好的响应GridBagLayout
使用GridBagConstraints
管理定位。创建一个GridBagConstraints
对象,并在添加面板时添加它:jp.add(dp,gbc)
您的DrawingPanel
没有定义的大小细节,因此布局管理器首先将其大小调整为0x0
。您的“形状”不会居中,因为……它不是。您的坐标应该基于已知值(如组件的宽度和高度)计算,而不是您编的一些幻数…还有,为什么从JLabel
…?@MadProgrammer扩展?为什么没有定义大小?它有坐标。这些不是尺寸吗?考虑提供一个说明你的问题的方法。这将减少混乱和更好的响应GridBagLayout
使用GridBagConstraints
管理定位。创建一个GridBagConstraints
对象,并在添加面板时添加它:jp.add(dp,gbc)
您的DrawingPanel
没有定义的大小细节,因此布局管理器首先将其大小调整为0x0
。您的“形状”不会居中,因为……它不是。您的坐标应该基于已知值(如组件的宽度和高度)计算,而不是您需要的一些幻数