当前位置: 技术问答>java相关
这个程序有问题!但可以运行!我没有办法了!请大家帮忙!
来源: 互联网 发布时间:2015-09-23
本文导语: import java.awt.*; import java.awt.event.*; import java.awt.event.KeyEvent; import javax.swing.*; import javax.swing.JTextField; import javax.swing.event.*; import javax.swing.border.*; import java.util.*; import java.lang.Runnable; import java.lang.Thread; class testThrea...
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.JTextField;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.lang.Runnable;
import java.lang.Thread;
class testThread implements Runnable{
Thread t;
int i=0;
int result[]=new int[4];
int ThreadInput;
testThread(int in)
{
t=new Thread(this,"test child Thread");
ThreadInput=in;
t.start();
}
public void run(){
for (i=3;i>=0&&ThreadInput>0;i--)
{
result[i]=(ThreadInput+7)%10;
ThreadInput=ThreadInput/10;
}
String msg = "This is run in the child Thread"+"
"+"the result is "+result[2]+result[3]+result[0]+result[1]+"
";
JOptionPane.showMessageDialog(null, msg);
}
}
public class test {
public static void main(String[] args) {
final JFrame frame = new JFrame("test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
final JPanel pContainer = new JPanel();
final JPanel Containers = new JPanel();
final JLabel firstIn = new JLabel("Input");
final JTextField Input=new JTextField(5);
Containers.add(firstIn);
Containers.add(Input);
pContainer.setLayout(new BoxLayout(pContainer,BoxLayout.Y_AXIS));
pContainer.add(Containers);
JPanel ReportPanel = new JPanel();
JButton report = new JButton("Report");
report.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int In;
In=Integer.parseInt(Input.getText());
new testThread(In);
}
});
frame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int In;
In=Integer.parseInt(Input.getText());
int c=e.getKeyChar();
if (c==KeyEvent.VK_ENTER)
{
new testThread(In);
}
}
});
ReportPanel.add(report);
Container contentPane = frame.getContentPane();
contentPane.add(pContainer, BorderLayout.CENTER);
contentPane.add(ReportPanel, BorderLayout.EAST);
frame.pack();
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
Dimension framesize=frame.getSize();
int x=(screen.width-framesize.width)/2;
int y=(screen.height-framesize.height)/2;
frame.setLocation(x,y);
frame.show();
}
}
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.JTextField;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
import java.lang.Runnable;
import java.lang.Thread;
class testThread implements Runnable{
Thread t;
int i=0;
int result[]=new int[4];
int ThreadInput;
testThread(int in)
{
t=new Thread(this,"test child Thread");
ThreadInput=in;
t.start();
}
public void run(){
for (i=3;i>=0&&ThreadInput>0;i--)
{
result[i]=(ThreadInput+7)%10;
ThreadInput=ThreadInput/10;
}
String msg = "This is run in the child Thread"+"
"+"the result is "+result[2]+result[3]+result[0]+result[1]+"
";
JOptionPane.showMessageDialog(null, msg);
}
}
public class test {
public static void main(String[] args) {
final JFrame frame = new JFrame("test");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
final JPanel pContainer = new JPanel();
final JPanel Containers = new JPanel();
final JLabel firstIn = new JLabel("Input");
final JTextField Input=new JTextField(5);
Containers.add(firstIn);
Containers.add(Input);
pContainer.setLayout(new BoxLayout(pContainer,BoxLayout.Y_AXIS));
pContainer.add(Containers);
JPanel ReportPanel = new JPanel();
JButton report = new JButton("Report");
report.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int In;
In=Integer.parseInt(Input.getText());
new testThread(In);
}
});
frame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int In;
In=Integer.parseInt(Input.getText());
int c=e.getKeyChar();
if (c==KeyEvent.VK_ENTER)
{
new testThread(In);
}
}
});
ReportPanel.add(report);
Container contentPane = frame.getContentPane();
contentPane.add(pContainer, BorderLayout.CENTER);
contentPane.add(ReportPanel, BorderLayout.EAST);
frame.pack();
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
Dimension framesize=frame.getSize();
int x=(screen.width-framesize.width)/2;
int y=(screen.height-framesize.height)/2;
frame.setLocation(x,y);
frame.show();
}
}
|
KeyListener里面错了,到换一下顺序就可以避免第一次空输入的转型错误
frame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int In;
int c=e.getKeyChar();
if (c==KeyEvent.VK_ENTER)
{
In=Integer.parseInt(Input.getText());
new testThread(In);
}
}
});
frame.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int In;
int c=e.getKeyChar();
if (c==KeyEvent.VK_ENTER)
{
In=Integer.parseInt(Input.getText());
new testThread(In);
}
}
});
|
完全同意DanielYWoo(绿色毒汁):
不然的话!就会出现事件传递异常的!你想啊!当你按下键盘的时候,文本框中输入还没有完成,就有人监听,把还正在但没有结束输入就取走!你想啊………………………………可怕不可怕!
呵呵!
不然的话!就会出现事件传递异常的!你想啊!当你按下键盘的时候,文本框中输入还没有完成,就有人监听,把还正在但没有结束输入就取走!你想啊………………………………可怕不可怕!
呵呵!