[英]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;
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) {
}
}
出於某種原因,這段代碼沒有運行......我不明白為什么......有人可以解釋一下嗎? 當我剛放下它時,我的代碼沒有任何錯誤,但是第二次我嘗試運行它時它不起作用。 我很困惑我缺少哪些組件才能使其運行。 我知道我還沒有完成 displayInfo 部分,但這似乎不是它無法運行的原因。 我真的很困惑,我覺得我開始這段代碼的方式與其他代碼一樣,但這個代碼無法運行。
對於初學者:
JTextField recieveText; // declares the field, but does not create it!
所以應該是:
JTextField recieveText = new JTextField("No NullPointerException!");
然后:
JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());
應該是這樣的:
JFrame frame = new RecallStudy();
// ..
frame.setTitle("RecallStudy");
但老實說,該代碼充滿了“錯誤”。 最好在經歷了教程的 Swing 跟蹤后將其扔掉並重新開始。
但這似乎不是它無法運行的原因
始終復制/粘貼錯誤和異常輸出!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.