Java 有人能告诉我为什么我的程序没有运行吗?
由于某种原因,这段代码没有运行…我不明白为什么…有人能解释一下吗?当我把代码放下的时候,我没有在代码上得到任何错误,但是当我尝试运行它的时候,它就不起作用了。我不知道我缺少什么组件来运行它。我知道我还没有完成displayInfo部分,但这似乎不是它无法运行的原因。我真的很困惑,我觉得我开始编写这段代码的方式与其他代码一样,但这段代码不会运行。对于初学者:Java 有人能告诉我为什么我的程序没有运行吗?,java,swing,nullpointerexception,jtextfield,gridbaglayout,Java,Swing,Nullpointerexception,Jtextfield,Gridbaglayout,由于某种原因,这段代码没有运行…我不明白为什么…有人能解释一下吗?当我把代码放下的时候,我没有在代码上得到任何错误,但是当我尝试运行它的时候,它就不起作用了。我不知道我缺少什么组件来运行它。我知道我还没有完成displayInfo部分,但这似乎不是它无法运行的原因。我真的很困惑,我觉得我开始编写这段代码的方式与其他代码一样,但这段代码不会运行。对于初学者: import java.awt.*; import java.awt.event.*; import javax.swing.*;
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) {
}
}
因此,它应该是:
JTextField recieveText; // declares the field, but does not create it!
然后:
应该是这样的:
JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());
但老实说,这段代码充满了“错误”。最好将其扔掉,在完成本教程的摆动轨迹后重新开始
但这似乎不是它无法运行的原因
始终复制/粘贴错误和异常输出 在我看来,这似乎是你生命的延续。看起来您对该问题中的代码做了一些改进,而不是那个问题,您决定发布另一个问题。你的代码给我的印象是,你需要彻底学习Swing,而这不是你可以通过提问来做到的。请注意,
weightx
的值必须介于0.0
和1.0
之间,而不是3.0
。在与Java和Swing相关的至少两个问题上,没有添加任何标记。为了给可能提供帮助的人提供最好的“覆盖”,别忘了添加这些标签!(例如,这些天我只收听标签)。我看到这一点的唯一原因是,偶尔我会挑选一个标签(比如)并查找缺少Swing标签的帖子。请查看&如果它有助于解决问题。
JFrame frame = new JFrame("RecallStudy");
// ..
frame.setContentPane(new RecallStudy());
JFrame frame = new RecallStudy();
// ..
frame.setTitle("RecallStudy");