在两个类之间通信增量器

时间:2018-12-07 20:35:47

标签: java

在我的程序中,当前有两个转换计数器,它们分成两个看起来像这样的不同类。

转换计数器位于两个不同的面板中(通过中间拆分),它们位于两个不同的类中,它们在第三个主类中声明和调用。

enter image description here

在进行了一些转换之后,它们各自逐一计数,直到被清除为止。

enter image description here

我需要做的是,但我无法弄清楚如何使一个转换计数器在两个类之间都可以工作并且对两个转换器都可以工作。它需要计算两个转换器的转换次数,并且只需要一个按钮即可清除。

有人可以指导我该怎么做吗?这是我附带的代码,用于同时包含两个面板和一个转换器的主类(这两个转换器是完全重复的,除了列表中的名称不同和转换因子的值不同,因此您可以使用第一个的两个副本运行该程序

主类

package autassignment;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Converter {

    public static void main(String[] args) {


        JFrame frame = new JFrame("Converter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      

        // Create a new master panel
        JPanel masterPanel = new JPanel();

        // Use a box layout to stack the panels
        masterPanel.setLayout(new BoxLayout(masterPanel, BoxLayout.PAGE_AXIS));

        MainPanel panel = new MainPanel(); 
        frame.setJMenuBar(panel.setupMenu());

        // Create the new currency panel
        CurrencyPanel currencyPanel = new CurrencyPanel();

        // Add the sub-panels to the master panel 
        masterPanel.add(panel);
        masterPanel.add(currencyPanel);

        // Add the master panel to the frame
        frame.getContentPane().add(masterPanel);

        frame.pack();
        frame.setVisible(true);

        panel.currencyPanel = currencyPanel;
        currencyPanel.mainPanel = panel;
    }


}

CurrencyConverter类

package autassignment;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class CurrencyPanel extends JPanel {



    //declaring everything before using it
    private final static String[] list = { "Euro (EUR)", "US Dollars (USD)", "Australian Dollars (AUD)", "Canadian Dollars (CAD)", "Icelandic Króna (ISK)", "United Arab Emirates Dirham (AED)", "South African Rand (ZAR)", "The Baht (THB)"};
    private JTextField textField;
    private JLabel label;
    private JLabel countLabel;
    private JComboBox<String> combo;
    int convCount=0;
    boolean reverse;
    public MainPanel mainPanel;
    JCheckBox reverseCheck;


    //start of my menu
    JMenuBar setupMenu() {

        //main bar to hold everything
        JMenuBar menuBar = new JMenuBar();

        //menu items
        //menomnic key events added to main menuitems
        JMenu filebar = new JMenu("File");
        filebar.setMnemonic(KeyEvent.VK_F);
        JMenu helpbar = new JMenu("Help");
        helpbar.setMnemonic(KeyEvent.VK_H);

        //menu items added to the JMENUBAR
        menuBar.add(filebar);
        menuBar.add(helpbar);

        //sub menu items added to menu items
        //actionlisteners added to submenuitems
        //menmonic key events added to subemenuitems
        JMenuItem exitbar = new JMenuItem("Exit");
        exitbar.addActionListener(new CloseListener());
        exitbar.setMnemonic(KeyEvent.VK_E);
        filebar.add(exitbar);

        JMenuItem about = new JMenuItem("About");
        about.addActionListener(new AboutListener());
        about.setMnemonic(KeyEvent.VK_A);
        helpbar.add(about);

        //command which displays the menu bar
        return menuBar;
    }


    CurrencyPanel() {

        ActionListener listener = new ConvertListener();
        ActionListener listener2 = new ClearListener();
        ItemListener listener3 = new ReverseListener();

        combo = new JComboBox<String>(list);
        combo.addActionListener(listener); //convert values when option changed

        JLabel inputLabel = new JLabel("Enter value:");

        JButton convertButton = new JButton("Convert");
        convertButton.addActionListener(listener); // convert values when pressed
        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(listener2);


        label = new JLabel("---");
        countLabel = new JLabel();
        textField = new JTextField(5);
        reverseCheck = new JCheckBox("Reverse Conversion");
        reverseCheck.addItemListener(listener3);
        textField.addKeyListener(new KeyAdapter() {

            public void keyPressed(KeyEvent e) {

                int key = e.getKeyCode();
                if (key == KeyEvent.VK_ENTER) {
                    String text = textField.getText().trim();

                    if (text.isEmpty() == false) {
                        convCount++;

                        double value = Double.parseDouble(text);

                        // the factor applied during the conversion
                        double factor = 0;

                        // the offset applied during the conversion.
                        double offset = 0;

                        // Setup the correct factor/offset values depending on
                        // required
                        // conversion
                        switch (combo.getSelectedIndex()) {

                        case 0: // EUR
                            if(reverse==false) {
                                factor = 1.359;
                                break;
                            } if(reverse==true) {
                                factor = 0.73;
                                break;
                            }

                        case 1: // USD
                            if(reverse==false) {
                                factor = 1.34;
                                break;
                            } if(reverse==true) {
                                factor = 1.82;
                                break;
                            }

                        case 2: // AUD
                            if(reverse==false) {
                                factor = 1.756;
                                break;
                            } if(reverse==true) {
                                factor = 0.62;
                                break;
                            }

                        case 3: // CAD
                            if(reverse==false) {
                                factor = 1.71;
                                break;
                            } if(reverse==true) {
                                factor = 0.21;
                                break;
                            }

                        case 4: // ISK
                            if(reverse==false) {
                                factor = 140.84;
                                break;
                            } if(reverse==true) {
                                factor = 3.28;
                                break;
                            }

                        case 5: // AED
                            if(reverse==false) {
                                factor = 4.92;
                                break;
                            } if(reverse==true) {
                                factor = -272.15;
                                break;
                            }

                        case 6: // ZAR
                            if(reverse==false) {
                                factor = 17.84;
                                break;
                            } if(reverse==true) {
                                factor = 2.47;
                                break;
                            }

                        case 7: // THB
                            if(reverse==false) {
                                factor = 43.58;
                                break;
                            } if(reverse==true) {
                                factor = 2.47;
                                break;
                            }
                        }

                        double result = factor * value + offset;
                        double roundedresult = Math.round(result *100)/100.00;

                        label.setText(Double.toString(roundedresult));
                        countLabel.setText("conversions: " + convCount);
                    }
                }
            }
        });


        add(combo);
        add(reverseCheck);
        add(inputLabel);
        add(textField);
        add(convertButton);
        add(clearButton);
        add(label);
        add(countLabel);

        setPreferredSize(new Dimension(800, 80));
        setBackground(Color.LIGHT_GRAY);
    }


    private class ConvertListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {

            String text = textField.getText().trim();

            if (text.isEmpty() == false) {

                convCount++;

                double value = Double.parseDouble(text);

                // the factor applied during the conversion
                double factor = 0;

                // the offset applied during the conversion.
                double offset = 0;

                // Setup the correct factor/offset values depending on
                // required
                // conversion
                switch (combo.getSelectedIndex()) {

                case 0: // EUR
                    if(reverse==false) {
                        factor = 1.359;
                        break;
                    } if(reverse==true) {
                        factor = 0.73;
                        break;
                    }

                case 1: // USD
                    if(reverse==false) {
                        factor = 1.34;
                        break;
                    } if(reverse==true) {
                        factor = 1.82;
                        break;
                    }

                case 2: // AUD
                    if(reverse==false) {
                        factor = 1.756;
                        break;
                    } if(reverse==true) {
                        factor = 0.62;
                        break;
                    }

                case 3: // CAD
                    if(reverse==false) {
                        factor = 1.71;
                        break;
                    } if(reverse==true) {
                        factor = 0.21;
                        break;
                    }

                case 4: // ISK
                    if(reverse==false) {
                        factor = 140.84;
                        break;
                    } if(reverse==true) {
                        factor = 3.28;
                        break;
                    }

                case 5: // AED
                    if(reverse==false) {
                        factor = 4.92;
                        break;
                    } if(reverse==true) {
                        factor = -272.15;
                        break;
                    }

                case 6: // ZAR
                    if(reverse==false) {
                        factor = 17.84;
                        break;
                    } if(reverse==true) {
                        factor = 2.47;
                        break;
                    }

                case 7: // THB
                    if(reverse==false) {
                        factor = 43.58;
                        break;
                    } if(reverse==true) {
                        factor = 2.47;
                        break;
                    }
                }

                double result = factor * value + offset;
                double roundedresult = Math.round(result *100)/100.00;


                label.setText(Double.toString(roundedresult));
                countLabel.setText("conversions: " + convCount);
            }
        }
    }


    private class ReverseListener implements ItemListener {


        public void itemStateChanged(ItemEvent event) {

            if (reverseCheck.isSelected()) {
                reverse = true;
    //          System.out.println("test test test");             <---- Test
            } 

            else {
                reverse = false;
            }

        }
    }

    private class ClearListener implements ActionListener {


        public void actionPerformed(ActionEvent event) {

            label.setText("---");
            convCount=0;
            countLabel.setText("Conversions: " + convCount);

        }
    }   




    // actionlistener exit
    private class CloseListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // closes window
            System.exit(0);
        }
    }

    // aboutlistener exit
    private class AboutListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This is my application designed to convert different values for you from your set paramaters. This is created by Daniel Siwiec. ©Daniel Siwiec 2018");
        }
    }}

1 个答案:

答案 0 :(得分:1)

您可以做的是,不要在一个int中跟踪您的转化,而在另一个类中对其进行跟踪。

public class ConvertTracker {
    private int conversions;

    public ConvertTracker() {
        conversions = 0;
    }

    public int getConversions() {
        return conversions;
    }

    public int incrementConversions() {
        conversions++;
        return conversions;
    }

    public void resetConversions() {
        conversions = 0;
    }
}

然后在两个面板之间传递相同的ConvertTracker实例。