2016-11-15 70 views
1

我试图做一个“句子随机数发生器”,当按下按钮时,通过从单独的文件夹和单独的文件中循环不同类型的单词来制作语法正确的句子,这可能没有任何意义。它也改变每个面板的颜色。我到目前为止能够让JButton出现,但我似乎无法弄清楚如何让面板出现?这是到目前为止我的代码的用户界面:如何让这些JLabel显示在我的JFrame中?

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


    } 
+0

请不要命名您的变量是这样的...你会后悔让这个习惯,我保证 – nhouser9

+0

见编辑回答 –

回答

4

您要添加topPanel两次到JFrame,这里

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

这里:

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

,并在第2除,您将其添加为JFrame当前使用BorderLayout,但不是因为您已经给它一个BoxLayout。

取而代之,只需添加一次topPanel,并以逻辑方式添加。另外考虑给你的JLabel的一些虚拟文本,如" ",这样当你第一次使用pack()时,它们会有一定的尺寸。


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

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

啊, 非常感谢你!然而,我现在看到,我对如何获得我需要的单词很无能,从: ArrayList > list = new ArrayList >(); list.add(_slst); int i = 0; list.get(i%5);进入JLabels。 :/ – sqwert

+0

@sqwert:这是一个单独的问题,应该在单独的问题中提出。这一个被回答。 –