簡體   English   中英

Java不能顯示JPanel,但是能顯示JLabel?

[英]Java cannot display the JPanel, but can display JLabel?

問題是我不能同時在 Frame 中添加 JPanel 和 JLabel。 當我使用以下代碼時,只有MyPanel可見。 myFrame.add(myLabel);myFrame.add(myPanel);myFrame.setVisible(true);

當我執行時: myFrame.add(myLabel);myFrame.setVisible(true);myFrame.add(myPanel); 只有myLabel可見。

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的首選方法。 這將根據內部組件的首選大小調整JFrame的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM