admin管理员组

文章数量:1313387

I am trying to make a frame with a label, (I have created a separate class for the frame), but idk why the label isn´t showing in the frame...

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class Frame extends JFrame{

    Frame(){

        //set title of the frame
        this.setTitle("Frame Test nº 1");
 
        //exit out of application 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //define o tamanho da janela
        this.setSize(420, 420);
         this.setResizable(true);
 
        //create an icon
        ImageIcon logo = new ImageIcon("\\\\wsl.localhost\\Ubuntu\\home\\tech\\Java_Codes\\GUI_test\\d6ede8c8-a316-488b-8d87-60dcb1cdbe06.png");
 
        //change icon frame
        this.setIconImage(logo.getImage());//get the image created above
 
        //change the colour fron the background
        this.getContentPane().setBackground(new Color(0x123456));


        this.setVisible(true);
    }
}
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class gui{
    public static void main (String[] args){
        Frame frame = new Frame();
        JLabel label = new JLabel();
        label.setText("h");
        frame.add(label);

    }

}

I have tried to change the label inside the Frame class, but it didn't help... I also tried to put frame.setVisible(true); in the last line of the main...

I am trying to make a frame with a label, (I have created a separate class for the frame), but idk why the label isn´t showing in the frame...

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class Frame extends JFrame{

    Frame(){

        //set title of the frame
        this.setTitle("Frame Test nº 1");
 
        //exit out of application 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //define o tamanho da janela
        this.setSize(420, 420);
         this.setResizable(true);
 
        //create an icon
        ImageIcon logo = new ImageIcon("\\\\wsl.localhost\\Ubuntu\\home\\tech\\Java_Codes\\GUI_test\\d6ede8c8-a316-488b-8d87-60dcb1cdbe06.png");
 
        //change icon frame
        this.setIconImage(logo.getImage());//get the image created above
 
        //change the colour fron the background
        this.getContentPane().setBackground(new Color(0x123456));


        this.setVisible(true);
    }
}
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class gui{
    public static void main (String[] args){
        Frame frame = new Frame();
        JLabel label = new JLabel();
        label.setText("h");
        frame.add(label);

    }

}

I have tried to change the label inside the Frame class, but it didn't help... I also tried to put frame.setVisible(true); in the last line of the main...

Share Improve this question asked Jan 30 at 23:55 silent stormsilent storm 111 silver badge1 bronze badge 4
  • 1 Hello silent storm, welcome, try sizing the label with: label.setSize( 150, 25 );, as a comment, all the “this” in your code are extra, and if there is no good reason not to, create the tag inside the Frame class. – Marce Puente Commented Jan 31 at 0:03
  • 1 1) Don't call you class Frame. There is already an AWT class with that name, so it is confusing. Class names should be descriptive. 2) All components should be added to the frame BEFORE the frame is made visible. You add the label AFTER the frame is visible so the label has a size of (0, 0) so there is nothing to paint. – camickr Commented Jan 31 at 0:32
  • 1 And try using a different color. Say Color.yellow – g00se Commented Jan 31 at 0:39
  • javadoc of add(): "This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to display the added component." → add frame.validate() after adding the label or consider point 2 of camickr's comment; and previous comment (dark gray on dark blue is hard to see) -- also not recommended to extend a JFrame (or any class) just to call its methods – user85421 Commented Jan 31 at 17:41
Add a comment  | 

1 Answer 1

Reset to default 4

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

I reworked your code and came up with the following GUI.

Seeing one letter is tough, so I increased the font size.

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Swing uses layout managers to position Swing components.

Don't extend Swing components unless you override one or more of the class methods.

Don't name your classes the same as Java standard classes, like java.awt.Frame. It's confusing for readers of your code and yourself after a few months.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabelExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JLabelExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Frame Test nº 1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(createMainPanel(), BorderLayout.CENTER);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(50, 200, 50, 200));
        panel.setBackground(new Color(0x123456));

        JLabel label = new JLabel("h");
        label.setForeground(Color.WHITE);
        label.setFont(panel.getFont().deriveFont(36f));
        panel.add(label);

        return panel;
    }

}

本文标签: