java 关于图形界面和事件处理
在界面中添加两个组件,分别为文本框和按钮,在文本框中输入一个数字,按下按钮后输出该数的平方(最好用getText()和setText()...
在界面中添加两个组件,分别为文本框和按钮,在文本框中输入一个数字,按下按钮后输出该数的平方(最好用getText()和setText()
展开
2个回答
展开全部
package client;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.event.AncestorListener;
public class Test extends JFrame{
private JTextField text;
private JButton button;
public Test(){
super();
this.setSize(new Dimension(250,100));
JPanel panel = new JPanel();
panel.setSize(new Dimension(250,100));
text = new JTextField();
text.setPreferredSize(new Dimension(150,30));
button = new JButton("确认");
button.setPreferredSize(new Dimension(60,30));
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String s = text.getText();
if(s!=null){
double d = Double.parseDouble(s);
double result = d*d;
text.setText(String.valueOf(result));
}
}
});
panel.add(text);
panel.add(button);
this.setContentPane(panel);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询