2017-09-30 1 views
-1

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

Antwort

0

einige Dinge geändert:

  • Klasse machte die ActionListener Schnittstelle, so dass keine Notwendigkeit zu instanziiert eine andere Klasse für sie umzusetzen.
  • Hinzugefügt FlowLayout als Layout-Manager, wie der Standard ist BorderLayout, und da Sie keine Position angegeben, füllten alle Komponenten den mittleren Teil (das ist das ganze Fenster). auch einen Anruf an pack(), um die Komponenten zu legen, und die Größe des Rahmens entsprechend einzustellen.
  • Umbenennung Ihrer createContent()-Methode in den de facto-Standardnamen initComponents().
  • ein JButton hinzugefügt, als JTextField nicht ActionEvent s feuern würde ...
  • die Einstellung von Farben auf ein separates Verfahren (setBgColor()) extrahierte, wie dies später genannt werden muß. Dies setzt auch opaque auf true den Hintergrund sichtbar ...
  • Added SwingUtilities.invokeLater() und ein Lambda zu machen, Ihren Schaukel Code in AWTs Event Dispatch Thread läuft ...

So die Ergebnisse sind etwas wie dieses:

public class JMUnit7Ch17 extends JFrame implements ActionListener 
{ 
    private static final long serialVersionUID = 0L; 
    private static String[]  colors = { "Red", "White", "Yellow", "Green", "Blue" }; 
    private static final String msg = "The following window color will be randomly" + 
             " chosen from\nRed, White, Yellow, Green, Blue." + 
             "\n\nYour color will be: "; 
    private int n; 
    private final JTextField nameBox = new JTextField(15); 
    private final JButton tyBtn = new JButton("Thanks"); 

    public static void main(final String[] args) 
    { 
     final Random rand = new Random(); 
     SwingUtilities.invokeLater(() -> 
     { 
      final JMUnit7Ch17 form = new JMUnit7Ch17(); 
      form.n = rand.nextInt(4); 
      JOptionPane.showMessageDialog(null, JMUnit7Ch17.msg + JMUnit7Ch17.colors[form.n]); 
      form.setBgColor(); 
      form.setVisible(true); 
     }); 
    } 

    private JMUnit7Ch17() 
    { 
     super("Color Changing Frame"); 
     initComponents(); 
    } 

    private void initComponents() 
    { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new FlowLayout()); 
     add(new JLabel("What is your name?")); 
     add(this.nameBox); 
     add(this.tyBtn); 
     this.tyBtn.addActionListener(this); 
     pack(); 
    } 

    private void setBgColor() 
    { 
     // JFrame has a JPanel as the default content pane... 
     final JPanel cp = (JPanel) getContentPane(); 
     cp.setOpaque(true); 
     switch (this.n) 
     { 
      case 0: 
       cp.setBackground(Color.RED); 
       cp.setForeground(Color.WHITE); 
       break; 
      case 1: 
       cp.setBackground(Color.WHITE); 
       cp.setForeground(Color.BLACK); 
       break; 
      case 2: 
       cp.setBackground(Color.YELLOW); 
       cp.setForeground(Color.BLACK); 
       break; 
      case 3: 
       cp.setBackground(Color.GREEN); 
       cp.setForeground(Color.BLUE); 
       break; 
      case 4: 
       cp.setBackground(Color.BLUE); 
       cp.setForeground(Color.WHITE); 
     } 
    } 

    @Override 
    public void actionPerformed(final ActionEvent e) 
    { 
     JOptionPane.showMessageDialog(this, "Thanks for playing, " + this.nameBox.getText()); 
    } 
} 
Verwandte Themen