Java 事件调度线程异常?
这是我的代码,我在eclipse上运行它,它显示按钮和字段的框架,使用按钮应该返回字段fe中键入的方程的值。当我在字段中输入2+2并单击“oblicz”时,它应该返回4,但它没有返回,并且它会删除一些奇怪的异常。我错过什么了吗Java 事件调度线程异常?,java,eclipse,Java,Eclipse,这是我的代码,我在eclipse上运行它,它显示按钮和字段的框架,使用按钮应该返回字段fe中键入的方程的值。当我在字段中输入2+2并单击“oblicz”时,它应该返回4,但它没有返回,并且它会删除一些奇怪的异常。我错过什么了吗 static JTextField pole1; public static void main(String[]args) { JFrame frame = new JFrame("applet 1.
static JTextField pole1;
public static void main(String[]args)
{
JFrame frame = new JFrame("applet 1.0");
frame.setSize(700, 600);
frame.setLocation(300, 100);
frame.setResizable(false);
frame.setLayout(null);
frame.setVisible(true);
// pole
pole1 = new JTextField();
pole1.setBounds(10, 10, 230, 50);
Font F1 = new Font("Comic Sans",Font.BOLD,20);
pole1.setFont(F1);
pole1.setHorizontalAlignment(JTextField.RIGHT);
frame.add(pole1);
// button
JButton button = new JButton("oblicz");
button.setBounds(10, 80, 100, 70);
button.setBorder(null);
frame.add(button);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
{
String str = pole1.getText();
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = str;
try
{
pole1.setText(String.valueOf(engine.eval(foo)));
}
catch (ScriptExecption e)
{
}
}
}
});
你写的代码对我有用 然而,为了更有效地使用Swing,我对您的代码做了一些更改
package com.ggl.testing;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class JavascriptCalculator implements Runnable {
private JButton button;
private JTextField pole1;
public static void main(String[] args) {
SwingUtilities.invokeLater(new JavascriptCalculator());
}
@Override
public void run() {
JFrame frame = new JFrame("Applet 1.0");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.getRootPane().setDefaultButton(button);
frame.setVisible(true);
}
private JPanel createPanel() {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.setLayout(new BorderLayout());
// pole
JPanel polePanel = new JPanel();
polePanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
pole1 = new JTextField(20);
Font f1 = new Font("Comic Sans", Font.BOLD, 20);
pole1.setFont(f1);
pole1.setHorizontalAlignment(JTextField.RIGHT);
polePanel.add(pole1);
panel.add(polePanel, BorderLayout.CENTER);
// button
button = new JButton("oblicz");
panel.add(button, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String str = pole1.getText().trim();
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = str;
try {
pole1.setText(String.valueOf(engine.eval(foo)));
} catch (ScriptException e) {
e.printStackTrace();
}
}
});
return panel;
}
}
为我们提供“奇怪的”异常+堆栈跟踪如何?以及代码。。在这里,在这个问题上,不是作为一个链接到其他网站。对不起,我乞丐-这是我的第一个问题这里没有担心,我们都在学习。我更新了你的问题,所以代码在那里。但是我们仍然需要堆栈跟踪。你能解释一下我怎么才能得到它吗?我在用eclipseThank非常感谢你Gilbert!!这些提示。。。我爱他们!祝你有愉快的一天当我尝试在我的电脑上运行这个程序时,它返回错误:错误:找不到或加载主类programy.okno1it不是,这就是为什么我尝试创建全新的java包,即使它返回相同的错误,该死的令人沮丧