java编写图形界面应用程序,其中包括一个文本框和一个按钮.用户单击按钮后,窗口的标题为文本框中的内容

如题所述

第1个回答  推荐于2017-09-27
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class changeTitle extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int width = 200;
public static final int height = 200;
JTextField text1;
JLabel la;
JButton jbn;
changeTitle(){
setTitle("hello");
setSize(width, height);
setLayout(new FlowLayout());
text1=new JTextField(15);
la=new JLabel("标题:");
jbn=new JButton("更改标题");
add(la);
add(text1);
add(jbn);
jbn.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbn){
setTitle(text1.getText());
}

}

public static void main(String[] args ){
changeTitle changetitle=new changeTitle();
changetitle.setVisible(true);
changetitle.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}本回答被提问者采纳
相似回答