Home > Mobile >  I'm not able to see anything in JFrame
I'm not able to see anything in JFrame

Time:12-19

I am not able to get the result that I want. when I open it, my frame goes on the top-left-hand side corner. when I expand it, it is blank. what am I doing wrong?

import javax.swing.*;
import java.awt.image.BufferedImage;

public class About extends JFrame {
    public static JFrame contentPane;
    private JLabel textField;
    private JLabel VerNum;

    public About() {
        init();
    }

    public static void main(String[] args) {
        new About().setVisible(true);
    }

    void init() {
        contentPane = new JFrame("About");
        contentPane.setLayout(null);

        textField = new JLabel();
        textField.setIcon(new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB)));
        textField.setBounds(324, 122, 766, 470);
        contentPane.add(textField);

        VerNum = new JLabel();
        VerNum.setBounds(473, 607, 130, 26);
        VerNum.setText("Version Number: test");
        contentPane.add(VerNum);

        JLabel label = new JLabel("");
        label.setBounds(162, 55, 61, 16);
        contentPane.add(label);

        JButton btnNewButton = new JButton("Close");
        btnNewButton.setBounds(6, 937, 117, 29);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Exit Program");
        btnNewButton_1.setBounds(877, 937, 117, 29);
        contentPane.add(btnNewButton_1);
        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(311, 681, 61, 16);
        contentPane.add(lblNewLabel);

        JButton btnNewButton_2 = new JButton("Get Latest SDK Version");
        btnNewButton_2.setBounds(324, 937, 232, 29);
        contentPane.add(btnNewButton_2);
        contentPane.setVisible(true);
    }
}

Sorry if my code is very long.

Here are the screenshots: Screenshot 1 (before I expand the frame):

Screenshot1 Screenshot 2 (after I expand the frame):

Screenshot2

I don't get any errors.

CodePudding user response:

Try this :

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Objects;

public class About {
    public static JFrame contentPane;
    private JLabel textField;
    private JLabel VerNum;

    public static void main(String[] args)
    {
        new About();
    }
    public About() {
        init();

    }
    void init() {
        contentPane = new JFrame("About");
        contentPane.setLayout(null);

        textField = new JLabel("ICON");
        //textField.setIcon(new ImageIcon(Objects.requireNonNull(About.class.getResource("/com/img/JUIT.png"))));
        textField.setBounds(324, 122, 766, 470);
        contentPane.add(textField);

        VerNum = new JLabel();
        VerNum.setBounds(473, 607, 130, 26);
        VerNum.setText("Version Number: "  Installer.getInstallerVersion());
        contentPane.add(VerNum);

        JLabel label = new JLabel("");
        label.setBounds(162, 55, 61, 16);
        contentPane.add(label);

        JButton btnNewButton = new JButton("Close");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                contentPane.setVisible(false);

            }
        });
        btnNewButton.setBounds(6, 937, 117, 29);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Exit Program");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnNewButton_1.setBounds(877, 937, 117, 29);
        contentPane.add(btnNewButton_1);
        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(311, 681, 61, 16);
        contentPane.add(lblNewLabel);

        JButton btnNewButton_2 = new JButton("Get Latest SDK Version");
        btnNewButton_2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                URL url = null;
                InputStream is = null;
                try {
                    url = new URL("https://www.filehosting.org/file/details/973233/ver.txt");
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    is = url.openStream();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                lblNewLabel.setText(is.toString());

            }
        });
        btnNewButton_2.setBounds(324, 937, 232, 29);
        contentPane.add(btnNewButton_2);
        contentPane.setVisible(true);
        contentPane.setSize(1200, 1200);
        contentPane.setPreferredSize(contentPane.getSize());
        contentPane.pack();
    }
}

I think for your txt file, try to use pastebin with raw url !

  • Related