BorderLayout 就像流式布局
BorderLayout acting like a flow layout
我很难弄清楚为什么这个边界布局仍然像流式布局一样..
我先试了这个:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas);
text = new JLabel("hey");
canvas.add(text, BorderLayout.SOUTH);
然后我试了这个:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas)
JPanel canvasSouth = new JPanel(new BorderLayout());
canvas.add(canvasSouth, BorderLayout.SOUTH);
text = new JLabel("hey");
canvasSouth.add(text);
然后我说去他的,我要像这样把所有东西都扔掉:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas);
JPanel canvasSouth = new JPanel(new BorderLayout());
canvas.add(canvasSouth, BorderLayout.SOUTH);
JPanel canvasNorth = new JPanel(new BorderLayout());
canvas.add(canvasNorth, BorderLayout.NORTH);
JPanel canvasEast = new JPanel(new BorderLayout());
canvas.add(canvasEast, BorderLayout.EAST);
JPanel canvasWest = new JPanel(new BorderLayout());
canvas.add(canvasWest, BorderLayout.WEST);
text = new JLabel("hey");
canvasSouth.add(text);
我以前遇到过这个问题,但一直无法自行解决。因此,我问你们所有人。谢谢。
这个问题是一个复合问题...
- 您将
canvas
添加到仍在使用 FlowLayout
的 JPanel
,后者将使用 canvas
的 preferredSize
来确定如何最好地布局它
- 您正在向
BorderLayout
的所有位置添加空 JPanel
,因为它们的默认 preferredSize
为 0x0
,就像您根本不添加任何东西。因此,唯一向 canvas
及其父容器提供大小调整提示的是 JLabel
相反,放弃 canvas
。只需将父容器的布局设置为 BorderLayout
。为要添加到父容器的其他组件提供一些大小调整提示。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class game extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public game() {
nonMaingame canvas = new nonMaingame();
setSize(1020, 720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(canvas);
}
public static void main(String[] args) {
new game().setVisible(true);
}
public class nonMaingame extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel text;
public nonMaingame() {
setLayout(new BorderLayout());
JPanel canvasSouth = makePanel(Color.RED);
add(canvasSouth, BorderLayout.SOUTH);
JPanel canvasNorth = makePanel(Color.BLUE);
add(canvasNorth, BorderLayout.NORTH);
JPanel canvasEast = makePanel(Color.YELLOW);
canvasEast.setBackground(Color.YELLOW);
add(canvasEast, BorderLayout.EAST);
JPanel canvasWest = makePanel(Color.MAGENTA);
add(canvasWest, BorderLayout.WEST);
text = new JLabel("hey");
canvasSouth.add(text);
}
protected JPanel makePanel(Color color) {
JPanel pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
pane.setBackground(color);
return pane;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
}
我很难弄清楚为什么这个边界布局仍然像流式布局一样..
我先试了这个:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas);
text = new JLabel("hey");
canvas.add(text, BorderLayout.SOUTH);
然后我试了这个:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas)
JPanel canvasSouth = new JPanel(new BorderLayout());
canvas.add(canvasSouth, BorderLayout.SOUTH);
text = new JLabel("hey");
canvasSouth.add(text);
然后我说去他的,我要像这样把所有东西都扔掉:
JPanel canvas = new JPanel(new BorderLayout());
add(canvas);
JPanel canvasSouth = new JPanel(new BorderLayout());
canvas.add(canvasSouth, BorderLayout.SOUTH);
JPanel canvasNorth = new JPanel(new BorderLayout());
canvas.add(canvasNorth, BorderLayout.NORTH);
JPanel canvasEast = new JPanel(new BorderLayout());
canvas.add(canvasEast, BorderLayout.EAST);
JPanel canvasWest = new JPanel(new BorderLayout());
canvas.add(canvasWest, BorderLayout.WEST);
text = new JLabel("hey");
canvasSouth.add(text);
我以前遇到过这个问题,但一直无法自行解决。因此,我问你们所有人。谢谢。
这个问题是一个复合问题...
- 您将
canvas
添加到仍在使用FlowLayout
的JPanel
,后者将使用canvas
的preferredSize
来确定如何最好地布局它 - 您正在向
BorderLayout
的所有位置添加空JPanel
,因为它们的默认preferredSize
为0x0
,就像您根本不添加任何东西。因此,唯一向canvas
及其父容器提供大小调整提示的是JLabel
相反,放弃 canvas
。只需将父容器的布局设置为 BorderLayout
。为要添加到父容器的其他组件提供一些大小调整提示。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class game extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public game() {
nonMaingame canvas = new nonMaingame();
setSize(1020, 720);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(canvas);
}
public static void main(String[] args) {
new game().setVisible(true);
}
public class nonMaingame extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel text;
public nonMaingame() {
setLayout(new BorderLayout());
JPanel canvasSouth = makePanel(Color.RED);
add(canvasSouth, BorderLayout.SOUTH);
JPanel canvasNorth = makePanel(Color.BLUE);
add(canvasNorth, BorderLayout.NORTH);
JPanel canvasEast = makePanel(Color.YELLOW);
canvasEast.setBackground(Color.YELLOW);
add(canvasEast, BorderLayout.EAST);
JPanel canvasWest = makePanel(Color.MAGENTA);
add(canvasWest, BorderLayout.WEST);
text = new JLabel("hey");
canvasSouth.add(text);
}
protected JPanel makePanel(Color color) {
JPanel pane = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(100, 100);
}
};
pane.setBackground(color);
return pane;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
}