lundi 8 février 2016

Linking an entry in a TextField to a randomly generated number?

I'm trying to make a program that is similar to Quizlet. A guessing game basically. I'm confused as to how I would do or even approach this.

I want a program where a random Number is generated in one TextField (which I have done), and the number that is generated is assigned to a string. I have made a list of characters and actions which should be assigned to any number between 1 and 50. So when the randomly generated number shows, the player would have to guess which character or action the number shown is associated with. There's a TextField below it where you guess.

   public class NumberSide {



public NumberSide(){

frame();

}

public void frame(){


JFrame f2 = new JFrame();
f2.setVisible(true); //Set Visible
f2.setSize(600,400); //Set Size
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add Close Function


JPanel p2 = new JPanel(new GridBagLayout()); //Create Panel
GridBagConstraints gbc = new GridBagConstraints();
p2.setBackground(Color.BLACK); 
//Code for Panel below.
f2.add(p2); //Add panel to frame.


JLabel TextAssigned = new JLabel();
//Code for Text Assigned below
TextAssigned.setText("What Character/Action is assigned with the number above?");
gbc.gridx = 3;
gbc.gridy = 2;
TextAssigned.setForeground(Color.WHITE);
p2.add(TextAssigned, gbc);


JTextField RandomNumber = new JTextField();
//Code for randum number below.
gbc.gridx = 3;
gbc.gridy = 1;
Random rn = new Random();
RandomNumber.setText(Integer.toString(rn.nextInt(51)));
RandomNumber.setEditable(false);
RandomNumber.setFont(new Font("Sans serif", Font.TRUETYPE_FONT, 60));
RandomNumber.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {  

    //Code for function goes here.  

    }}); 
p2.add(RandomNumber, gbc); //Adding to the panel, after done with all functions. 


JTextField AnswerBox = new JTextField();
//Code for Answer Box below.
AnswerBox.setSize(100,100);
gbc.gridx = 3;
gbc.gridy = 4;
AnswerBox.requestFocus(true);
AnswerBox.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {                       

    //Code for function goes here.        

    }}); 
AnswerBox.setPreferredSize(new Dimension (100,30)); //Set Dimensions
p2.add(AnswerBox, gbc); //Add Text Field to panel


JButton OK = new JButton("Ok");
//Code for OK Button below.
gbc.gridx = 3;
gbc.gridy = 5;
OK.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {                       

    //Code for function goes here.
    RandomNumber.setText(Integer.toString(rn.nextInt(51)));
    String input = AnswerBox.getText();


    }});    
p2.add(OK, gbc);


JButton Quit = new JButton("Quit");
//Code for Quit Button below.
gbc.gridx = 3;
gbc.gridy = 9;  
Quit.addActionListener(new ActionListener(){
    //Code for function goes here.
        public void actionPerformed(ActionEvent e) {                       
    System.exit(0);           
    }});    
p2.add(Quit, gbc);//Adding to the panel, after done with all functions. 


}

}

How would I go about doing this? Here's my GUI...

GUI!




Aucun commentaire:

Enregistrer un commentaire