关于java的题目: 设计一个GUI程序,在窗口的一个文本框中输入字符,其内容在窗口的另一个 10
public class Test extends JFrame{
JFrame frame=null;
JPanel panel=null;
JTextArea label2;
public Test(){
init();
}
public void init(){
this.setTitle("input");
this.setBounds(300, 300, 200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(new Panel());
setResizable(false);
setVisible(true);
frame=new JFrame("show");
frame.setBounds(600, 300, 200,200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(false);
panel=new JPanel();
label2=new JTextArea();
label2.setLineWrap(true);
label2.setBounds(0, 0, 200, 200);
panel.add(label2);
frame.add(panel);
}
class Panel extends JPanel{
JTextArea text;
JLabel label ;
public Panel(){
setLayout(null);
text=new JTextArea();
text.setBounds(0, 0, 200, 200);
text.setLineWrap(true);
add(text);
text.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
String str=text.getText();
label2.setText(str);
frame.setVisible(true);
}
}
});
}
}
public static void main(String[] args){
new Test();
}
}
结果:
给你个大概的例子吧,能跑写的有点现眼,自己按照面向对象监听模式优化一下吧。望采纳。
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.text.Document;
public class TestFrame {
private static Frame2 f22 = new Frame2();
private static JFrame f1 = new JFrame();
public static void main(String []arg){
final JTextField jt1 = new JTextField();
Document dt = jt1.getDocument();
dt.addDocumentListener(new javax.swing.event.DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) {
f22.setLabelText(jt1.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
f22.setLabelText(jt1.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
f22.setLabelText(jt1.getText());
}
}
);
f1.add(jt1);
f1.setDefaultCloseOperation(f1.EXIT_ON_CLOSE);
f1.setSize(100, 100);
f1.show();
f22.show();
}
}
class Frame1 extends JFrame{
}
class Frame2 extends JFrame{
private JLabel lable = new JLabel();
public Frame2(){
super();
this.setSize(100, 100);
this.add(lable);
}
public void setLabelText(String str){
lable.setText(str);
}
}