英文:
Can someone tell me why my program isnt running?
问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RecallStudy extends JFrame implements KeyListener, ActionListener {
// CREATION OF ELEMENTS
JLabel recallLabel;
JTextField enterText;
String enterTex; // Note: It seems like there's a typo here, it should be "enterText" instead of "enterTex"
JTextField recieveText; // Typo: It should be "receiveText" instead of "recieveText"
JPanel displayInfo;
GridBagConstraints constraints = new GridBagConstraints();
public static void main(String[] args) {
// MAKING THE WINDOW
JFrame frame = new JFrame("RecallStudy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setContentPane(new RecallStudy());
frame.pack();
frame.setVisible(true);
}
// GRIDLAYOUT CONSTRUCTION
public RecallStudy() {
setLayout(new GridBagLayout());
constraints.weightx = 3.0;
constraints.weighty = 3.0;
constraints.fill = GridBagConstraints.BOTH;
int x, y; // for clarity
constraints.gridheight = 2; // span two rows
addGB(recieveText, x = 2, y = 1);
constraints.gridheight = 2; // set it back
addGB(enterText, x = 0, y = 1);
constraints.gridwidth = 1; // span two columns
addGB(new JLabel("Recall"), x = 1, y = 0);
constraints.gridwidth = 2; // set it back
addGB(new JLabel(""), x = 0, y = 0);
constraints.gridwidth = 1;
addGB(new JLabel(""), x = 2, y = 0);
constraints.gridwidth = 1;
addGB(new JLabel(""), x = 2, y = 2);
constraints.gridwidth = 1;
addGB(new JLabel(""), x = 0, y = 2);
constraints.gridwidth = 1;
// Set the proper restrictions on the listeners
enterText.addKeyListener(this);
recieveText.setEditable(false);
}
void addGB(Component component, int x, int y) {
constraints.gridx = x;
constraints.gridy = y;
add(component, constraints);
}
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
// Clear the text components.
enterText.setText("");
recieveText.setText(enterTex);
// Return the focus to the typing area.
enterText.requestFocusInWindow();
}
private void displayInfo(KeyEvent e, String keyStatus) {
// Your implementation for displayInfo should be added here
}
}
Note: The provided code seems to have some typographical errors, particularly in variable names ("enterTex" should be "enterText" and "recieveText" should be "receiveText"). Also, the displayInfo
method is not implemented in the given code. If the code is still not working after correcting these issues, there might be other external factors causing the problem.
英文:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RecallStudy extends JFrame
implements KeyListener,
ActionListener
{
//CREATION OF ELEMENTS
JLabel recallLabel;
JTextField enterText;
String enterTex;
JTextField recieveText;
JPanel displayInfo;
GridBagConstraints constraints = new GridBagConstraints();
public static void main (String [] args) {
//MAKING THE WINDOW
JFrame frame = new JFrame("RecallStudy");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(500, 500);
frame.setLocation(200, 200);
frame.setContentPane(new RecallStudy());
frame.pack();
frame.setVisible(true);
}
//GRIDLAYOUT CONSTRUCTION
public RecallStudy() {
setLayout(new GridBagLayout());
constraints.weightx = 3.0;
constraints.weighty = 3.0;
constraints.fill = GridBagConstraints.BOTH;
int x, y; // for clarity
constraints.gridheight = 2; // span two rows
addGB(recieveText, x =2, y = 1);
constraints.gridheight = 2; // set it back
addGB(enterText, x = 0, y = 1);
constraints.gridwidth = 1; // span two columns
addGB(new JLabel("Recall"), x = 1, y = 0);
constraints.gridwidth = 2; // set it back
addGB(new JLabel(""), x = 0, y = 0);
constraints.gridwidth = 1;
addGB(new JLabel(""), x = 2, y =0);
constraints.gridwidth = 1;
addGB(new JLabel(""), x = 2, y = 2);
constraints.gridwidth = 1;
addGB (new JLabel(""), x = 0, y =2);
constraints.gridwidth = 1;
//Set the proper restrictions on the listeners
enterText.addKeyListener(this);
recieveText.setEditable(false);
}
void addGB(Component component, int x, int y) {
constraints.gridx = x;
constraints.gridy = y;
add(component, constraints);
}
public RecallStudy(String name) {
super (name);
}
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
enterText.setText("");
recieveText.setText(enterTex);
//Return the focus to the typing area.
enterText.requestFocusInWindow();
}
private void displayInfo (KeyEvent e, String keyStatus) {
}
}
For some reason, this code is not running...I cannot understand why...can someone please explain? I do not get any errors on my code when I just have it down, but the second I try to run it it does not work. I am confused as to what components I am missing to make it run. I know that I have not finished the displayInfo section, but that does not seem to be the reason why it won't run. I am just really confused, I feel like I started this code the same way I do every other but this one just won't run.
答案1
得分: 1
对于入门:
JTextField recieveText; // 声明字段,但不创建它!
所以应该是:
JTextField recieveText = new JTextField("没有空指针异常!");
然后:
JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());
应该类似于:
JFrame frame = new RecallStudy();
// ..
frame.setTitle("RecallStudy");
但老实说,那段代码充满了 '错误'。最好是将其丢弃,在完成 Swing 教程的基础上重新开始。
> 但这似乎不是它无法运行的原因
始终复制/粘贴错误和异常输出!
英文:
For starters:
JTextField recieveText; // declares the field, but does not create it!
So it should be:
JTextField recieveText = new JTextField("No NullPointerException!");
Then:
JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());
Should be something like:
JFrame frame = new RecallStudy();
// ..
frame.setTitle("RecallStudy");
But honestly, that code is riddled with 'errors'. It'd be better to toss it out and start again after going through the Swing trail of the tutorial.
> but that does not seem to be the reason why it won't run
Always copy/paste error and exception output!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论