我有这个程序在没有文件输入的情况下工作,我已经尝试过大量的谷歌搜索和询问,但无法弄明白。我不得不将firstnames
和secondnames
更改为ArrayList,任何想法如何解决这个问题?它也适用于已注释掉的文件的所有输入。
package com.gamenamegenerator;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class GameNameGenerator{
public static void main(String[] args) throws InterruptedException, IOException{
JFrame frame = new JFrame("Game Name Generator");
frame.setSize(1400, 1200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground( Color.black );
JLabel label = new JLabel();
label.setFont(new Font("SansSerif", Font.BOLD, 75));
label.setForeground(Color.BLUE);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
ArrayList<String> firstnames = new ArrayList<String>();
ArrayList<String> secondnames = new ArrayList<String>();
String[] firstnameslist = new String[] {"Healthy", "Closed", "Open", "Central", "Remorseful", "Sideways", "Diving", "Trackable", "Wary", "Determined", "Contested", "Continuous", "Hungry", "Limited", "Accredited", "Junky", "Hilarious", "Real", "Stiff", "Agile", "Solid", "Knowing", "Expectant", "Heavy", "Dense", "Unexpectant", "Insolent", "Accurate", "Sufferable", "Painful", "Wise", "Knowledgeable", "Growing", "Confused", "Careful", "Untrusting", "Untrustworthy", "Trusting", "Trustworthy", "Laughing", "Sick", "Hasty", "Reluctant", "Barbaric", "Desperate", "Returning", "Mechanical", "Pondering", "Thoughtful", "Dark", "Positive", "Hated", "Spiteful", "Hateful", "Shimmering", "Assumptive", "Brainy", "Battling", "Commercial", "Bankrupt", "Back Firing", "Babysitting", "Pampered", "Pocket", "Adaptive", "Wet", "Automatic", "Shaky", "Sorrowful", "Experienced", "Inexperienced", "Smooth", "Youthful", "Technological", "Advanced", "Minor", "Hungry", "Misguided", "Feverish", "Drunk", "Dependent", "Burnt", "Humiliated", "Bossy", "Peaceful", "Official", "Junior", "Logical", "Absent", "Curious", "Sharp", "Black", "Broad", "Flabby", "Runny", "Cold Hearted", "Cold", "Fiery", "Speeding", "Flying", "Awoken", "Camoflaged", "Iron", "Golden", "Watchful", "Guarding", "Avenging", "Displaced", "Broken", "Mismatched", "Glistening", "Turbulent", "Difficult", "Approving", "Floating", "Aquatic", "Energetic", "Vibrant", "Animated", "Metallic", "Streamlined", "Cracked", "Aerobatic", "Flimsy", "Absurd", "Dozing", "Dopy", "Pointy", "Paranormal", "Angry", "Slurping", "Gasping", "Oblivious", "Babbling", "Steroid Taking", "Steroids", "Mutated", "Chubby", "Uncooperative", "Smelly", "Inhuman", "Accidental", "Freak", "Scientific", "Combative", "Inaccessible", "Abusive", "Extraterrestrial", "Extravagant", "Undeniably", "Combat", "Enormous", "Giant", "Overrated", "Undeniable", "Secretive", "Comprehensive", "Legendary", "Treacherous", "Deceptive", "Lying", "Hairy", "Loony", "Skinny", "Scrawny", "Genetically Modified", "Modified", "Super", "Arrogant", "Mysterious", "Sneaky", "Deadly", "Fatal", "Dangerous", "Breezy", "Superior", "Flabbergasted", "Mouldy", "Molten", "Squishy", "Moist", "Mushy", "Fluffy", "Magical", "Slimy", "Sloppy", "Elderly", "Stout", "Fat", "Genetically Mutated", "Mutated", "Space", "Cheeky", "Violent", "Amputated", "Snowy", "Poisonous", "Stale", "Alpine", "Rural", "Urban", "Sparkling", "Epicer", "Clumsy", "Interactive", "Edible", "Sublime", "Woollen", "Toxic", "Flammable", "Lazy", "Glowing", "Explosive", "Exploring", "Colossal", "United", "Water", "Clever", "Irresistible", "Grumpy", "Ballistic", "Rebellious", "Majestic"};
String[] secondnameslist = new String[] {"Life", "Warmonger", "Computer", "Buttocks", "Drink", "Aid", "Shot", "Mate", "Standards", "Alien", "Cloud", "Trees", "Nursing", "Time Bomb", "Bomb", "Time", "Times", "Happiness", "Gunfire", "Heart", "Darkness", "Positivity", "Hater", "Oil", "Spy", "Outsider", "Insider", "Biscuit", "Business", "Advertisement", "Instrument", "Balaclava", "Backpacker", "Back Fire", "Believer", "Handler", "Weakling", "Man", "Peacock", "Buddy", "Revenge", "Eye", "Bear", "Bin", "Nose", "Rocket", "Golem", "Guard", "Sentry", "Canine", "Bone", "Error", "Robot", "Rock", "Stick", "Invention", "Bear", "Koala", "Dinosaur", "Bunny", "Bubble", "Chocolate", "Creation", "Accident", "Mutation", "Mongoose", "Vibrations", "Liar", "Merchant", "Orb", "Turnip", "Wombat", "Creature", "Strength", "Contempt", "Disk", "Vegetable", "Vegan", "Squirrel ", "Mud", "Cheese", "Tape", "Lava", "Magma", "Rainbow", "Devil", "Dwarf", "Remix", "Sapling", "Owl", "Poison", "Butterfly", "Flatulence", "Flop", "Fail", "Fart", "Cake", "Monkey", "Penguin", "Dog", "Cat", "Block", "Steam", "Goose", "Waste", "Nightmare", "Potato", "Nerd", "Cookie", "Whale", "Falcon", "Chipmunk", "Carrot", "Donkey", "Toad", "Cauliflower", "Fire", "Gust", "Bumfluff", "Blob", "Puppy", "Cube", "Butt Cheeks", "Bumfluffles"};
for(int i = 0; i < firstnameslist.length; i++){
firstnames.add(firstnameslist[i]);
}
for(int i = 0; i < secondnameslist.length; i++){
firstnames.add(secondnameslist[i]);
}
Scanner firstnameinput = null;
try{
firstnameinput = new Scanner(new File("firstnameinput.txt"));
firstnameinput.useDelimiter(", ");
}
catch(Exception e){
System.out.println("File firstnameinput.txt not found! :(");
}
while(firstnameinput.hasNext()){
String a = firstnameinput.next();
System.out.print(a + " ");
firstnames.add(a);
}
firstnameinput.close();
Scanner secondnameinput = null;
try{
secondnameinput = new Scanner(new File("secondnameinput.txt"));
secondnameinput.useDelimiter(", ");
}
catch(Exception e){
System.out.println("File secondnameinput.txt not found! :(");
}
while(secondnameinput.hasNext()){
String a = secondnameinput.next();
System.out.print(a + " ");
secondnames.add(a);
}
secondnameinput.close();
while(true){
Random rnd = new Random();
int n1 = rnd.nextInt(firstnames.size() - 1);
int n2 = rnd.nextInt(secondnames.size() - 1);
int combinations = firstnames.size() * secondnames.size();
String result = firstnames.get(n1) + " " + secondnames.get(n2);
System.out.println(result);
label.setText(result);
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
try {
File file = new File("generatednames.txt");
FileWriter x = new FileWriter(file, true);
x.write(result + ", ");
x.close();
} catch (FileNotFoundException e) {
System.out.println("Could not open or create text file!");
}
try{
PrintWriter writer = new PrintWriter("gamenamegeneratorstats.txt", "UTF-8");
writer.println("STATS FOR THIS VERSION");
writer.println("Possible Combinations: " + combinations);
writer.println("Please enjoy your generated names!");
writer.close();
} catch (Exception e) {
System.out.println("Could not open or create text file!");
}
Thread.sleep(2500);
}
}
}
答案 0 :(得分:0)
您的代码仍有一些问题。 您需要学习的主要问题是EDT(必须从事件派发线程调用所有其他Swing组件方法)
您需要更新代码:
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
在SwingUtilities中调用
SwingUtilities.invokeLater(() -> {
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
});
并改变:
label.setText(result);
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
到
SwingUtilities.invokeLater(() -> {
label.setText(result);
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
frame.revalidate();
frame.repaint();
});