dejvava
de thi so 6
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;
public class De6
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl_Title;
TextField tf1,tf2;
Button btn1 ,btn2;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
lbl1 = new Label("Name");
lbl2 = new Label("Password");
lbl_Title = new Label("Form test");
tf1 = new TextField(10);
tf2 = new TextField(10);
btn1 = new Button("Register");
btn2 = new Button("Cancel");
btn2.addActionListener(this);
btn1.addActionListener(this);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
addConstraints(lbl_Title,1,0);
addConstraints(lbl1,0,1);
addConstraints(tf1,1,1);
addConstraints(lbl2,0,2);
addConstraints(tf2,1,2);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,0,3);
addConstraints(btn2,1,3);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y)
{
gbc.gridx = x;
gbc.gridy = y;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn2)
{
System.exit(0);
}
if(e.getSource()== btn1)
{
File f = new File("Login.txt");
String us,pas;
us = "";
pas = "";
try
{
OutputStreamWriter isWriter=new OutputStreamWriter(new FileOutputStream(f),"utf-8");
BufferedWriter bfWriter = new BufferedWriter(isWriter);
bfWriter.write(tf1.getText());
bfWriter.newLine();
bfWriter.write(tf2.getText());
bfWriter.flush();
bfWriter.close();
}
catch(Exception ei)
{
JOptionPane.showMessageDialog(new Frame(),"Loi Khi Doc File");
}
}
}
}
de so 8
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class De8
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl_Title;
TextField tf1,tf2,tf3;
Button btn1 ,btn2;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
lbl1 = new Label("Name");
lbl2 = new Label("Password");
lbl_Title = new Label("Form test");
tf1 = new TextField(10);
tf2 = new TextField(10);
btn1 = new Button("Register");
btn1.addActionListener(this);
btn2 = new Button("Cancel");
btn2.addActionListener(this);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
addConstraints(lbl_Title,1,0);
addConstraints(lbl1,0,1);
addConstraints(tf1,1,1);
addConstraints(lbl2,0,2);
addConstraints(tf2,1,2);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,0,4);
addConstraints(btn2,1,4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y)
{
gbc.gridx = x;
gbc.gridy = y;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn2)
{
System.exit(0);
}
if(e.getSource() == btn1)
{
try{
File f = new File("user.dat");
FileOutputStream fOut = new FileOutputStream(f);
ObjectOutputStream obOut = new ObjectOutputStream(fOut);
User a = new User();
a.set(tf1.getText(),tf2.getText());
obOut.writeObject(a);
obOut.flush();
obOut.close();
}
catch(Exception er)
{
}
}
}
}
class User implements Serializable
{
String name,pass;
void set(String a , String b)
{
name = a;
pass = b;
}
String getName()
{
return name;
}
String getPass()
{
return pass;
}
}
de so 7
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class De7
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl3,lbl_Title;
TextField tf1,tf2,tf3;
Button btn1 ,btn2;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
lbl1 = new Label("Basic Salary");
lbl2 = new Label("Rate");
lbl3 = new Label("Salary");
lbl_Title = new Label("Form test");
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(10);
btn1 = new Button("Calculate");
btn1.addActionListener(this);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
addConstraints(lbl_Title,1,0);
addConstraints(lbl1,0,1);
addConstraints(tf1,1,1);
addConstraints(lbl2,0,2);
addConstraints(tf2,1,2);
addConstraints(lbl3,0,3);
addConstraints(tf3,1,3);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,1,4);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y)
{
gbc.gridx = x;
gbc.gridy = y;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
float bs,rt;
bs = Float.parseFloat(tf1.getText());
if(tf2.getText().equals(""))
{
Float a = new Float(this.calculate(bs));
tf3.setText(a.toString());
}
else
{
rt = Float.parseFloat(tf2.getText());
Float a2 = new Float(this.calculate(bs,rt));
tf3.setText(a2.toString());
}
}
float calculate(float basic)
{
float ft =(float)2.34;
return basic * ft;
}
float calculate(float basic,float rate)
{
return basic*rate;
}
}
--deso 5
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.io.*;
public class De5
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl_Title;
TextField tf1,tf2;
Button btn1 ,btn2;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
lbl1 = new Label("Name");
lbl2 = new Label("Password");
lbl_Title = new Label("Form test");
tf1 = new TextField(10);
tf2 = new TextField(10);
btn1 = new Button("Login");
btn2 = new Button("Cancel");
btn2.addActionListener(this);
btn1.addActionListener(this);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
addConstraints(lbl_Title,1,0);
addConstraints(lbl1,0,1);
addConstraints(tf1,1,1);
addConstraints(lbl2,0,2);
addConstraints(tf2,1,2);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,0,3);
addConstraints(btn2,1,3);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y)
{
gbc.gridx = x;
gbc.gridy = y;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn2)
{
System.exit(0);
}
if(e.getSource()== btn1)
{
File f = new File("Login.txt");
String us,pas;
us = "";
pas = "";
try
{
InputStreamReader isReader=new InputStreamReader(new FileInputStream(f),"utf-8");
BufferedReader bfReader = new BufferedReader(isReader);
String str ="";
int tg = 1;
boolean dung = false;
while ((str = bfReader.readLine()) != null)
{
if(tg == 1)
{
us = str;
tg = 0;
}
else
{
if(tg == 0)
{
pas = str;
tg = 1;
if(us.equals(tf1.getText()) && pas.equals(tf2.getText()))
{
dung = true;
break;
}
else
{
dung = false;
}
}
}
}
if(dung == true)
{
System.out.println("Login Successful");
JOptionPane.showMessageDialog(new Frame(),"Login Successful");
}
else
{
System.out.println("Login invaid");
JOptionPane.showMessageDialog(new Frame(),"Login invaid");
}
bfReader.close();
}
catch(Exception ei)
{
JOptionPane.showMessageDialog(new Frame(),"Loi Khi Doc File");
}
}
}
}
de so 3
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class De3
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl_Title,lbl3;
TextField tf1,tf2;
Button btn1 ,btn2;
Checkbox cb1,cb2;
CheckboxGroup cbg;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
cbg = new CheckboxGroup();
lbl2 = new Label("Address");
lbl1 = new Label("Name");
lbl_Title = new Label("Form test");
tf2 = new TextField(10);
tf1 = new TextField(10);
lbl3 = new Label("Sex");
btn1 = new Button("Regis");
btn2 = new Button("Cancel");
btn2.addActionListener(this);
btn1.addActionListener(this);
cb1= new Checkbox("Nam",cbg,false);
cb2 = new Checkbox("Nu",cbg,true);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
addConstraints(lbl_Title,1,0,1,1);
addConstraints(lbl1,0,1,1,1);
addConstraints(tf1,1,1,2,1);
addConstraints(lbl3,0,2,1,1);
addConstraints(cb1,1,2,1,1);
addConstraints(cb2,2,2,1,1);
addConstraints(lbl2,0,3,1,1);
addConstraints(tf2,1,3,2,1);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,1,4,1,1);
addConstraints(btn2,2,4,1,1);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y,int width,int height)
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = height;
gbc.gridwidth = width;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn2)
{
System.exit(0);
}
if(e.getSource()== btn1)
{
if(cb1.getState() == true)
{
System.out.println("Ten:"+tf1.getText() + " Gioi Tinh : Nam" + " Dia chi" +tf2.getText());
}
if(cb1.getState() == false)
{
System.out.println("Ten:"+tf1.getText() + " Gioi Tinh : Nu" + " Dia chi" +tf2.getText());
}
}
}
}
deso 2
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class De2
{
public static void main (String[] args)
{
new Form();
}
}
class Form extends Frame implements ActionListener
{
Label lbl1,lbl2,lbl_Title;
TextField tf1,tf2;
Button btn1 ,btn2;
GridBagConstraints gbc = new GridBagConstraints();
GridBagLayout gbl = new GridBagLayout();
Form()
{
setSize(300,300);
setLayout(gbl);
lbl2 = new Label("Destination");
lbl1 = new Label("Source");
lbl_Title = new Label("Form test");
tf2 = new TextField(10);
tf1 = new TextField(10);
btn2 = new Button("Cancel");
btn1 = new Button("Copy To");
btn2.addActionListener(this);
btn1.addActionListener(this);
gbc.insets = new Insets(2,2,2,2);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridheight = 1;
gbc.gridwidth = 1;
addConstraints(lbl_Title,1,0);
addConstraints(lbl1,0,1);
addConstraints(tf1,1,1);
addConstraints(lbl2,0,2);
addConstraints(tf2,1,2);
gbc.insets = new Insets(10,5,5,5);
addConstraints(btn1,0,3);
addConstraints(btn2,1,3);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
setVisible(false);
System.exit(0);
}
});
setVisible(true);
}
void addConstraints(Component cpn,int x,int y)
{
gbc.gridx = x;
gbc.gridy = y;
gbl.setConstraints(cpn,gbc);
add(cpn);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn2)
{
tf2.setText("");
tf1.setText("");
}
if(e.getSource()== btn1)
{
if(tf1.getText().equals(""))
{
JOptionPane.showMessageDialog(new Frame(),"Loi Khi nhap Source trong' ","Loi",JOptionPane.ERROR_MESSAGE);
}
else
{
tf2.setText(tf1.getText());
}
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top