简体   繁体   中英

How to Get these JLabels to be Displayed in my JFrame?

I am attempting to make a "sentence randomizer" that, when a button is pressed, makes a grammatically correct sentence, that may not make any sense, by looping different types of words from a separate folder and separate files. It also alternates colors in each panel. I so far am able to get the JButton to show up, but I can't seem to figure out how to get the panels to appear? Here is my code so far for the UI:

package user_interface;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import code.sentence;
import user_interface.RandomButtonListener;

public class sentenceUI {

    private sentence _s;
    private JButton _rando;

    public sentenceUI() {
        _s = new sentence(this);
        JFrame f = new JFrame("Ryan Ellis' Lab 9");
        f.setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS));


        JPanel topPanel = new JPanel();
        f.add(topPanel);

        JPanel lowerPanel = new JPanel();
        f.add(lowerPanel);

        _rando = new JButton("Random Sentence");
        _rando.addActionListener(new RandomButtonListener(_s, this));
        lowerPanel.add(_rando);


        Color c1 = Color.BLUE;
        Color c2 = new Color( 255 - c1.getRed(), 255 - c1.getGreen(), 255 - c1.getBlue()); 
        for(int i = 0; i < 8; i++){
            JLabel _l = new JLabel();
            _l.setBackground(c1);
            _l.setForeground(c2);
            Color temp = c1;
                    c1 = c2;
                    c2 = temp;
            _l.setBorder(BorderFactory.createEmptyBorder(0,0,8,5));
            _l.setFont(new Font("Comic Sans", Font.BOLD, 18));
        topPanel.add(_l);
        }

        ArrayList<String> _slst = new ArrayList<String>();
            _slst.add("WordLists/adjectives.txt");
            _slst.add("WordLists/adverbs.txt");
            _slst.add("WordLists/determiners.txt");
            _slst.add("WordLists/nouns.txt");
            _slst.add("WordLists/verbs.txt");

        ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
        list.add(_slst);
            int i = 0;
            list.get(i % 5);

            f.add(topPanel, BorderLayout.PAGE_START);
            f.add(lowerPanel, BorderLayout.PAGE_END);

        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();

    }

    private void createRButton(String string, JPanel lowerPanel) {
        createRButton("Random", lowerPanel);


    }

You're adding topPanel twice to the JFrame, here

JPanel topPanel = new JPanel();
f.add(topPanel);

and here:

f.add(topPanel, BorderLayout.PAGE_START);
f.add(lowerPanel, BorderLayout.PAGE_END);

and in the 2nd addition, you're adding it as if the JFrame currently used a BorderLayout, but it's not since you've given it a BoxLayout.

Instead, only add the topPanel once, and in a logical way. Also consider giving your JLabel's some dummy text, such as " " so that they have some size when you first pack() your GUI.


Also, your labels are being added but they have no size and are non-opaque and so can't be seen. For example try this within your for loop to see for yourself:

JLabel _l = new JLabel("Label " + i);  // to give labels size 
_l.setOpaque(true);   // so you can see the background color
_l.setBackground(c1);
_l.setForeground(c2);

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