【发布时间】:2014-04-21 21:09:18
【问题描述】:
所以我有这个程序可以随机生成贵公司的描述。
这主要用于建立链接网站等,所以你没有重复的数据。我在JFrame 中添加了一个按钮,这样当按下时它会重新打乱句子而不是退出程序并再次填写所有信息。
我一直在尝试使用组,但似乎无法弄清楚。有什么想法吗?
我不希望程序再次询问信息,所以我需要循环从随机变量开始的地方开始。
if (actionCommand.equals("Product")) {
JButton Pbutton;
Pbutton = new JButton("Generate");
final String name = JOptionPane.showInputDialog(null, "Enter your Business name: \n \n ex: We at 'your business' deliver...");
final String productP = JOptionPane.showInputDialog(null, "Enter your Business product(plural): \n \n ex: take pride in our 'pizza slicers'");
final String productS = JOptionPane.showInputDialog(null, "Enter your Business product(singular): \n \n ex: we make the highest quality 'pizza slicer' in...");
final String location = JOptionPane.showInputDialog(null, "Where is your business located? \n \n ex: Cityville, Virginia.");
final String keywords = JOptionPane.showInputDialog(null, "Enter 6 keywords for your product: \n \n Ex: pizza, pizza slicer, cityville, Virginia, tools, restaurant equipment");
final String URL = JOptionPane.showInputDialog(null, "Enter your website URL: \n \n ex: www.yourwebsite.com");
Random rand1 = new Random();
int number1 = rand1.nextInt(3);
if (number1 == 2)
v1 = "We at ";
else if (number1 == 1)
v1 = "The staff at ";
else if (number1 == 0)
v1 = "The professionals at ";
Random rand2 = new Random();
int number2 = rand2.nextInt(5);
if (number2 == 4)
v2 = " take great pride in our ";
else if (number2 == 3)
v2 = " know the importance of having great ";
else if (number2 == 2)
v2 = " bring you only the best ";
else if (number2 == 1)
v2 = " offer only the greatest ";
else if (number2 == 0)
v2 = " are bringing you top-of-the-line ";
Random rand3 = new Random();
int number3 = rand3.nextInt(5);
if (number3 == 4)
v3 = " We offer the highest quality " + productS + " ";
else if (number3 == 3)
v3 = " Let us be the ones to supply you with " + productP + " ";
else if (number3 == 2)
v3 = " No one can compete with our high quality " + productP + " ";
else if (number3 == 1)
v3 = " Only we can offer these a " + productS + " of this quality ";
else if (number3 == 0)
v3 = " Turn to us if you need a " + productS + " ";
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame frame = new JFrame(name);
frame.getContentPane().setBackground(JMUpurple);
frame.setLayout(new GridLayout(2, 3));
String text = v1 + name + v2 + productP + "." + v3 + "in " + location + ".";
String text2 = name + " of " + location + "| High quality " + productP;
String text3 = keywords;
String text4 = URL;
Font font = new Font("Georgia", 0, 12);
JTextArea textArea1 = new JTextArea(text, 5, 25);
JTextArea textArea2 = new JTextArea(text2, 5, 25);
JTextArea textArea3 = new JTextArea(text3, 10, 25);
JTextArea textArea4 = new JTextArea(text4, 2, 25);
textArea2.setPreferredSize(new Dimension(100, 100));
textArea3.setPreferredSize(new Dimension(50, 50));
JScrollPane scrollPane = new JScrollPane(textArea1, 22,
32);
JScrollPane scrollPane2 = new JScrollPane(textArea2, 22,
32);
JScrollPane scrollPane3 = new JScrollPane(textArea3, 22,
32);
JScrollPane scrollPane4 = new JScrollPane(textArea4, 22,
32);
textArea1.setWrapStyleWord(true);
textArea1.setLineWrap(true);
textArea2.setWrapStyleWord(true);
textArea2.setLineWrap(true);
textArea3.setWrapStyleWord(true);
textArea3.setLineWrap(true);
textArea4.setWrapStyleWord(true);
textArea4.setLineWrap(true);
frame.add(scrollPane);
frame.add(scrollPane2);
frame.add(scrollPane3);
frame.add(scrollPane4);
frame.add(Pbutton);
frame.pack();
frame.setVisible(true);
textArea1.setFont(font);
textArea1.setBackground(JMUgold);
textArea2.setFont(font);
textArea2.setBackground(JMUgold);
textArea3.setFont(font);
textArea3.setBackground(JMUgold);
textArea4.setFont(font);
textArea4.setBackground(JMUgold);
}
【问题讨论】:
-
有点大的代码块,还有什么提示吗?
-
是的,我没有大量的 Java 编写经验,是自学的,所以我做的一些事情可能效率很低。抱歉,代码量很大。我只需要以某种方式重新绘制 Jframe,同时保留开头的字符串数据,但重新生成随机变量。
-
当变量名除数字外相似时,应考虑使用数组。
标签: java swing loops jframe jbutton