简体   繁体   English

如何让我的动作侦听器与我的计算方法进行通信以将文本输出到我的 GUI?

[英]How do I get my action listener to communicate with my calculation methods in order to output text to my GUI?

I have been tasked with creating a GUI application that will take a 3 digit input and return the sum, reverse, and array of that number.我的任务是创建一个 GUI 应用程序,它将接受 3 位数字输入并返回该数字的总和、反向和数组。 I have created all of my variables and GUI components and laid them out, but I am now stuck when it comes to doing the calculation and returning the answers to the GUI.我已经创建了所有变量和 GUI 组件并对其进行了布置,但是现在我在进行计算并将答案返回给 GUI 时遇到了困难。

package lab02;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Lab02 extends JFrame implements ActionListener
{

   Container content = this.getContentPane();
   SpringLayout layout = new SpringLayout();
   JLabel lblNum = new JLabel("Enter a 3-digit number:");
   JLabel lblSum = new JLabel("Sum:");
   JLabel lblReverse = new JLabel("Reverse:");
   JLabel lblArray = new JLabel("Array:");
   JLabel lblTheSum = new JLabel("");
   JLabel lblTheReverse = new JLabel("");
   JLabel lblTheArray = new JLabel("");
   JLabel lblTextColor = new JLabel("Text color:");
   JButton btnComp = new JButton("Compute");
   JTextField tfInput = new JTextField(5);

   public Lab02()
   {
      content.setLayout(layout);
      this.CreateWindow();
      content.add(lblNum);
      layout.putConstraint(SpringLayout.NORTH, lblNum, 25, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblNum, 25, SpringLayout.WEST, content);

      content.add(lblSum);
      layout.putConstraint(SpringLayout.NORTH, lblSum, 60, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblSum, 25, SpringLayout.WEST, content);

      content.add(lblReverse);
      layout.putConstraint(SpringLayout.NORTH, lblReverse, 95, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblReverse, 25, SpringLayout.WEST, content);

      content.add(lblArray);
      layout.putConstraint(SpringLayout.NORTH, lblArray, 130, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblArray, 25, SpringLayout.WEST, content);

      content.add(lblTextColor);
      layout.putConstraint(SpringLayout.NORTH, lblTextColor, 165, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblTextColor, 25, SpringLayout.WEST, content);

      content.add(lblTheSum);
      layout.putConstraint(SpringLayout.NORTH, lblTheSum, 60, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblTheSum, 175, SpringLayout.WEST, content);

      content.add(lblTheReverse);
      layout.putConstraint(SpringLayout.NORTH, lblTheReverse, 95, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblTheReverse, 175, SpringLayout.WEST, content);

      content.add(lblTheArray);
      layout.putConstraint(SpringLayout.NORTH, lblTheArray, 130, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, lblTheArray, 175, SpringLayout.WEST, content);

      content.add(btnComp);
      layout.putConstraint(SpringLayout.NORTH, btnComp, 200, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, btnComp, 150, SpringLayout.WEST, content);

      content.add(tfInput);
      layout.putConstraint(SpringLayout.NORTH, tfInput, 25, SpringLayout.NORTH, content);
      layout.putConstraint(SpringLayout.WEST, tfInput, 175, SpringLayout.WEST, content);

      btnComp.addActionListener(this);
   }

   public void CreateWindow()
   {
      this.setVisible(true);
      this.setSize(400, 300);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Lab02");
   }

   @Override
   public void actionPerformed(ActionEvent e)
   {
      int num = Integer.parseInt(tfInput.getText());
      this.sumTheDigits(num);
      this.reverseTheOrder(num);
      this.createArrayFrom(num);

   }

   public String sumTheDigits(int num)
   {

      int one;
      int two;
      int three;
      int sum;

      one = num % 10;
      two = (num/10) % 10;
      three = num / 100;
      sum = one + two + three;

      String strSum = String.valueOf(sum);

      return strSum;
   }

    public String reverseTheOrder(int num)
   {
      int first;
      int second;
      int third;

      first = num / 100;
      second = (num/10) % 10;
      third = num % 10;

      StringBuffer s = new StringBuffer(3);

      s.append(third);
      s.append(second);
      s.append(first);

      String reverse = s.toString();

      return reverse;
   }

     public int[] createArrayFrom(int num)
   {

      int first;
      int second;
      int third;
      int[] array = new int[3];

      first = num / 100;
      second = (num/10) % 10;
      third = num % 10;

      array[0] = first;
      array[1] = second;
      array[2] = third;

      return array;
   }

   public static void main(String[] args)
   {
      Lab02 gui = new Lab02();
   }



}

You get your action listener to communicate with your GUI by using a model / view / controller pattern.通过使用模型/视图/控制器模式,您可以让您的动作侦听器与您的 GUI 进行通信。 Lots of people have said as much.很多人都说了这么多。 I'm going to show you in practice how you separate your code into a model, a view, and a controller.我将在实践中向您展示如何将代码分成模型、视图和控制器。

I tried not to modify your code too much.我尽量不要过多地修改你的代码。 Frankly, a spring layout is useless, in my opinion.坦率地说,在我看来,弹簧布局是无用的。

Anyway, here's the GUI.无论如何,这是GUI。

在此处输入图片说明

