简体   繁体   English

swing和GUI组件未出现

[英]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. 我试图设置一些JPanels和TextAreas来显示,但是花了两天时间阅读API并试图将面板添加到框架中并将textareas添加到面板中之后,却没有任何显示。.我完全感到困惑。 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. 您已经在JFrame中添加了一些东西,然后就可以看到它们了。 If you do that, you need to revalidate your JFrame so it knows to redo its layout. 如果这样做,则需要重新验证JFrame,以便它知道重做其布局。

You could also just wait to show your JFrame until after you've added everything. 您还可以等到添加所有内容后再显示JFrame。

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. 尝试运行此代码,然后取消对revalidate()的调用以查看区别。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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