问题是我不能同时在Frame中添加JPanel和JLabel。当我使用以下代码时,只有MyPanel
可见。myFrame.add(myLabel);myFrame.add(myPanel);myFrame.setVisible(true);
执行时:myFrame.add(myLabel);myFrame.setVisible(true);myFrame.add(myPanel);
则只有CCD_ 4可见。
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
MyLabel myLabel = new MyLabel();
MyPanel myPanel = new MyPanel();
myFrame.add(myLabel);
myFrame.add(myPanel);
myFrame.setVisible(true);
}
}
public class MyFrame extends JFrame {
MyFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //exit out of application.
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29
this.setSize(750, 750); //set the size.
this.setResizable(true);//resize the frame.
this.setTitle("Welocme to new world."); //set the Title.
ImageIcon imageIcon = new ImageIcon("logo.png");
this.setIconImage(imageIcon.getImage());
this.getContentPane().setBackground(new Color(217, 217, 217));
this.setBackground(Color.YELLOW);//this.setVisible(true);//Make the frame visible.
}}
import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel {
MyPanel() {
this.setBackground(Color.white);
this.setBounds(2,2,25,25);
}}
import javax.swing.*;
import java.awt.*;
public class MyLabel extends JLabel {
MyLabel(){
this.setText("<html>Heaven <br/>Heaven's body"<br/> Whirl around me <br/>Make me wonder</html>");
//,SwingConstants.CENTER);
//https://stackoverflow.com/questions/1090098/newline-in-jlabel
//How to print multi line in java
ImageIcon image = new ImageIcon("Cosmogony_Björk_Cover.jpg");
this.setIcon(image);
//jLabel.setForeground(new Color(217,217,217));
this.setForeground(Color.BLACK);
this.setFont(new Font("helvetica",Font.PLAIN,18));
this.setBackground(Color.gray);
this.setOpaque(true);
//jLabel.setVerticalTextPosition(JLabel.TOP); Set the relative text position of the label.
//jLabel.setBorder();
this.setVerticalAlignment(JLabel.CENTER);
this.setHorizontalAlignment(JLabel.CENTER);
}}
JFrame
的默认布局管理器是BorderLayout
。
由于您没有更改JFrame
的布局管理器,这也是代码段中使用的当前布局管理器。
通常,在使用BorderLayout
时,您可以指定在添加组件时应填充BorderLayout
的哪个区域。这通常通过完成
frame.add(component, BorderLayout.CENTER);
请注意add()
方法中的面积规格,它"告诉"BorderLayout放置构件的位置。
然而,问题是这样的。如果将add()
方法与BorderLayout
结合使用而不指定零部件的放置,则它将始终将零部件放置在BorderLayout.CENTER
中。(导致当前要更换的组件(
要解决此问题,请执行以下操作之一:
明确指定位置,这样两个组件都会显示:
frame.add(component1, BorderLayout.PAGE_START); frame.add(component2, BorderLayout.CENTER);
或者使用不同的布局管理器,它将为您处理放置。例如
FlowLayout
JPanel contentPanel = new JPanel(); // JPanel uses flowlayout by default! contentPanel.add(component1); contentPanel.add(component2); myFrame.setContentPane(contentPanel);
您也可以明确设置布局:
Container contentPane = myFrame.getContentPane(); // creates new FlowLayout and sets on content pane contentPane.setLayout(new FlowLayout()); contentPane.add(component1); contentPane.add(component2);
旁注:
浏览"在容器中布局组件"Oracle教程,该教程将为您提供有关哪些布局管理器以及如何使用它们的更多信息。
在构建GUI时,
JFrame
上的setVisible()
应该是添加所有组件后要做的最后一件事。因为如果在将框架设置为可见之后添加组件,则如果不告诉swing某些内容发生了更改,则更改将不会立即生效。当正确使用Swing布局管理器时,应该不需要使用
setBounds(...)
或setSize()
之类的东西。添加所有组件后,在JFrame
上调用pack()
是首选方法。这将根据内部组件的优选尺寸来确定JFrame
的尺寸。