0
我尝试使用的ActionListener在我的计算器textarea的打印值textarea的价值,但我不明白为什么它不工作如何使用的ActionListener打印在我的计算器
你可以帮助我与此代码,
我希望我得到这个解决方案,因为在Java IM begineer,我会感谢您的帮助很多
CODE:
package calculator;
import javax.swing.SwingUtilities ;
public class Calculator {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new frame();
}
});
}
}
package calculator;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame ;
public class frame extends JFrame{
private TextPanel txt ;
private KeyPanel key ;
frame(){
setVisible(true);
setSize(300,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setName("Calc");
setLayout(new BorderLayout());
txt =new TextPanel();
key =new KeyPanel();
add(txt,BorderLayout.NORTH);
add(key,BorderLayout.CENTER);
}
}
package calculator;
import javax.swing.JPanel ;
import javax.swing.JTextArea ;
import java.awt.GridLayout;
import java.awt.BorderLayout ;
import java.awt.Font;
import javax.swing.JScrollPane;
public class TextPanel extends JPanel {
private JTextArea textarea ;
TextPanel(){
textarea= new JTextArea(5,5);
textarea.append("0");
setLayout(new BorderLayout());
add(new JScrollPane(textarea),BorderLayout.NORTH);
}
public String getText(){
return textarea.getText();
}
public void write(String text){
textarea.append(text);
}
}
package calculator;
import javax.swing.JButton ;
import java.awt.GridLayout ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel ;
public class KeyPanel extends JPanel /*implements ActionListener*/ {
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
private TextPanel text ;
public KeyPanel(){
b1=new JButton("0");
b2=new JButton("1");
b3=new JButton("2");
b4=new JButton("3");
b5=new JButton("4");
b6=new JButton("5");
b7=new JButton("6");
b8=new JButton("7");
b9=new JButton("8");
b10=new JButton("9");
b11=new JButton("+");
b12=new JButton("-");
b13=new JButton("*");
b14=new JButton("/");
b15=new JButton("C");
b16=new JButton("=");
setLayout(new GridLayout(5,5));
add(b1,new GridLayout());
add(b2,new GridLayout());
add(b3,new GridLayout());
add(b4,new GridLayout());
add(b5,new GridLayout());
add(b6,new GridLayout());
add(b7,new GridLayout());
add(b8,new GridLayout());
add(b9,new GridLayout());
add(b10,new GridLayout());
add(b11,new GridLayout());
add(b12,new GridLayout());
add(b13,new GridLayout());
add(b14,new GridLayout());
add(b15,new GridLayout());
add(b16,new GridLayout());
}
}
你会好得多通过教程去担任:http://download.oracle.com/javase/tutorial/uiswing/events/actionlistener.html – ControlAltDel
我要投票关闭这个问题作为题外话,因为教程是更合适的 – ControlAltDel