gpt4 book ai didi

java - 我怎样才能阻止程序第二次跳过我的检查?

转载 作者:行者123 更新时间:2023-11-30 08:43:44 26 4
gpt4 key购买 nike

我正在创建一个程序来接受二进制数字集并将它们转换为汉明码(有效地接受 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);
}


}

最佳答案

我发现很难在不完全了解您的代码在做什么的情况下回答这个问题,尤其是您“...用那个数字做时髦的####...”

但我确实知道您的程序结构存在重大问题,尤其是在冗长的 run() 方法中,您有许多嵌套的 while (...)循环和 do-while 循环,这些代码结构在线性处理控制台程序中似乎很常见,但在事件驱动的 Swing GUI 中却显得格格不入。

我建议您尝试使用一些状态相关的编码。例如,您可以为您的类提供 boolean 变量 enteringData 和 dataValidYet,以表示两个关键状态:用户现在是否正在向 JTextField 中输入数据,以及该数据是否已经过验证。然后在您的 JButton ActionListeners 中,使用 if 和 if/else block 来根据这些 boolean 字段的状态以及可能的类的其他关键字段来决定在按钮按下时做什么。

对于代码“骨架”示例,它还没有做任何事情,但希望能向您展示我正在谈论的结构:

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();
}
});
}
}

此外,作为与您的主要问题无关的“附带”建议,了解空布局和 setBounds() 对于 Swing 新手来说可能是创建复杂 GUI 的最简单和最佳方法,您创建的 Swing GUI 越多,使用它们时就会遇到越严重的困难。当 GUI 调整大小时,它们不会调整组件的大小,它们是增强或维护的皇家女巫,当放置在滚动 Pane 中时它们会完全失败,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时,它们看起来很糟糕.

请注意,如果这是我的程序,我会使用更多间接方法,包括创建单独的类以将程序的 GUI 部分与逻辑部分分开。

关于java - 我怎样才能阻止程序第二次跳过我的检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34095161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com