java cac xu ly

Xử Lý Thoát

class XuLyThoat implements ActionListener

    {

        public void actionPerformed(ActionEvent e){

            System.exit(0);

        }

Xử Lý Lưu

class XuLyLuu implements ActionListener{

public void actionPerformed(ActionEvent e){

JFileChooser fchooser = new JFileChooser();

if ( fchooser.showSaveDialog(null) == JFileChooser.CANCEL_OPTION){

return; // khong lam gi ca thoat khoi ham;

}

try{

File selectedFile = fchooser.getSelectedFile();

PrintWriter pw = new PrintWriter(selectedFile);

for(int i = 0; i < dlDuLieu.getSize(); i++){

String tmp = (String) dlDuLieu.getElementAt(i);

pw.println(tmp);

}

pw.close();

}

catch(FileNotFoundException fnfe){}

}

}

 class XuLySelect implements ListSelectionListener

    {

        public void valueChanged(ListSelectionEvent lse) {

            tfHoTen.setText((String)lstDanhSach.getSelectedValue());

        }

    }

   class XuLyHoTen_ENTER implements ActionListener

        {

            @Override

            public void actionPerformed(ActionEvent ae) {

                dlm.addElement(tfHoTen.getText());

                tfHoTen.selectAll();

            }

        }

package luyentapthi;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

import javax.swing.event.*;

import java.io.FileInputStream;

public class LuyenTapThi extends  JFrame{

    JLabel lbBang=new JLabel(" Luyen Tap Thi JaVa ^^!");

    JLabel lbMaHS=new JLabel("Ma Hoc Sinh");

    JLabel lbTenHS=new JLabel("Ten Hoc Sinh");

    JLabel lbPhai=new JLabel("Phai");

    DefaultComboBoxModel cbTim=new DefaultComboBoxModel();

    JComboBox lsTim=new JComboBox(cbTim);

    JTextField tfTen=new JTextField();

    JButton btTim=new JButton("Tim Kiem");

    JButton btThem=new JButton("Them");

    JButton btXoa=new JButton("Xoa");

    JButton btThoat=new JButton("Thoat");

    JRadioButton rdNam=new JRadioButton("Nam");

    JRadioButton rdNu=new JRadioButton("Nu");

    DefaultListModel dlLuoi = new DefaultListModel();

    JList lstLuoi = new JList(dlLuoi);

    public LuyenTapThi()

    {

        super("Luyen Tap Thi");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container c=this.getContentPane();

        c.setLayout(new GridBagLayout());

        GridBagConstraints gc=new GridBagConstraints();

        gc.gridx =0;

        gc.gridy=0;

        gc.gridwidth=3;

        c.add(lbBang,gc); 

        gc.gridwidth=1;

        gc.gridx=0;

        gc.gridy=1;

        //gc.weightx=3;

        c.add(lbMaHS,gc);

        //gc.weightx=1;

        gc.gridx=1;

        gc.gridy=1;

        gc.weightx=3;

        //gc.anchor=gc.WEST;

        gc.fill=GridBagConstraints.BOTH;

        c.add(lsTim,gc);

        gc.weightx=0;

        gc.gridx=0;

        gc.gridy=2;

        c.add(lbTenHS,gc);

        gc.gridx=1;

        gc.gridy=2;

        gc.weightx=3;

        gc.fill=GridBagConstraints.BOTH;

        c.add(tfTen,gc);

        gc.weightx=0;

        gc.gridx=0;

        gc.gridy=3;

        c.add(lbPhai,gc);

        //JPanel p3=new JPanel(new FlowLayout(FlowLayout.CENTER));

        gc.gridx=1;

        gc.gridy=3;

        c.add(rdNam,gc);

        gc.gridx=1;

        gc.gridy=4;

        c.add(rdNu,gc);

        ButtonGroup bg=new ButtonGroup();

        bg.add(rdNam);

        bg.add(rdNu);

        JPanel p1=new JPanel(new FlowLayout(FlowLayout.RIGHT));

        gc.gridx=2;

        gc.gridy=1;

        p1.add(btTim);

        c.add(p1,gc);

        gc.gridx=0;

    gc.gridy=5;

    gc.gridwidth=3;

    gc.weighty=2;

    JScrollPane sp=new JScrollPane(lstLuoi,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        c.add(sp,gc);   

        gc.gridwidth=1;

        gc.weighty=1;

        gc.gridx=1;

        gc.gridy=6;

        JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER));

        p2.add(btThem);

        p2.add(btXoa);

        p2.add(btThoat);

        c.add(p2,gc);

        btThoat.addActionListener(new XuLyThoat());

        setSize(500,300);

        setVisible(true);

    }

    class XuLyThoat implements ActionListener

    {

        public void actionPerformed(ActionEvent e){

            System.exit(0);

        }

    }

    public static void main(String[] args) {

        new LuyenTapThi();

    }

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: #long