简体   繁体   中英

swing and GUI components not appearing

I'm a complete noobie with swing. I'm trying to set a few JPanels and TextAreas to show up but after spending 2 days reading the APIs and trying to add panels to frames and textareas to panels and nothing is showing up.. I'm utterly confused. If anyone could explain how is the best way to do this I would be very grateful

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class GUI {

public static void main(String[] args) {


    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLayout(new FlowLayout());                           // J FRAME

    JPanel panel = new JPanel();                                               // first panel on the left
    panel.setLayout(new BoxLayout(panel, 1));
//  frame.getContentPane().setBackground(Color.red);
    frame.add(panel);

    JLabel surname = new JLabel();
    JLabel initial = new JLabel();
    JLabel ext    = new JLabel();
    surname.setOpaque(true);
    initial.setOpaque(true);
    ext.setOpaque(true);
    frame.add(surname);
    panel.add(initial);
    panel.add(ext);


    JTextArea table = new JTextArea();
    table.setEditable(false);
    panel.add(table);
    table.setVisible(true);

You're adding stuff to the JFrame after it's already visible. If you do that, you need to revalidate your JFrame so it knows to redo its layout.

You could also just wait to show your JFrame until after you've added everything.

Edit: Here is an example program that shows what I'm talking about. Try running this, then take out the call to revalidate() to see the difference.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Test {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JButton show = new JButton("Show");
        show.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent showE) {
                frame.add(new JLabel("Test"), BorderLayout.SOUTH);
                frame.revalidate(); //tell the JFrame to redo its layout!
            }

        });

        frame.add(show);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }
}

You are adding an empty elements like:

JLabel surname = new JLabel();

Your elements is already added but have nothing to be display.

Try :

JLabel surname = new JLabel("UserName");
JLabel initial = new JLabel("Iinitial");
JLabel ext = new JLabel("Ext");
JTextArea table = new JTextArea(10, 5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM