import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Runnable;
import java.lang.InterruptedException;
public class Phone implements ActionListener {
String action = "";
int callCount = 0;
int cn = 0;
boolean calling = false;
JFrame frame;
JPanel textPanel;
JPanel buttonPanel;
JTextField text;
JTextArea input;
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton star = new JButton("*");
JButton hash = new JButton("#");
JButton erase = new JButton("Erase");
JButton call = new JButton("Call");
JButton back = new JButton("<=");
public Phone() {
frame = new JFrame();
frame.setTitle("Phone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 400);
input = new JTextArea(10, 20);
textPanel = new JPanel();
textPanel.add(input);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3));
frame.add(textPanel, BorderLayout.NORTH);
frame.add(buttonPanel, BorderLayout.CENTER);
buttonPanel.add(one);
one.addActionListener(this);
one.setActionCommand("1");
buttonPanel.add(two);
two.addActionListener(this);
two.setActionCommand("2");
buttonPanel.add(three);
three.addActionListener(this);
three.setActionCommand("3");
buttonPanel.add(four);
four.addActionListener(this);
four.setActionCommand("4");
buttonPanel.add(five);
five.addActionListener(this);
five.setActionCommand("5");
buttonPanel.add(six);
six.addActionListener(this);
six.setActionCommand("6");
buttonPanel.add(seven);
seven.addActionListener(this);
seven.setActionCommand("7");
buttonPanel.add(eight);
eight.addActionListener(this);
eight.setActionCommand("8");
buttonPanel.add(nine);
nine.addActionListener(this);
nine.setActionCommand("9");
buttonPanel.add(star);
star.addActionListener(this);
star.setActionCommand("*");
buttonPanel.add(zero);
zero.addActionListener(this);
zero.setActionCommand("0");
buttonPanel.add(hash);
hash.addActionListener(this);
hash.setActionCommand("#");
buttonPanel.add(erase);
erase.addActionListener(this);
erase.setActionCommand("Erase");
buttonPanel.add(call);
call.addActionListener(this);
call.setActionCommand("Call");
buttonPanel.add(back);
back.addActionListener(this);
back.setActionCommand("Back");
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == one || e.getSource() == two || e.getSource() == three
|| e.getSource() == four || e.getSource() == five
|| e.getSource() == six || e.getSource() == seven
|| e.getSource() == eight || e.getSource() == nine
|| e.getSource() == zero){
action = input.getText();
action += e.getActionCommand();
action = addDash(action);
input.setText(action);
System.out.println(""+e.getActionCommand());
}
if(e.getSource() == erase){
action = input.getText();
action = erase(action);
input.setText(action);
System.out.println(""+e.getActionCommand());
}
if(e.getSource() == back){
action = input.getText();
action = remove(action);
input.setText(action);
System.out.println(""+e.getActionCommand());
}
if(e.getSource() == call){
if(!calling){
String temp = input.getText();
action = called(temp);
input.setText(temp + "\n" + action);
System.out.println(""+e.getActionCommand());
calling = true;
} else{
calling = false;
String temp = input.getText();
input.setText(temp + "\n" + "Hanging Up...");
r.run();
}
}
if(e.getSource() == star){
action = input.getText();
action = erase(action);
input.setText("YOU BAST*&#!");
System.out.println(""+e.getActionCommand());
}
if(e.getSource() == hash){
action = input.getText();
action = erase(action);
input.setText("OMG YOU KILLED KENNY!");
System.out.println(""+e.getActionCommand());
}
}
public String called(String s){
if(s.length() == 8){
return s = String.format("Calling %s%s%s", s.substring(0, 3),
s.substring(3, 8), s.substring(8, s.length()));
}
if(s.length() == 13){
return s = String.format("Calling %s%s%s", s.substring(0, 3),
s.substring(3, 8), s.substring(8, 13));
}
if(s.length() == 0){
return s;
}
return s = "Ill formed number";
}
public String addDash(String s){
if(s.length() == 4){
return s = String.format("%s-%s", s.substring(0, 3),
s.substring(3, s.length()));
}
if(s.length() == 9){
return s = String.format("%s%s-%s", s.substring(0, 3),
s.substring(3, 8), s.substring(8, s.length()));
}
if(s.length() > 12){
return s = String.format("%s%s%s", s.substring(0, 3),
s.substring(3, 8), s.substring(8, 13));
}
return s;
}
public String remove(String s){
return s = String.format("%s", s.substring(0, s.length() -1));
}
public String erase(String s){
return s = String.format("%s", s.substring(0, 0));
}
Runnable r = new Runnable() {
public void run() {
try{
Thread.sleep(2000);
action = input.getText();
action = erase(action);
input.setText(action);
}
catch(InterruptedException e){
System.out.println("Thread Interrupted" + e);
}
}
};
}
我需要线程上方的三行代码在线程休眠2秒之前执行。这样,当手机“挂断”时,它会显示在GUI文本区域上,然后发生睡眠,模拟挂机。
我不明白为什么在运行时没有发生这种情况,即使变量调用设置为false;跟随它的两行不会被执行。
我指的代码是:
else{
calling = false;//executes
String temp = input.getText();//does not execute
input.setText(temp + "\n" + "Hanging Up...");//does not execute
r.run();
}
此代码位于actionPerformed方法的调用按钮的if语句中。
答案 0 :(得分:2)
在Java中Runnable
!= Thread
。在您的代码中,您可以在runnable上调用run()
,但您从未创建过线程。您需要执行类似
new Thread(r).start();
某处。 所以我不确定你说的是3行代码,但是线程从未创建过。
确定。假设您根本不创建任何新线程,只能直接从run()
方法内部执行代码。在这种情况下,Thread.sleep(2000)
只会锁定UI 2秒钟。
答案 1 :(得分:1)
执行Runnable
的方式使其在当前线程上运行,而不是在新线程上运行。要在新线程上运行它,请使用它:
new Thread(r).start();