繁体   English   中英

如何使这些JLabel显示在我的JFrame中?

[英]How to Get these JLabels to be Displayed in my JFrame?

我试图制作一个“句子随机化器”,通过按下一个单独的文件夹和单独的文件中的不同类型的单词,使按下按钮时的语法正确的句子变得毫无意义。 它还在每个面板中交替显示颜色。 到目前为止,我可以显示JButton,但似乎无法弄清楚如何显示面板? 到目前为止,这是我的用户界面代码:

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,在这里

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

和这里:

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

在第二个版本中,您将其添加为好像JFrame当前使用BorderLayout一样,但这不是因为您给它提供了BoxLayout。

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


此外, 添加您的标签,但他们没有大小和是非不透明,所以无法看到。 例如,在您的for循环中尝试以下操作,自己看看:

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

暂无
暂无

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

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