Java 作业四
1、设计一个Applet,完成登陆界面。
效果图
代码:
package one;
import java.awt.;
import java.awt.event.;
import javax.swing.*;
@SuppressWarnings({ “serial”, “unused” })
public class Dengru extends JFrame {
private JPanel p;
private JLabel lblName, lblPwd;
private JTextField txtName;
private JPasswordField txtPwd;
private JButton btnOk;
public Dengru() {
super("界面");
p = new JPanel();
p.setLayout(null);
lblName = new JLabel("用户名");
lblPwd = new JLabel("密码");
txtName = new JTextField(20);
txtPwd = new JPasswordField(20);
btnOk = new JButton("登入");
// 注册确定按钮的事件处理
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String strName = txtName.getText();
String strPwd = new String(txtPwd.getPassword());
System.out.println("用户名:" + strName + "\n密码:" + strPwd);
}
});
lblName.setBounds(20, 20, 150, 25);
txtName.setBounds(90, 20, 150, 25);
lblPwd.setBounds(20, 50, 150, 25);
txtPwd.setBounds(90, 50, 150, 25);
btnOk.setBounds(130, 80, 60, 25);
p.add(lblName);
p.add(txtName);
p.add(lblPwd);
p.add(txtPwd);
p.add(btnOk);
this.add(p);
this.setSize(300, 200);
this.setLocation(300, 180);
// 设置窗体不可改变大小
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Dengru f = new Dengru();
f.setVisible(true);
}
}
效果图
代码:
package one;
import java.awt.;
import java.awt.event.;
import javax.swing.*;
public class A extends JFrame {
private static final long serialversionUID = 1L;
public A() {
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("界面");
setBounds(300, 200, 300, 180);
Container cp = getContentPane();
cp.setLayout(null);
JLabel j1 = new JLabel("用户名");
j1.setBounds(20, 20, 150, 25);
final JTextField name = new JTextField();
name.setBounds(90, 20, 150, 25);
JLabel j12 = new JLabel("密码:");
j12.setBounds(20, 50, 150, 25);
final JPasswordField password = new JPasswordField();
password.setBounds(90, 50, 150, 25);
cp.add(j1);
cp.add(name);
cp.add(j12);
cp.add(password);
JButton jb = new JButton("登录");
jb.setBounds(90, 80, 60, 25);
cp.add(jb);
final JButton button = new JButton();
button.setText("重置");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arge) {
name.setText("");
password.setText("");
}
});
button.setBounds(180, 80, 60, 25);
getContentPane().add(button);
}
public static void main(String[] args) {
new A();
}
// TODO Auto-generated method stub
}
效果图
代码:
package one;
import java.applet.;
import java.awt.;
import java.util.;
import java.awt.event.;
public class C extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
TextField name = new TextField(30);
TextField pw = new TextField(30);
TextField pw1 = new TextField(30);
Label la1 = new Label();
Label la2 = new Label();
Label la3 = new Label();
Button b1 = new Button(“登陆”);
public void actionPerformed1(ActionEvent e) {
if (e.getSource() == pw)
pw1.setText("action from pw textfield");
else if (e.getSource() == b1)
pw1.setText("action from b1 button");
}
public void init() {
this.setLayout(new FlowLayout());
la1.setText("用户名:");
this.add(la1);
this.add(name);
la2.setText("密码;");
this.add(la2);
pw.setEchoChar('*');
this.add(pw);
pw.addActionListener(this);
la3.setText("显示密码:");
this.add(la3);
this.add(pw1);
this.add(b1);
b1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
pw1.setText(pw.getText());
}
}
2、设计一个Applet,完成转换功能界面,要求:包含两个文本区域,左边为输入需要转换的小写数字货币形式,例如:365.98,右边为输出的货币大写形式,例如:叁佰陆拾伍元玖角捌分。
效果图
代码:
package one;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class B extends JFrame implements CaretListener {
JTextField tfdMonry, tfdStr;
public B(String str) {
super(str);
this.setBounds(300, 240, 500, 140);
this.getContentPane().setBackground(Color.white);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
Font font = new Font("a", Font.BOLD, 20);
JLabel lb = new JLabel("金额");
lb.setFont(font);
this.getContentPane().add(lb);
tfdMonry = new JTextField("365.98", 20);
tfdMonry.setFont(font);
tfdMonry.setHorizontalAlignment(JTextField.RIGHT);
this.getContentPane().add(tfdMonry);
JLabel lb2 = new JLabel("中文大写形式");
lb2.setFont(font);
this.getContentPane().add(lb2);
tfdStr = new JTextField(20);
tfdStr.setFont(font);
tfdStr.setHorizontalAlignment(JTextField.RIGHT);
tfdStr.setEditable(false);
this.getContentPane().add(tfdStr);
tfdMonry.addCaretListener(this);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e) {
String str = tfdMonry.getText();
try {
double x = Double.parseDouble(str);
tfdStr.setText(RMBtoString(x));
} catch (NumberFormatException e1) {
JDialog dlg = new JDialog(this, true);
dlg.setSize(300, 80);
dlg.add(new JLabel(str + "不能转换成浮点数,请重新输入!"));
int lx = this.getX();
int ly = this.getY();
dlg.setLocation(lx + 20, ly + 20);
dlg.setVisible(true);
}
}
private String RMBtoString(double x) {
String digit = "零壹贰叁肆伍陆柒捌玖";
String yuan = "亿千百拾万千百拾元角分";
String result = "";
int y = (int) (x * 100 + 0.5);
int i = yuan.length() - 1;
while (y > 0 && i > 0) {
String str = "" + digit.charAt(y % 10) + yuan.charAt(i);
result = str + result;
y = y / 10;
i--;
}
return result;
}
public static void main(String[] args) {
new B("金额的中文大写形式");
}
}