【问题标题】:Printing random string from ArrayList class into JLabel text将 ArrayList 类中的随机字符串打印到 JLabel 文本中
【发布时间】:2014-05-05 13:39:37
【问题描述】:

我正在创建一个测验

我有一个类,其中包含 GUI 和用于放置将在我的应用程序中提出的问题的位置的代码。下面的类将用于保存JLabel 组件(以及其他 UI 元素),并且应该从给它的问题列表中显示一个随机问题。我已将这些“问题列表”中的每一个都保存为另一个班级的ArrayList

我知道您必须使用 随机数生成器从问题列表中选择一行,方法是生成一个索引号,然后使用它从数组中检索一行以使用作为JLabel中显示的文本

我不确定如何在实际代码中执行此操作,请任何人提供帮助。

我的代码如下:

package ZillionaireGUI;

import java.awt.Frame;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;


public class questionDialog extends JDialog {
private JLabel Question;
private JRadioButton answerThree;
private JRadioButton answerFour;
private JRadioButton answerTwo;
private JRadioButton answerOne;
public questionDialog(Frame parent) {
    super(parent);
}


public questionDialog(JFrame frame) {
    super(frame);
    initGUI();
}

private void initGUI() {
    try {

        getContentPane().setLayout(null);

        Question = new JLabel();
        getContentPane().add(Question);
        Question.setText("jLabel1");
        Question.setBounds(39, 127, 383, 29);

        answerOne = new JRadioButton();
        getContentPane().add(answerOne);
        answerOne.setText("jRadioButton1");
        answerOne.setBounds(26, 183, 93, 20);

        answerTwo = new JRadioButton();
        getContentPane().add(answerTwo);
        answerTwo.setText("jRadioButton1");
        answerTwo.setBounds(130, 183, 93, 20);

        answerThree = new JRadioButton();
        getContentPane().add(answerThree);
        answerThree.setText("jRadioButton1");
        answerThree.setBounds(247, 183, 93, 20);

        answerFour = new JRadioButton();
        getContentPane().add(answerFour);
        answerFour.setText("jRadioButton1");
        answerFour.setBounds(360, 183, 93, 20);

        ButtonGroup group = new ButtonGroup();
        group.add(answerOne);
        group.add(answerTwo);
        group.add(answerThree);
        group.add(answerFour);


        this.setSize(490, 393);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

import java.util.*;
public class Questions {
public static void main(String[] args) {
    ArrayList<String> SPOquestions = new ArrayList<String>(); // adding the sports questions for the quiz

    SPOquestions.add("Who won the 2005 Formula One World Championship?");
    SPOquestions.add("Which team has the most Formula One Constructors titles?");
    SPOquestions.add("In what year did Roger Federer win his first 'Grand Slam'?");
    SPOquestions.add("How many 'Grand Slams' has Rafael Nadal won?");
    SPOquestions.add("Who has scored the most amount of goals in the Premier League?");
    SPOquestions.add("Who has won the most World Cups in football?");
    SPOquestions.add("How many MotoGP titles does Valentino Rossi hold?");
    SPOquestions.add("Who was the 2013 MotoGP champion?");
    SPOquestions.add("Who won the 2003 Rugby World Cup?");
    SPOquestions.add("In rugby league, how many points are awarded for a try?");
    SPOquestions.add("Who is the youngest ever snooker World Champion?");
    SPOquestions.add("In snooker, what is the highest maximum possible break?");
    SPOquestions.add("How many majors has Tiger Woods won?");
    SPOquestions.add("In golf, what is the tournament between the USA and Europe called?");
    SPOquestions.add("How many World Championships has darts player Phil Taylor won?");
    SPOquestions.add("What is the maximum possible amount of points a player can earn from throwing three darts?");
    SPOquestions.add("How many gold medals did Michael Phelps win at the 2008 Beijing Olympics?");
    SPOquestions.add("Who won the 2012 Olympic 100 metres mens race?");
    SPOquestions.add("Which of these events are not a part of the heptathlon?");
    SPOquestions.add("When was the first modern Olympics held?");

    ArrayList<String> MUSquestions = new ArrayList<String>(); // adding the music questions

    MUSquestions.add("'Slash' was a member of which US rock band?");
    MUSquestions.add("Brian May was a member of which English rock band?");
    MUSquestions.add("What is the name of the music festival held annually in the town of Boom, Belgium?");
    MUSquestions.add("The rapper Tupac Shakuer '2Pac' died in which year?");
    MUSquestions.add("Which of these bands headlined the 2013 Glastonbury music festival?");
    MUSquestions.add("Which of these people designed the 'Les Paul' series of guitars?");
    MUSquestions.add("Keith Moon was a drummer for which English rock band?");
    MUSquestions.add("Kanye West has a total of how many Grammy awards?");
    MUSquestions.add("Beyonce Knowles was formally a member of which US group?");
    MUSquestions.add("In which US city was rapper 'Biggie Smalls' born?");
    MUSquestions.add("Michael Jackson's first number one single in the UK as a solo artist was what?");
    MUSquestions.add("The best selling album of all time in the UK is what?");
    MUSquestions.add("The best selling album of all time in the US is what?");
    MUSquestions.add("What is the artist known as 'Tiesto's real name?");
    MUSquestions.add("Which of these was not a member of The Beatles?");

    ArrayList<String> GENquestions = new ArrayList<String>(); // adding general knowledge questions

    GENquestions.add("Who was the second President of the United States?");
    GENquestions.add("The youngest son of Bob Marley was who?");
    GENquestions.add("In the film '8 Mile', the character portrayed by Eminem is known as what?");
    GENquestions.add("What is the capital city of New Zealand?");
    GENquestions.add("What is the capital city of Australia?");
    GENquestions.add("How many millilitres are there in an English pint?");
    GENquestions.add("What was the biggest selling game for the PS2 worldwide?");
    GENquestions.add("What is the last letter of the Greek alphabet?");
    GENquestions.add("Who created the television series Futurama?");
    GENquestions.add("A word which reads the same backwards as it does forwards is known as a what?");
    GENquestions.add("A 'baker's dozen' consists of how many items?");
    GENquestions.add("World War 1 officially occured on which date?");
    GENquestions.add("'Trouble and strife' is cockney rhyming slang for what?");
    GENquestions.add("Who was the last Prime Minister to hail from the labour party in the UK?");
    GENquestions.add("WalMart is the parent company of which UK based supermarket chain?");




}

}

【问题讨论】:

    标签: java swing user-interface arraylist jlabel


    【解决方案1】:

    首先,您需要根据数组的大小生成一个随机数。从Math.random() 中获取一个 0.0 - 1.0 范围内的随机值,并将其乘以问题数组的大小(例如,MUSquestions.size()) - 这将为您提供数组中的随机索引。

    那么。使用ArrayListget方法,传入索引,得到你想要的字符串。

    【讨论】:

    • 那么我是在我的arraylist类还是JLabel所在的类中生成这个随机数?
    • 可能是 JLabel 所在的类。类似label.setText(MUSquestions.get(Math.random()*MUSquestions.size()));
    • 嗯,我试过了,但它无法解决 MUSquestions 部分?
    • 您的main 方法永远不会创建questionDialog - 所以您甚至不会看到您的用户界面。您需要从main 创建您的questionDialog。当您这样做时,更改questionDialog 的构造函数以获取您要显示的四个问题。然后在调用构造函数的时候,使用我之前提到的部分sn-p。
    【解决方案2】:

    我肯定会这样使用 collections.shuffle : 列表 arrlist = new ArrayList();

      // populate the list
      arrlist.add("A");
      arrlist.add("B");
      arrlist.add("C");  
    
      System.out.println("Initial collection: "+arrlist);
    
      // shuffle the list
      Collections.shuffle(arrlist);
    
      System.out.println("Final collection after shuffle: "+arrlist);
    

    【讨论】:

      【解决方案3】:

      获取随机数:

      Random rand = new Random();
      
      // nextInt is normally exclusive of the top value,
      // so add 1 to make it inclusive
      int randomNum = rand.nextInt((max - min) + 1) + min;
      

      在这里阅读更多:How do I generate random integers within a specific range in Java?

      使用它来访问随机问题

      String randomQuestion=SPOquestions.get(randomNum);
      

      并在标签中显示

      Question.setText(randomQuestion);
      

      【讨论】:

        猜你喜欢
        • 2014-05-05
        • 1970-01-01
        • 2023-03-17
        • 2021-07-02
        • 2014-10-11
        • 1970-01-01
        • 2011-07-10
        • 2016-03-21
        相关资源
        最近更新 更多