【问题标题】:Using a loop to re-generate JFrame使用循环重新生成 JFrame
【发布时间】: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


【解决方案1】:

重构

您的代码大多以程序化风格编写。您应该将其分成小块并将它们放入方法中。


这里的最低要求是将随机生成代码包装在一个方法中,该方法以JTextBoxes 作为参数。

这个方法应该对它们调用setText,而不是创建它们。您应该在开始创建它们时不带任何文本,并在类中保留一个私有的全局引用。

即使从将 UI 实例化与任何业务逻辑分开的观点来看,这也是有道理的。您现在可以随时安全地调用此方法。


注意:
如果要重构它,考虑将所有字符串移到源代码之外可能是一个好主意。将它们放在 xml 文件或其他文件中通常被认为是一种好习惯。

它们不包含逻辑,它们不应该在源代码中。它还极大地影响了代码的可读性(因为您有很多代码)。您甚至会发现代码的可维护性得到了提升。

祝你好运。

【讨论】:

  • 谢谢你们详细的cmets,我会考虑把它分解成方法!
【解决方案2】:

这是您第一次接触面向对象的编程语言吗?虽然您可以在本地存储所有变量,但正如您所尝试的那样,创建对象并将相关信息存储在单独的对象中几乎总是一个更好的主意。此外,正如 Tanmay 在另一个答案中提到的那样,将工作分解为方法可以提高可读性和可重用性。

为了解决这个问题,我会使用三个类:

1) 创建一个类来存储适当的字符串。在这个类中有一个(或四个)方法,它为每个框返回适当的随机字符串。 (我将其称为BusinessInfo 类。)如果您确实选择保留字符串硬编码(正如 Tanmay 所提到的,这通常不是一个好主意),那么您可以将它们保留在这里。

2) 创建一个覆盖JPanel 的类。让这个类存储四个JTextBox 对象。在构造函数中,使用那些JTextBoxes 来显示信息。在JTextBox 对象中添加四种设置文本的方法。

3) 在JFrame 类中,在显示框架之前,创建BusinessInfo 对象。 (在这里使用JOptionPanes 并不是一个坏主意。)创建自定义JPanel 并调用它的四个set 方法。将该面板和另一个包含重置按钮的面板添加到JFrame。添加一个新的randomize() 方法,该方法在按下重置按钮时调用。让 randomize 方法调用自定义 JPanel 上的四个 set 方法。

这是一个基本的、即兴的想法,我将如何分解它。这只是许多可行的例子之一。

希望这个和其他答案可以让您朝着正确的方向前进。如果您认为您已经充分重构了代码,并希望其他人对其进行审核,请尝试使用code review stack exchange site.

相关阅读:Single Responsibility Principle

【讨论】:

    猜你喜欢
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多