本文介绍了如何让这些 JLabel 显示在我的 JFrame 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个句子随机化器",当按下按钮时,通过从单独的文件夹和单独的文件中循环不同类型的单词,生成一个语法正确的句子,这可能没有任何意义.它还在每个面板中交替颜色.到目前为止,我能够让 JButton 出现,但我似乎无法弄清楚如何让面板出现?到目前为止,这是我的 UI 代码:

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


    }

推荐答案

您在此处将 topPanel 添加到 JFrame 两次

You're adding topPanel twice to the JFrame, here

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

这里:

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

在第二个添加中,您添加它就像 JFrame 当前使用 BorderLayout,但不是因为您给它一个 BoxLayout.

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.

相反,只添加一次 topPanel,并以合乎逻辑的方式.还可以考虑为 JLabel 提供一些虚拟文本,例如 " ",以便在您第一次 pack() GUI 时它们具有一定的大小.

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.

此外,您的标签正在被添加,但它们没有大小且不透明,因此无法被看到.例如,在你的 for 循环中试试这个,自己看看:

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

这篇关于如何让这些 JLabel 显示在我的 JFrame 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:00