samedi 30 septembre 2017

How to add listener or initialize a class with the clicking of OK in showMessageDialog in Java?

I am trying to output a showMessageDialog stating that a color will be randomly chosen and stating the color chosen. Once the user clicks OK the program should initialize the JFrame utilizing createFrame(). Once initialized the title I set and a label and text box are added with the class createContents(). Color scheme is then chosen based on what random color was chosen and once set visible is set to true. The only issue I am having is initializing my frame on the click of ok. I have tried setting the J.OptionPane.showMessageDialog = int x and use an if loop but receive void cannot be converted to int. I have also attempted to initialize createFrame() with visibility set to false but its not static so cannot initialize it from the main whether it is a class or a method. Thanks for your time and assistance.

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 


public class JMUnit7Ch17 extends JFrame
{
  private JTextField nameBox; //Holds user's name
  private static ArrayList<String> colors = new ArrayList<>();//creates 
       array to hold colors 
  private static int n;//will hold a random value

  public static void main(String[] args)
  {
   //adds colors to ArrayList Colors
   colors.add("Red"); 
   colors.add("White"); 
   colors.add("Yellow"); 
   colors.add("Green"); 
   colors.add("Blue"); 

   //creates a new random number n between 0-4
   Random rand = new Random(); 
   n  =rand.nextInt(4);

   //Outputs the message appropriatly formated
   int x=JOptionPane.showMessageDialog(null, String.format("%s%n %s%n%n %s %s",
   "The following window color will be randomly chosen from"
   ,"Red, White, Yellow, Green, Blue.","Your color will be:",colors.get(n))); 
   //Need to create a function to initialize createFrame() on click of ok from showMessageDialog
   //fuction
}

//Method for inserting the label and  text field with a listener for the users name

private void createContents()
{
 JLabel namePrompt = new JLabel("What is your name?"); 
 nameBox= new JTextField(15);
 add(namePrompt); 
 add(nameBox); 
 nameBox.addActionListener(new Listener()); 
}
//Action listener class is initialized by createContents() method

private class Listener implements ActionListener
{
 public void actionPerformed(ActionEvent e)
 {
     String message; 
     message = "Thanks for playing"+ nameBox.getText();  
    }
}
//creates a JFrame with unique title and colors based on the randomly chosen on
//createContent() is used here then JFrame is setVisible with boolean true

private void createFrame()
{
 JFrame colorFrame = new JFrame(); 
 setTitle("Color Changing Frame"); 
 createContents(); 
 switch(n)
 {
     case 0: getContentPane().setBackground(Color.RED);
             getContentPane().setForeground(Color.WHITE);    
                 break;
     case 1: getContentPane().setBackground(Color.WHITE);
             getContentPane().setForeground(Color.BLACK);
                 break;
     case 2: getContentPane().setBackground(Color.YELLOW);
             getContentPane().setForeground(Color.BLACK);
                 break;            
     case 3: getContentPane().setBackground(Color.GREEN);
             getContentPane().setForeground(Color.BLUE);
                 break;
     case 4: getContentPane().setBackground(Color.BLUE);
             getContentPane().setForeground(Color.WHITE);
                 break;            
    }   
 setVisible(true);  
}
}




Aucun commentaire:

Enregistrer un commentaire