Here are the changes I made, in no particular order.以下是我所做的更改,没有特别的顺序。

  1. I created a Lab02Model class and moved all your calculations into this class.我创建了一个 Lab02Model 类并将您的所有计算移到这个类中。

  2. I created a Lab02Listener class and moved the action listener code into that class.我创建了一个 Lab02Listener 类并将动作侦听器代码移动到该类中。

  3. I started your GUI by calling the SwingUtilities invokeLater method.我通过调用 SwingUtilities invokeLater 方法启动了您的 GUI。 This ensures that your GUI is created and executed on the Event Dispatch Thread.这确保您的 GUI 是在事件调度线程上创建和执行的。

  4. I used a JFrame.我使用了 JFrame。 There's no need to extend a Swing component unless you intend to override one of the component methods.除非您打算覆盖其中一种组件方法,否则无需扩展 Swing 组件。

  5. I moved most of your field definitions into the constructor of Lab02.我将您的大部分字段定义移到 Lab02 的构造函数中。 You only make fields class fields if they need to be referenced in more than one place in the class.仅当需要在类中的多个位置引用字段时,才将字段设为类字段。

  6. I added lots of getter methods so one class could get values from another class.我添加了许多 getter 方法,以便一个类可以从另一个类中获取值。 Pay attention to these methods.注意这些方法。 This is how you communicate between classes.这就是您在类之间进行通信的方式。

Anyway, here's my code.无论如何,这是我的代码。

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;

public class Lab02 {

    private JLabel lblTheSum = new JLabel("");
    private JLabel lblTheReverse = new JLabel("");
    private JLabel lblTheArray = new JLabel("");

    private JTextField tfInput = new JTextField(5);

    public Lab02() {
        JFrame frame = new JFrame("Lab02");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = frame.getContentPane();

        SpringLayout layout = new SpringLayout();
        JLabel lblNum = new JLabel("Enter a 3-digit number:");
        JLabel lblSum = new JLabel("Sum:");
        JLabel lblReverse = new JLabel("Reverse:");
        JLabel lblArray = new JLabel("Array:");
        JLabel lblTextColor = new JLabel("Text color:");
        JButton btnComp = new JButton("Compute");

        content.setLayout(layout);
        content.add(lblNum);
        layout.putConstraint(SpringLayout.NORTH, lblNum, 25, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblNum, 25, SpringLayout.WEST, content);

        content.add(lblSum);
        layout.putConstraint(SpringLayout.NORTH, lblSum, 60, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblSum, 25, SpringLayout.WEST, content);

        content.add(lblReverse);
        layout.putConstraint(SpringLayout.NORTH, lblReverse, 95, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblReverse, 25, SpringLayout.WEST, content);

        content.add(lblArray);
        layout.putConstraint(SpringLayout.NORTH, lblArray, 130, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblArray, 25, SpringLayout.WEST, content);

        content.add(lblTextColor);
        layout.putConstraint(SpringLayout.NORTH, lblTextColor, 165, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblTextColor, 25, SpringLayout.WEST, content);

        content.add(lblTheSum);
        layout.putConstraint(SpringLayout.NORTH, lblTheSum, 60, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblTheSum, 175, SpringLayout.WEST, content);

        content.add(lblTheReverse);
        layout.putConstraint(SpringLayout.NORTH, lblTheReverse, 95, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblTheReverse, 175, SpringLayout.WEST, content);

        content.add(lblTheArray);
        layout.putConstraint(SpringLayout.NORTH, lblTheArray, 130, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, lblTheArray, 175, SpringLayout.WEST, content);

        content.add(btnComp);
        layout.putConstraint(SpringLayout.NORTH, btnComp, 200, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, btnComp, 150, SpringLayout.WEST, content);

        content.add(tfInput);
        layout.putConstraint(SpringLayout.NORTH, tfInput, 25, SpringLayout.NORTH, content);
        layout.putConstraint(SpringLayout.WEST, tfInput, 175, SpringLayout.WEST, content);

        Lab02Model lab02Model = new Lab02Model();
        btnComp.addActionListener(new Lab02Listener(this, lab02Model));

        frame.setSize(400, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public void updateGUI(String sumText, String reverseText, int[] array) {
        lblTheSum.setText(sumText);
        lblTheReverse.setText(reverseText);
        lblTheArray.setText(Arrays.toString(array));
    }

    public JTextField getTfInput() {
        return tfInput;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Lab02();
            }
        });
    }

    public class Lab02Listener implements ActionListener {

        private Lab02 lab02;
        private Lab02Model lab02Model;

        public Lab02Listener(Lab02 lab02, Lab02Model lab02Model) {
            this.lab02 = lab02;
            this.lab02Model = lab02Model;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int num = Integer.parseInt(lab02.getTfInput().getText());
            lab02Model.calculateValues(num);
            lab02.updateGUI(lab02Model.getDigitSum(), lab02Model.getReverseOrder(), lab02Model.getNumArray());
        }

    }

    public class Lab02Model {

        private int[] numArray;

        private String reverseOrder;
        private String digitSum;

        public void calculateValues(int num) {
            this.digitSum = sumTheDigits(num);
            this.reverseOrder = reverseTheOrder(num);
            this.numArray = createArrayFrom(num);
        }

        public int[] getNumArray() {
            return numArray;
        }

        public String getReverseOrder() {
            return reverseOrder;
        }

        public String getDigitSum() {
            return digitSum;
        }

        public String sumTheDigits(int num) {

            int one;
            int two;
            int three;
            int sum;

            one = num % 10;
            two = (num / 10) % 10;
            three = num / 100;
            sum = one + two + three;

            String strSum = String.valueOf(sum);

            return strSum;
        }

        public String reverseTheOrder(int num) {
            int first;
            int second;
            int third;

            first = num / 100;
            second = (num / 10) % 10;
            third = num % 10;

            StringBuffer s = new StringBuffer(3);

            s.append(third);
            s.append(second);
            s.append(first);

            String reverse = s.toString();

            return reverse;
        }

        public int[] createArrayFrom(int num) {

            int first;
            int second;
            int third;
            int[] array = new int[3];

            first = num / 100;
            second = (num / 10) % 10;
            third = num % 10;

            array[0] = first;
            array[1] = second;
            array[2] = third;

            return array;
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM