为什么在构造的 JPanel 中表达了组件的某些属性而不表达其他属性?
Why are some properties of a component, in a constructed JPanel, expressed but not others?
我正在开始一个程序,在该程序中我构建了一个 JFrame 并使用我创建的另一个名为 StartPanel 的 class 添加了一个 JPanel。当 JPanel 构造时,它会添加一个在 StartPanel class 中定义的 JLabel。然后将此面板添加到 JFrame。标签的一些属性在 GUI 中表达(它将自己添加到面板,可以在其上添加红色边框,可以更改文本)但有些则不是。例如,我无法更改标签的位置或大小。
到目前为止,我所读到的关于这个问题的所有内容(我认为)都同意我的方法,或者没有太多澄清。此外,我还为 StartPanel 和标签添加了边框,以可视化问题并确认标签没有改变它的大小。我无法调整标签的大小或位置属性。我该如何解决这个问题并控制标签的属性?
此外,我知道我可以通过不通过单独的 class 构建面板并在同一函数中声明标签来规避整个问题。然而,这个项目将相当大,所以我更愿意有一个 classes 系统,我可以根据需要构建一个面板。
打码时间:
我在其中使用 class:
构建框架和面板
public class StartFrame {
public StartFrame() {
JFrame frame = new JFrame("Constuctor tests");
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel startPanel = new StartPanel(); //This is where I construct the panel
startPanel.setVisible(true);
startPanel.setBorder(BorderFactory.createLineBorder(Color.blue)); //Visualize the panel
frame.add(startPanel);
}
}
开始面板class:
public class StartPanel extends JPanel {
public StartPanel() {
JLabel label = new JLabel("This is a label");
label.setLocation(100, 100); //It is not at 100,100
label.setSize(200, 100); //It is not a box this large
label.setBorder(BorderFactory.createLineBorder(Color.red)); //Done as to visualize it
label.setVisible(true);
add(label);
}
}
最后放一张产品图片:
The final product
非常感谢您的帮助!
您可以通过布局管理器控制元素的大小和布局。您可以阅读all about it here。
首先,让我们来看看为什么您的屏幕看起来像那样。
JFrame 的默认布局是 BorderLayout。 BorderLayout 首先采用 child(在本例中为 JPanel),将其置于中心并拉伸以占据容器的整个宽度(在本例中为 JFrame)。这就是为什么您的 StartPanel 占据了 JFrame 的全部大小。 Here you can read more about how to use the border layout.
JPanel 的默认布局是 FlowLayout。在 flow-layout 中,每个 child 元素(在本例中为 JLabel)都被添加到中心顶部。当添加更多 children 时,它们会并排添加到一行中。 FlowLayout 让它的 children 占据他们想要的大小。在这种情况下,JLable 只需要一个小矩形来显示文本,这就是 JLabel 的大小。 Go here to learn more about the FlowLayout.
我正在开始一个程序,在该程序中我构建了一个 JFrame 并使用我创建的另一个名为 StartPanel 的 class 添加了一个 JPanel。当 JPanel 构造时,它会添加一个在 StartPanel class 中定义的 JLabel。然后将此面板添加到 JFrame。标签的一些属性在 GUI 中表达(它将自己添加到面板,可以在其上添加红色边框,可以更改文本)但有些则不是。例如,我无法更改标签的位置或大小。
到目前为止,我所读到的关于这个问题的所有内容(我认为)都同意我的方法,或者没有太多澄清。此外,我还为 StartPanel 和标签添加了边框,以可视化问题并确认标签没有改变它的大小。我无法调整标签的大小或位置属性。我该如何解决这个问题并控制标签的属性?
此外,我知道我可以通过不通过单独的 class 构建面板并在同一函数中声明标签来规避整个问题。然而,这个项目将相当大,所以我更愿意有一个 classes 系统,我可以根据需要构建一个面板。
打码时间:
我在其中使用 class:
构建框架和面板public class StartFrame {
public StartFrame() {
JFrame frame = new JFrame("Constuctor tests");
frame.setSize(800, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JPanel startPanel = new StartPanel(); //This is where I construct the panel
startPanel.setVisible(true);
startPanel.setBorder(BorderFactory.createLineBorder(Color.blue)); //Visualize the panel
frame.add(startPanel);
}
}
开始面板class:
public class StartPanel extends JPanel {
public StartPanel() {
JLabel label = new JLabel("This is a label");
label.setLocation(100, 100); //It is not at 100,100
label.setSize(200, 100); //It is not a box this large
label.setBorder(BorderFactory.createLineBorder(Color.red)); //Done as to visualize it
label.setVisible(true);
add(label);
}
}
最后放一张产品图片: The final product
非常感谢您的帮助!
您可以通过布局管理器控制元素的大小和布局。您可以阅读all about it here。
首先,让我们来看看为什么您的屏幕看起来像那样。
JFrame 的默认布局是 BorderLayout。 BorderLayout 首先采用 child(在本例中为 JPanel),将其置于中心并拉伸以占据容器的整个宽度(在本例中为 JFrame)。这就是为什么您的 StartPanel 占据了 JFrame 的全部大小。 Here you can read more about how to use the border layout.
JPanel 的默认布局是 FlowLayout。在 flow-layout 中,每个 child 元素(在本例中为 JLabel)都被添加到中心顶部。当添加更多 children 时,它们会并排添加到一行中。 FlowLayout 让它的 children 占据他们想要的大小。在这种情况下,JLable 只需要一个小矩形来显示文本,这就是 JLabel 的大小。 Go here to learn more about the FlowLayout.