我怎样才能阻止程序第二次跳过我的支票?

时间:2015-12-04 18:34:12

标签: java swing jframe jbutton actionlistener

我正在创建一个程序来接收二进制数字组并将它们转换为汉明码(有效地取8位数,变成12位,打印出来)但我遇到了麻烦。目前,我正在使用JTextField为用户输入他们的号码,然后他们按下JButton输入数据。然后,我用这个数字做一个时髦的狗屎把它放入一个列表并确认这是他们想要输入的最后一个数字。如果他们单击一个名为yes的JButton(按钮中的新文本,但同一个按钮),如果继续做我需要的。但如果他们点击另一个名为no的JButton,它会返回并重复相同的过程。我的问题是在单击否一次之后,程序停止允许您在步骤中按否以检查是否要添加另一个数字列表。 IT似乎一起跳过检查,并假设他们按下了是,因为它完成了所有输入完成后完成的剩余工作。

我的代码因为弄乱了几个小时而有点麻烦。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;


public class MainProgram extends JFrame
{
public MainProgram()
    {
    }

public static void main(String[] args)
    {
    MainProgram mp = new MainProgram();
    mp.run();
    }

private void run()
    {
    java.util.List<Integer> streamSplit = new ArrayList<>();
    java.util.List<Integer> tempEight = new ArrayList<>();
    java.util.List<Integer> finalStream = new ArrayList<>();
    yes.addActionListener(new ActionListener()
        {
        @Override
        public void actionPerformed(ActionEvent e)
            {
            checkYes = true;
            }
        });
    no.addActionListener(new ActionListener()
        {
        @Override
        public void actionPerformed(ActionEvent e)
            {
            checkNo = true;
            }
        });
    init();
    yesChange("Enter");
    boolean confirm = false;
    int aCheck = 0;
    while (aCheck == 0)
        {
        confirm = false;
        while (!confirm)
            {
            setTopText("<html>Please enter your next 8 bits. Do not enter more than 8 bits.<br> Press Enter when done</html>");
            yesChange("Enter");
            confirm =  checkYes();
            }
        confirm = false;
        setTopText("Digits Successfully added.");
        int stream =  checkInput();
        do
            {
            streamSplit.add(stream % 10);
            stream /= 10;
            } while (stream != 0);
        setYesNo();
        setTopText("<html>Are you finished entering streams?</html>");
        yesChange("YES");
        noChange("NO");
        aCheck = 2;
        checkYes();
        checkNo();
        while (aCheck == 2)
            {
            if ( checkNo())
                {
                aCheck = 0;
                System.out.println("CrapNo");
                }
            else if (checkYes())
                {
                aCheck = 1;
                System.out.println("CrapYes");
                }
            }
        }




    int arrayLength = streamSplit.size();
    int bufferLength = 8 - arrayLength % 8;
    int numberOfStreams = 0;
    if (bufferLength != 8)
        {
        numberOfStreams = arrayLength / 8 + 1;
        } else
        {
        numberOfStreams = arrayLength / 8;
        }
    int tempStreams = numberOfStreams;
    System.out.println(numberOfStreams + "<Streams Buffer>" + bufferLength);
    while (bufferLength > 0 && bufferLength != 8)
        {
        streamSplit.add(0);
        bufferLength--;
        }
    while (tempStreams > 0)
        {
        for (int i = 0; i < 8; i++)
            {
            tempEight.add(streamSplit.get(i));
            }
        if ((tempEight.get(0) + tempEight.get(1) + tempEight.get(3) + tempEight.get(4) + tempEight.get(6)) % 2 == 0)
            {
            tempEight.add(0, 0);
            } else
            {
            tempEight.add(0, 1);
            }
        if ((tempEight.get(1) + tempEight.get(3) + tempEight.get(5) + tempEight.get(6) + tempEight.get(7)) % 2 == 0)
            {
            tempEight.add(1, 0);
            } else
            {
            tempEight.add(1, 1);
            }
        if ((tempEight.get(3) + tempEight.get(4) + tempEight.get(5) + tempEight.get(8) + tempEight.get(9)) % 2 == 0)
            {
            tempEight.add(3, 0);
            } else
            {
            tempEight.add(3, 1);
            }
        if ((tempEight.get(7) + tempEight.get(8) + tempEight.get(9) + tempEight.get(10)) % 2 == 0)
            {
            tempEight.add(7, 0);
            } else
            {
            tempEight.add(7, 1);
            }
        tempStreams--;
        for (int i = 0; i < 12; i++)
            {
            finalStream.add(tempEight.get(0));
            tempEight.remove(0);
            }
        }

    Collections.reverse(streamSplit);
    System.out.print("Your original bit-stream was: ");
    for (int i = 0; i < numberOfStreams * 2; i++)
        {
        for (int j = 0; j < 4; j++)
            {
            System.out.print(streamSplit.get(j + (i * 4)));
            }
        System.out.print(" ");
        }
    System.out.println();
    System.out.print("Your new HammingCode bit-stream is: ");
    for (int i = 0; i < numberOfStreams * 3; i++)
        {
        for (int j = 0; j < 4; j++)
            {
            System.out.print(finalStream.get(j + (i * 4)));
            }
        System.out.print(" ");
        }
    System.out.println();
    }


public Boolean checkYes = false;
public Boolean checkNo = false;
private JFrame frame = new JFrame("Absolute Layout Example");
private JPanel contentPane = new JPanel();
private JLabel topText = new JLabel("Welcome to my Hamming Code Generator", JLabel.CENTER);
private JTextField inputText = new JTextField();
private JButton yes = new JButton("YES");
private JButton no = new JButton("NO");

public void init()
    {
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane.setOpaque(true);
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);

