BorderLayout 显示边框线
BorderLayout showing border lines
我的Java GUI不是很好,需要寻求帮助。
我打算在 BorderLayout
的西边添加图片,中间是我的内容,按钮在底部。
我创建了一个空边框,以便在我的南面板与西面板和中心面板之间制作一些填充物。现在我只想在南边界的顶部添加一条线。
如下图所示,西面板和中间面板之间也有一条线,我怎样才能去掉那条线并保持横跨南面板顶部的线?
附上我的代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
public static void main(String[] args) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JFrame frame = new JFrame();
panel2.add( new JLabel( "WEST <will be adding image here>" ));
panel3.add( new JLabel( "CENTER <contents>"));
panel4.add( new JLabel( "SOUTH <will be adding buttons>" ));
panel1.add(panel2, BorderLayout.WEST);
panel1.add(panel3, BorderLayout.CENTER);
panel1.add(panel4, BorderLayout.SOUTH);
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createRaisedBevelBorder());
panel4.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(510,390);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}
试试这个:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
public static void main(String[] args) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JPanel panel5 = new JPanel(new FlowLayout());
JFrame frame = new JFrame();
panel4.add( new JLabel( "WEST <will be adding image here>" ));
panel5.add( new JLabel( "CENTER <contents>"));
panel3.add( new JLabel( "SOUTH <will be adding buttons>" ));
panel1.add(panel2, BorderLayout.CENTER);
panel1.add(panel3, BorderLayout.SOUTH);
panel2.add(panel4, BorderLayout.WEST);
panel2.add(panel5, BorderLayout.CENTER);
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(510,390);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}
要删除 WEST 和 CENTER 之间的边界,只需删除它们的边界
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createRaisedBevelBorder());
如果您想保留边框与边框的边框,请改为 panel1
添加边框。
至于南,如果你想要"to add a line across on top of the south border"并保持空边框,使用:
panel4.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(10, 10, 10, 10),
BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK)));
或 createRaisedBevelBorder()
而不是 createMatteBorder
.
请记住,您可以切换边框的顺序及其样式。有关详细信息,请参阅 tutorial。
`以下代码将组件添加到框架的内容窗格。
因为内容面板默认使用 BorderLayout class,所以代码
不需要设置布局管理器。完整的程序
在 BorderLayoutDemo.java 文件中。
...//Container pane = aFrame.getContentPane()...
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);
//Make the center component big, since that's the
//typical usage of BorderLayout.
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
Specify the component's location (for example, BorderLayout.LINE_END) as one of the arguments to the add method. If this component is missing from a container controlled by a BorderLayout object, make sure that the component's location was specified and no another component was placed in the same location.
All tutorial examples that use the BorderLayout class specify the component as the first argument to the add method. For example:
add(component, BorderLayout.CENTER) //preferred
However, the code in other programs specifies the component as the second argument. For example, here are alternate ways of writing the preceding code:
add(BorderLayout.CENTER, component) //valid but old fashioned
or
add("Center", component) //valid but error prone
我的Java GUI不是很好,需要寻求帮助。
我打算在 BorderLayout
的西边添加图片,中间是我的内容,按钮在底部。
我创建了一个空边框,以便在我的南面板与西面板和中心面板之间制作一些填充物。现在我只想在南边界的顶部添加一条线。
如下图所示,西面板和中间面板之间也有一条线,我怎样才能去掉那条线并保持横跨南面板顶部的线?
附上我的代码:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
public static void main(String[] args) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new FlowLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JFrame frame = new JFrame();
panel2.add( new JLabel( "WEST <will be adding image here>" ));
panel3.add( new JLabel( "CENTER <contents>"));
panel4.add( new JLabel( "SOUTH <will be adding buttons>" ));
panel1.add(panel2, BorderLayout.WEST);
panel1.add(panel3, BorderLayout.CENTER);
panel1.add(panel4, BorderLayout.SOUTH);
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createRaisedBevelBorder());
panel4.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(510,390);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}
试试这个:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class test {
public static void main(String[] args) {
JPanel panel1 = new JPanel(new BorderLayout());
JPanel panel2 = new JPanel(new BorderLayout());
JPanel panel3 = new JPanel(new FlowLayout());
JPanel panel4 = new JPanel(new FlowLayout());
JPanel panel5 = new JPanel(new FlowLayout());
JFrame frame = new JFrame();
panel4.add( new JLabel( "WEST <will be adding image here>" ));
panel5.add( new JLabel( "CENTER <contents>"));
panel3.add( new JLabel( "SOUTH <will be adding buttons>" ));
panel1.add(panel2, BorderLayout.CENTER);
panel1.add(panel3, BorderLayout.SOUTH);
panel2.add(panel4, BorderLayout.WEST);
panel2.add(panel5, BorderLayout.CENTER);
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel1);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setSize(510,390);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}
要删除 WEST 和 CENTER 之间的边界,只需删除它们的边界
panel2.setBorder(BorderFactory.createRaisedBevelBorder());
panel3.setBorder(BorderFactory.createRaisedBevelBorder());
如果您想保留边框与边框的边框,请改为 panel1
添加边框。
至于南,如果你想要"to add a line across on top of the south border"并保持空边框,使用:
panel4.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createEmptyBorder(10, 10, 10, 10),
BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK)));
或 createRaisedBevelBorder()
而不是 createMatteBorder
.
请记住,您可以切换边框的顺序及其样式。有关详细信息,请参阅 tutorial。
`以下代码将组件添加到框架的内容窗格。
因为内容面板默认使用 BorderLayout class,所以代码
不需要设置布局管理器。完整的程序
在 BorderLayoutDemo.java 文件中。
...//Container pane = aFrame.getContentPane()...
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);
//Make the center component big, since that's the
//typical usage of BorderLayout.
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
Specify the component's location (for example, BorderLayout.LINE_END) as one of the arguments to the add method. If this component is missing from a container controlled by a BorderLayout object, make sure that the component's location was specified and no another component was placed in the same location.
All tutorial examples that use the BorderLayout class specify the component as the first argument to the add method. For example:
add(component, BorderLayout.CENTER) //preferred
However, the code in other programs specifies the component as the second argument. For example, here are alternate ways of writing the preceding code:
add(BorderLayout.CENTER, component) //valid but old fashioned
or
add("Center", component) //valid but error prone