同时使用 JFrame 和 JLabel
Using both JFrame and JLabel
我正在尝试在 JFrame 中添加图片和文本。
当我同时添加图片和文字时,只显示图片。我的猜测是文字隐藏在它后面。当我注释掉 try / catch 块时,文本应该出现在中心。谁能帮我把文字显示在图片前面?
我自己去掉了图片的路径,没有错
public class Vindu {
public Vindu(){
JFrame F = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
F.add(label);
try{
F.setContentPane(new JLabel (new ImageIcon
(ImageIO.readnewfile))));
}catch (IOException e){
System.out.print("wrong place");
}
F.setResizable(false);
F.setSize(600, 400);
F.setVisible(true);
}
}
以下是在图像上获取 JLabel 的方法:
JPanel panel = new JPanel();
try{
panel.add(new JLabel (new ImageIcon
(ImageIO.read(new File("")))));
}catch (IOException e){
System.out.print("wrong place");
}
JLabel label = new JLabel ("hello world", JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
JFrame F = new JFrame("name");
F.add(panel);
F.setResizable(false);
F.setSize(600, 400);
F.setVisible(true);
首先,您向框架添加了一个组件 (label
)。到目前为止,一切都很好。
F.add(label);
事实上,您将它添加到框架的内容窗格中,因为框架只包含其内容窗格。见 documentation for JFrame
:
As a convenience, the add, remove, and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane. For example, you can add a child component to a frame as follows:
frame.add(child);
And the child will be added to the contentPane.
然后您用新组件替换了框架的内容窗格:
F.setContentPane(...);
这就是您丢失标签的原因。相反,将第二个标签(带有图像)添加到农场。
F.add(new JLabel(new ImageIcon(...)));
您有时还想了解 layout managers 以控制布局。
您可以通过阅读 JFrame documentation 找到问题的答案。来自该文档:
… you can add a child component to a frame as follows:
frame.add(child);
And the child will be added to the contentPane.
因此,您对 F.add(label)
的调用与 F.getContentPane().add(label)
相同。当然,您的代码接下来要做的就是完全替换 contentPane。
由于默认的 contentPane 使用 BorderLayout(如 JFrame 文档中所述),您可以保留该 contentPane,并将 BorderLayout 约束应用于每个组件:
F.add(label, BorderLayout.PAGE_START);
F.add(new JLabel(ImageIO.read(file)), BorderLayout.CENTER);
一些小的旁注:
- 在 Java 中,按照惯例,变量应始终以小写字母开头。所以你的 JFrame 变量应该是
f
或 frame
或 primaryFrame
。约定已完整描述 here.
- 异常对于解释出现问题的原因非常有用。至少,你应该在你的 catch 块中使用
e.printStackTrace()
,这样异常的信息就不会丢失。 (如果您遇到问题并希望 Stack Overflow 帮助您解决问题,堆栈跟踪也很有用。)
您可以将图标和文本添加到同一个 JLabel 中,并以图标为中心显示文本:
JLabel label = new JLabel( "Center Text" );
label.setIcon( new ImageIcon( "..." ) );
label.setHorizontalTextPosition( JLabel.CENTER );
label.setVerticalTextPosition( JLabel.CENTER );
frame.add( label );
我正在尝试在 JFrame 中添加图片和文本。
当我同时添加图片和文字时,只显示图片。我的猜测是文字隐藏在它后面。当我注释掉 try / catch 块时,文本应该出现在中心。谁能帮我把文字显示在图片前面?
我自己去掉了图片的路径,没有错
public class Vindu {
public Vindu(){
JFrame F = new JFrame("name");
JLabel label = new JLabel ("hello world", JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
F.add(label);
try{
F.setContentPane(new JLabel (new ImageIcon
(ImageIO.readnewfile))));
}catch (IOException e){
System.out.print("wrong place");
}
F.setResizable(false);
F.setSize(600, 400);
F.setVisible(true);
}
}
以下是在图像上获取 JLabel 的方法:
JPanel panel = new JPanel();
try{
panel.add(new JLabel (new ImageIcon
(ImageIO.read(new File("")))));
}catch (IOException e){
System.out.print("wrong place");
}
JLabel label = new JLabel ("hello world", JLabel.CENTER);
label.setAlignmentX(0);
label.setAlignmentY(0);
panel.add(label);
JFrame F = new JFrame("name");
F.add(panel);
F.setResizable(false);
F.setSize(600, 400);
F.setVisible(true);
首先,您向框架添加了一个组件 (label
)。到目前为止,一切都很好。
F.add(label);
事实上,您将它添加到框架的内容窗格中,因为框架只包含其内容窗格。见 documentation for JFrame
:
As a convenience, the add, remove, and setLayout methods of this class are overridden, so that they delegate calls to the corresponding methods of the ContentPane. For example, you can add a child component to a frame as follows:
frame.add(child);
And the child will be added to the contentPane.
然后您用新组件替换了框架的内容窗格:
F.setContentPane(...);
这就是您丢失标签的原因。相反,将第二个标签(带有图像)添加到农场。
F.add(new JLabel(new ImageIcon(...)));
您有时还想了解 layout managers 以控制布局。
您可以通过阅读 JFrame documentation 找到问题的答案。来自该文档:
… you can add a child component to a frame as follows:
frame.add(child);
And the child will be added to the contentPane.
因此,您对 F.add(label)
的调用与 F.getContentPane().add(label)
相同。当然,您的代码接下来要做的就是完全替换 contentPane。
由于默认的 contentPane 使用 BorderLayout(如 JFrame 文档中所述),您可以保留该 contentPane,并将 BorderLayout 约束应用于每个组件:
F.add(label, BorderLayout.PAGE_START);
F.add(new JLabel(ImageIO.read(file)), BorderLayout.CENTER);
一些小的旁注:
- 在 Java 中,按照惯例,变量应始终以小写字母开头。所以你的 JFrame 变量应该是
f
或frame
或primaryFrame
。约定已完整描述 here. - 异常对于解释出现问题的原因非常有用。至少,你应该在你的 catch 块中使用
e.printStackTrace()
,这样异常的信息就不会丢失。 (如果您遇到问题并希望 Stack Overflow 帮助您解决问题,堆栈跟踪也很有用。)
您可以将图标和文本添加到同一个 JLabel 中,并以图标为中心显示文本:
JLabel label = new JLabel( "Center Text" );
label.setIcon( new ImageIcon( "..." ) );
label.setHorizontalTextPosition( JLabel.CENTER );
label.setVerticalTextPosition( JLabel.CENTER );
frame.add( label );