Java程序 题目1: 综合图形界面程序设计 实验目的和要求:学习和理解JAVA SWING中的容

2024-11-26 10:51:03
推荐回答(1个)
回答1:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
class Frame20 extends JFrame implements ActionListener
{ JButton jb1;
JTextField jtf1,jtf2;
Frame20()
{ setLayout(new BorderLayout());
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();

jtf1 = new JTextField("", 12);
jp1.add(new JLabel("人民币金额:"));
jp1.add(jtf1);

jb1=new JButton("转换成美元等于");
jb1.addActionListener(this);
jp2.add(jb1);

jtf2 = new JTextField("", 12);
jp3.add(new JLabel("美元金额:"));
jp3.add(jtf2);
add(jp1);
add(jp2);
add(jp3);

setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
setBounds(120,125,300,200); //设定窗体大小和位置
setTitle("美元转换"); //设定窗体标题
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //设定窗体可见
}
public void actionPerformed(ActionEvent e)
{ String rmb;
double dollar;
if(e.getSource()==jb1)
{ rmb=jtf1.getText();
dollar=Integer.parseInt(rmb)*0.1609;
jtf2.setText(String.valueOf(dollar));
}
}
}
public class meiyuan_zhuanghuan
{ public static void main(String []args)
{ new Frame20();
}
}