Java Swing JLabel没有出现

时间:2013-12-06 06:17:27

标签: java swing jpanel jlabel

JLabel nameLabel不会出现在gui上。我曾尝试使用SwingWorker,因此并发性不是问题。当Jlabel添加到内部JPanel的BorderLayout的West部分时,面板为标签腾出空间,但标签实际上并未出现。如果有人遇到这个问题或知道如何修复它,我会非常感激

感谢

public class Frame_2 
{
    JButton done = new JButton("Next Page");
    JLabel topLabel = new JLabel("2. Contact Information");

    JTextField nameInput = new JTextField();
    JTextField mailingAddressInput = new JTextField();
    JTextField cityStateInput = new JTextField();
    JTextField telephoneInput = new JTextField();
    JTextField faxInput = new JTextField();
    JTextField eMailInput = new JTextField();

    JLabel nameLabel = new JLabel("Name:");

    JPanel internal = new JPanel();
    JPanel northGrid = new JPanel();


    int keepTrack = 0;

    String name;
    String mailingAddress;
    String cityState;
    String telephone;
    String fax;
    String eMail;

    public void buildFrame_2(JFrame frame)
    {
        nameLabel.setText("Name:");
        nameInput.setText("Name:");
        mailingAddressInput.setText("Mailing Address:");
        cityStateInput.setText("City, State, Zip Code:");
        telephoneInput.setText("Telephone:");
        faxInput.setText("Fax:");
        eMailInput.setText("E-mail:");
        internal.setLayout(new BorderLayout());
        topLabel.setHorizontalAlignment(SwingConstants.CENTER);
        frame.setLayout(new BorderLayout());
        northGrid.setLayout(new GridLayout(12,2));

        nameInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    name = nameInput.getText();
                    System.out.println(name);
                    nameInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    


        mailingAddressInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    mailingAddress = mailingAddressInput.getText();
                    System.out.println(mailingAddress);
                    mailingAddressInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));        
        cityStateInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    cityState = cityStateInput.getText();
                    System.out.println(cityState);
                    cityStateInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        telephoneInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    telephone = telephoneInput.getText();
                    System.out.println(telephone);
                    telephoneInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        faxInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    fax = faxInput.getText();
                    System.out.println(fax);
                    faxInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    
        eMailInput.addActionListener((new ActionListener()
        {

            public void actionPerformed(ActionEvent e) 
            {
                    eMail = eMailInput.getText();
                    System.out.println(eMail);
                    eMailInput.setVisible(false);
                    keepTrack++;
                    if(keepTrack == 6)
                    {
                        askForSecondary(internal);
                    }
            }
        }));    

        northGrid.add(nameInput);
        northGrid.add(mailingAddressInput);
        northGrid.add(cityStateInput);
        northGrid.add(telephoneInput);
        northGrid.add(faxInput);
        northGrid.add(eMailInput);

        internal.add(nameLabel, BorderLayout.WEST);

        //internal.add(northGrid, BorderLayout.CENTER);



        frame.add(internal, BorderLayout.CENTER);
        frame.add(done, BorderLayout.SOUTH);
        frame.add(topLabel, BorderLayout.NORTH);

    }

1 个答案:

答案 0 :(得分:1)

Frame with visible label

使用以下代码,标签对我来说很好。

JFrame f = new JFrame();
Frame_2 f2 = new Frame_2();
f2.buildFrame_2(f);
f.pack();
f.setVisible(true);

但是,如果删除pack()调用,则框架默认为其最小尺寸,以便无法看到内容。

Unpacked frame

我的猜测是你只需要调整框架大小。但是,如果您想要更准确的答案,您真的应该提供SSCCE。