Ich versuche, einen showMessageDialog auszugeben, der besagt, dass eine Farbe zufällig ausgewählt wird und die gewählte Farbe angibt. Sobald der Benutzer auf OK klickt, sollte das Programm den JFrame unter Verwendung von createFrame() initialisieren. Sobald der von mir festgelegte Titel initialisiert wurde, werden ein Label und ein Textfeld mit der Klasse createContents() hinzugefügt. Das Farbschema wird dann basierend darauf ausgewählt, welche zufällige Farbe ausgewählt wurde, und sobald es sichtbar ist, wird es auf "Wahr" gesetzt. Das einzige Problem, das ich habe, initialisiert meinen Rahmen auf den Klick von ok. Ich habe versucht, die J.OptionPane.showMessageDialog = int x und verwenden Sie eine if-Schleife, aber erhalten void kann nicht in int konvertiert werden. Ich habe auch versucht, createFrame() mit Sichtbarkeit auf false zu initialisieren, aber es ist nicht statisch, so kann es nicht von der Haupt initialisieren, ob es eine Klasse oder eine Methode ist. Danke für Ihre Zeit und Unterstützung.Wie fügt man Listener hinzu oder initialisiert eine Klasse mit dem Klick auf OK in showMessageDialog in Java?
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);
}
}