Dies könnte eine sehr offensichtliche Frage für einige sein, aber ich kann es nicht herausfinden. Ich bin neu bei Eclipse und habe nur mit Dr.Java programmiert. Ich erstelle ein mad libs Programm, wo der Benutzer Substantive, Adjektive, Namen und Zahlen eingeben muss und am Ende wird es in einer Geschichte angezeigt.Wie drucke ich etwas Text auf dem Java jPanel?
Nachdem der Benutzer alle erforderlichen Informationen eingegeben hat, möchte ich die fertige Geschichte in einem jPanel öffnen. Ich kann nicht herausfinden, wie der Text der JPanel hinzuzufügen (I der Text mit der c beginnen möchten Code in einem Fenster angezeigt werden, nachdem der Benutzer alle Informationen eingegeben hat.) Hier ist der Code:.
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MadLibs{
public static void Action1()
{
JFrame frame = new JFrame("Mad Libs");
// Add a window listener for close button
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Scanner input = new Scanner(System.in);
System.out.println("Male Friend:");
String maleFriend = input.nextLine();
System.out.println("Adjective:");
String adjective1 = input.nextLine();
System.out.println("Past Tense Verb:");
String pastTenseVerb1 = input.nextLine();
System.out.println("Past Tense Verb 2:");
String pastTenseVerb2 = input.nextLine();
System.out.println("Large Number:");
String largeNumber = input.nextLine();
System.out.println("Famous Female:");
String famousFemale = input.nextLine();
System.out.println("Adverb:");
String adverb = input.nextLine();
System.out.println("Place:");
String place = input.nextLine();
System.out.println("Body Part(singular):");
String bodyPart = input.nextLine();
System.out.println("Large Number:");
String largeNumber2 = input.nextLine();
System.out.println("Verb ending with -ing");
String ingEnding1 = input.nextLine();
System.out.println("Singular noun:");
String singularNoun = input.nextLine();
System.out.println("Plural Noun:");
String pluralNoun = input.nextLine();
// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(600, 800));
frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
//The story I want displayed on the jPanel:
/*
c.println("The Great Dough Disaster");
c.println("\nLast summer, my friend "+ maleFriend + " got a job at the " + adjective1 +" Pastry Shop. For the first few");
c.println("weeks, he " + pastTenseVerb1 + " the floors, " + pastTenseVerb2 + " on the shelves, and unloaded " + largeNumber + " pound sacks");
c.println("of flour from the delivery trucks.\n");
c.println("Finally, "+famousFemale+", the owner, told "+maleFriend+" that she would teach him to make bread. Now,");
c.println("pay attention, "+maleFriend+",” she said every day. “I'll make the first batch of dough. Then you");
c.println("can make the next batch while I go to "+place+".\n");
c.println("Poor "+maleFriend+"! He had a habit of letting his "+bodyPart+" wander. When " +famousFemale+ " left for "+place);
c.println("he started to mix the ingredients. “Let me see,” he said. “I think she put in "+largeNumber2);
c.println("packages of yeast.”\n");
c.println("A short while later, the dough started "+ingEnding1+". It kept on "+ingEnding1+". "+maleFriend+" tried to");
c.println("cover it with a(n) "+singularNoun+", but the dough wouldn't stop "+ingEnding1+". It was everywhere! ");
c.println("“What can I do?” thought "+maleFriend+".\n");
c.println("Just then, Tyana returned from toronto. “"+maleFriend+"” she screamed. “What have you done?”");
c.println("“It's not my fault,” cried "+maleFriend+". “The dough just started "+ingEnding1+" and wouldn't stop.”");
c.println(famousFemale + " had to let him go. Now "+maleFriend+" has a job making "+singularNoun+". I don't think he'll ever");
c.print("eat bread again, let alone make it.");
*/
}
public static void main(String []args){
Action1();
}
}
Außerdem bin ich etwas verwirrt darüber, wie das jPanel funktioniert. Ich habe viele Möglichkeiten gefunden, Dinge auf dem jPanel anzuzeigen, und ich weiß nicht, welchen ich verwenden soll.
Verwenden Sie JLabels für Text auf JPanels. – Compass
Es gibt keine 'jpanel's hier, nur Etiketten .... – Antoniossss