gpt4 book ai didi

java - 为什么按下 JButton 后没有任何反应

转载 作者:行者123 更新时间:2023-12-02 03:29:33 49 4
gpt4 key购买 nike

我有以下代码 - 除了定义按下按钮时应采取的操作的方法之外,生成的所有 Netbeans。当我按下按钮时,首先它没有出现,并且 Netbeans IDE 的标准输出窗口中没有显示任何内容。出了什么问题?

import javax.swing.JFrame;

public class CodeCenter extends JFrame {

/**
* Creates new form CodeCenter
*/
public CodeCenter() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);

jButton1.setText("Run");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(288, 288, 288)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(17, 17, 17)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//writes each line of user code to seperate array place
//userCodeArray can then be passed into an enhanced for loop to act on each line individually
// May need to be moved into different class
String[] userCodeArray = getTextasArray();

System.out.println("Printing");

if(userCodeArray[0].equals("TEST")) {
System.out.println("Text read");
} else {
System.out.print("Text not read");
}

}

//Sets up Intrigued machine and the codecenter
public static void main(String args[]) {

//Not relevant to question
IntriguedMachine machine = new IntriguedMachine();

/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CodeCenter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CodeCenter().setVisible(true);
}
});
}

//Takes the user code, splits it into a single String for each line, puts each String into userCodeArray, and returns userCodeArray
//Not automatically-generated
public String[] getTextasArray() {
int lineCount = jTextArea1.getLineCount();
String[] userCodeArray = new String[lineCount];
for(int i = 0; i<lineCount; lineCount++){

String [] throwawayArray = jTextArea1.getText().split("//n");
userCodeArray = throwawayArray;
}

return userCodeArray;
} //End of getTextasArray;

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
}

最佳答案

好吧,无意冒犯,但是你的 getTextasArray 函数完全被劫持了。毫无疑问,按下 JButton 时没有任何反应的原因是 for 循环由于拼写错误而无限循环。让我们看一下循环声明,好吗?

for(int i = 0; i<lineCount; lineCount++){

你能看出哪里出了问题吗?查看增量器部分。您不是增加 i,而是增加 lineCount,我认为您无意这样做。这意味着 i 永远不会大于或等于 lineCount。正如你亲眼所见,你必须小心那些东西。 :)

但是让我们摆脱这个循环并以正确的方式进行。我有点明白您试图通过使用 for 循环来完成什么,但是有一个更简单的方法。只需像这样编写您的函数:

public String[] getTextasArray() {
return jTextArea1.getText().split("\n");
}

这将获取文本区域并用换行符将其分割。请注意,我使用了字符串 \n 而不是 //n。使用 //n 会将其分割为文字“斜杠斜杠 N”。希望这能让您得到您想要的!

关于java - 为什么按下 JButton 后没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295738/

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