    topText.setLocation(0, 0);
    topText.setSize(400, 50);
    topText.setBorder(BorderFactory.createLineBorder(Color.black));

    inputText.setLocation(0,50);
    inputText.setSize(400,75);
    inputText.setBorder(BorderFactory.createLineBorder(Color.black));

    yes.setSize(80, 40);
    yes.setLocation(60, 135);

    no.setSize(80, 40);
    no.setLocation(260, 135);

    contentPane.add(topText);
    contentPane.add(inputText);
    contentPane.add(yes);
    contentPane.add(no);

    frame.setContentPane(contentPane);
    frame.setSize(400, 225);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    }

public void setTopText(String s)
    {
    topText.setText(s);
    }

public void setYesNo()
    {
    checkYes = false;
    checkNo = false;
    }

public Boolean checkYes() {return checkYes;}

public Boolean checkNo() {return checkNo;}

public int checkInput()
    {
    String temp1 = inputText.getText();
    int temp = Integer.parseInt(temp1);
    return temp;
    }

public void yesChange(String s)
    {
    yes.setText(s);
    }

public void noChange(String s)
    {
    no.setText(s);
    }


}

1 个答案:

答案 0 :(得分:2)

我发现很难回答这个问题,不完全知道你的代码在做什么,特别是你"... do funky #### with that number..."

的部分

但我确实知道您的程序结构存在重大问题,尤其是在冗长的run()方法中,您有许多嵌套的while (...)循环和do-while循环,可能是代码结构似乎在线性处理控制台程序中的家里,但在事件驱动的Swing GUI中似乎不合适。

我建议您尝试使用一些依赖于状态的编码。例如,您可以为您的类提供布尔变量,输入data和dataValidYet,以表示两个关键状态:用户现在是否将数据输入JTextField,以及该数据是否尚未经过验证。然后在你的JButton ActionListeners中,使用if和if / else块来决定按钮按下的操作,具体取决于这些布尔字段的状态,以及类的其他关键字段。

代码&#34;骨架&#34;例如,一个尚未做任何事情的人,但希望能告诉你我正在谈论的结构:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class StateMachine extends JPanel {
    private static final String INITIAL_TITLE = "Please enter your next 8 bits. "
            + "Do not enter more than 8 bits.\n"
            + "Press Enter when done";
    private static final String ARE_YOU_FINISHED = "Are you finished entering streams?";
    private static final String YES = "Yes";
    private static final String ENTER = "Enter";
    private static final String NO = "No";
    private static int GAP = 8;
    private static final int COLUMNS = 30;

    // this is a JTextArea built to look like a JLabel
    private JTextArea topTextArea = new JTextArea(2, COLUMNS);
    private JTextField dataEntryField = new JTextField(COLUMNS);
    private JButton yesEnterButton = new JButton(ENTER);
    private JButton noButton = new JButton(NO);
    private boolean enteringData = true;
    private boolean dataValidYet = false;

    public StateMachine() {
        yesEnterButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                yesEnterButtonActionPerfromed(e);
            }
        });

        noButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                noButtonActionPerfromed(e);
            }
        });

        topTextArea.setWrapStyleWord(true);
        topTextArea.setLineWrap(true);
        topTextArea.setFocusable(false);
        topTextArea.setEditable(false);
        topTextArea.setOpaque(false);
        topTextArea.setText(INITIAL_TITLE);

        JPanel innerButtonPanel = new JPanel(new GridLayout(1, 0, GAP, 0));
        innerButtonPanel.add(yesEnterButton);
        innerButtonPanel.add(noButton);
        JPanel outerButtonPanel = new JPanel();
        outerButtonPanel.add(innerButtonPanel);

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new BorderLayout(GAP, GAP));
        add(topTextArea, BorderLayout.PAGE_START);
        add(dataEntryField, BorderLayout.CENTER);
        add(outerButtonPanel, BorderLayout.PAGE_END);
    }

    protected void noButtonActionPerfromed(ActionEvent e) {
        // TODO depending on state of enteringData and dataValidYet booleans
        // change text in buttons, do things with JTextField data
        // set state of enteringData and dataValidYet booleans

        if (enteringData) {
            // a no press is meaningless if entering data
            return;
        }

        // .... more

    }

    private void yesEnterButtonActionPerfromed(ActionEvent e) {
        // TODO depending on state of enteringData and dataValidYet booleans
        // change text in buttons, do things with JTextField data
        // set state of enteringData and dataValidYet booleans

        if (enteringData) {
            topTextArea.setText(ARE_YOU_FINISHED);
            yesEnterButton.setText(YES);
            yesEnterButton.setActionCommand(YES);
            enteringData = false;
            return;
        }
        // .... more

    }

    private static void createAndShowGui() {
        StateMachine mainPanel = new StateMachine();

        JFrame frame = new JFrame("State Machine");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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

此外,作为&#34; side&#34;推荐,一个与您的主要问题无关,理解空布局和setBounds()似乎可以像Swing新手那样创建复杂GUI的最简单和最好的方式,您创建的Swing GUI更多使用它们时会遇到更严重的困难。当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。

请注意,如果这是我的程序,我会使用更多的间接,包括创建单独的类,以从逻辑部分中分离出程序的GUI部分。