ADD

来源:互联网 发布:知廉耻 懂敬畏 编辑:程序博客网 时间:2024/06/05 07:19
=================================
package Rong;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.Graphics;
public class CardLyouta extends JFrame{
    publicstatic CardLyouta def = null;
    privateJFrame jf;
    privateJPanel jp,jp1,jp2,jp3,jp4,jp5;
    privateCardLayout card,card1;
    privateJMenuBar jmb;
    privateJMenu jmu,jip;
    privateJTable datetable=null;
    privateJMenuItem open,on,exit,biaoge,wenben,qq,caipiao;
    privateAddDialog jaccount,jname,jsex,jphone,jpassword;
    privateJLabel benji,ip;
    privateJTextField jbenji,jjip;
    privateJButton huoqu;
    private int width = 0;
    private int height = 0;
    public voidinit(){
       jf=newJFrame();   
       card=newCardLayout();
       jmb=newJMenuBar();
       jmu=newJMenu("新建(N)");
       jmu.setMnemonic(KeyEvent.VK_N);
       jip=new JMenu("获取信息(H)");
       jip.setMnemonic(KeyEvent.VK_I);
       open=newJMenuItem("打开");
       on=newJMenuItem("图片");
       exit=newJMenuItem("退出");
       biaoge=newJMenuItem("表格");
       wenben=newJMenuItem("获取本机IP");
       qq=newJMenuItem("模拟QQ聊天");
       caipiao=newJMenuItem("彩票机选测试");
       jp=newJPanel();
      jp.setLayout(card);
       jp1=newJPanel();
      jp1.setSize(350,260);
       jp2=newJPanel();
      jp2.setOpaque(false);
       jp3=newJPanel();
       jp5=newJPanel();
       jp1.add(newJLabel(newImageIcon("2.gif")));   
       ImageIconimage1=new ImageIcon("1.png");
       jp2.add(newJLabel(image1));
       Object[]meishititle={"帐号","姓名","性别","电话","密码"};
       Object[][] meishiDate={
             newObject[]{2455443,"张速度","男","1569841525","156235ddf"},
             newObject[]{2455443,"王扩大","男","1435564622","432132232323f"},
             newObject[]{2455443,"张仙","女","1569841525","3426235ddf"},
             newObject[]{2455443,"李辅导","男","1569841525","dfs35ddf"},
             newObject[]{2455443,"孙月","女","1432432325","54g43dsf32"},
       };
       final DefaultTableModel modelForm = newDefaultTableModel(meishiDate,meishititle) //不可编辑
          {
           public boolean isCellEditable(int row,intcolumn)
           {
            return false;
           }
          };
         datetable=new JTable(modelForm);
         datetable.getTableHeader().setReorderingAllowed(false);    //关闭拖放 
         datetable.setSelectionBackground(new Color(51,123,176));
         datetable.setSelectionForeground(Color.white);
         datetable.setGridColor(Color.black);
         datetable.setPreferredScrollableViewportSize(new Dimension(755,447));    //设置表格大小
         datetable.getTableHeader().setResizingAllowed(true);
         datetable.setRowHeight(20);                                  //每行表格的高度
         datetable.setGridColor(new Color(10,116,88));
         datetable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);    //单选
          final JButton add = new JButton("添加(T)");
          add.setMnemonic('T');
          final JButton res = new JButton("修改(X)");
          res.setMnemonic('X');
         final  JButton modify = newJButton("删除(R)");
         modify.setMnemonic('R');
         final  JButton res1= new JButton("修改测试");
//          add.setMargin(new Insets(0, 0, 0,0));  
          ActionListener action1=new ActionListener(){
              public void actionPerformed(ActionEvent e){
                 if(e.getSource()==add){
                    new AddDialog().init();
              }
                 else if(e.getSource()==res){
                    String[]  Cells=newString[5];
                    for(inti=0;i<Cells.length;i++){
                       Cells[i]="新增"+modelForm.getRowCount()+""+i;
                    }
                    modelForm.addRow(Cells);
                 }
                 else if(e.getSource()==res1){
                      int i =datetable.getSelectedRow();  //获得行的索引
                     if(i==-1){                  
                        JOptionPane.showMessageDialog(null, "请选择要修改的条目!");
                      }
                      else{
                     modelForm.setValueAt(jaccount.getText(), i, 0);
                     modelForm.setValueAt(jname.getText(), i, 1);
                     modelForm.setValueAt(jsex.getSelectedText(), i, 2);
                     modelForm.setValueAt(jphone.getText(), i, 3);
                     modelForm.setValueAt(jpassword.getText(), i, 4);
                          
                 }
                 elseif(e.getSource()==modify){  
                    int[]selected=datetable.getSelectedRows();
                      int i =datetable.getSelectedRow();
                     if(i==-1){
                        JOptionPane.showMessageDialog(null, "请选择要删除的条目!");
                      }else{
                         for(intj=selected.length-1;j>=0;j--){
                        modelForm.removeRow(selected[j]);
                         }
                      }
                 }
              }
              };
              add.addActionListener(action1);
              res.addActionListener(action1);
              modify.addActionListener(action1);
              res1.addActionListener(action1);
          jp3. add(newJScrollPane(datetable));
          jp3.add(add,BorderLayout.EAST);
          jp3.add(res,BorderLayout.EAST);
         jp3.add(modify,BorderLayout.EAST);         
         jp3.add(res1,BorderLayout.EAST);   
          jp3.add(newJLabel());
         jp.add(jp1,"1");
         jp.add(jp2,"2");
         jp.add(jp3,"3");
         jp.add(jp5,"5");
         
          benji=newJLabel("本机信息:");
          ip=newJLabel("I P:");
         benji.setBounds(22, 55, 70, 25);
         ip.setBounds(22, 31, 70, 25);
         jbenji=new  JTextField(11);
         jjip=new  JTextField(11);
         jbenji.setBounds(71, 8, 255, 19);
         jjip.setBounds(71, 34, 255, 19);
          huoqu=newJButton("获取本机IP");
         jp5.add(benji);
         jp5.add(jbenji);
         jp5.add(ip);   
         jp5.add(jjip);
         jp5.add(huoqu);
         huoqu.addActionListener(new ActionListener(){
             public voidactionPerformed(ActionEvent e ){
                try{   
                     InetAddress i =InetAddress.getLocalHost();
                     jbenji.setText(i.getHostName()); 
                     jjip.setText(i.getHostAddress()); 
                     }
                     catch(Exceptionee){ee.printStackTrace();}
             }
          });
      add(jp);
      ActionListener action=new ActionListener(){
          public voidactionPerformed(ActionEvent e ){
            if(e.getSource()==open){
               card.show(jp,"1");   
             }
             elseif(e.getSource()==on){
               card.show(jp,"2");
             }
             elseif(e.getSource()==exit){
               System.exit(0);
             }
             elseif(e.getSource()==biaoge){
               card.show(jp, "3");
             }
             elseif(e.getSource()==wenben){
               card.show(jp,"5");
             }
         
              
       };
      ActionListener action3=new ActionListener(){
          public voidactionPerformed(ActionEvent e ){
            if(e.getSource()==qq){
             newQQ();
            setVisible(false);
          }
             elseif(e.getSource()==caipiao){
             newCardcaipiao();
            setVisible(false);
             }
          }
       };
      open.addActionListener(action);
      on.addActionListener(action);
      biaoge.addActionListener(action);
      exit.addActionListener(action);
      wenben.addActionListener(action);
      open.setAccelerator(KeyStroke.getKeyStroke('O',InputEvent.CTRL_MASK));
      on.setAccelerator(KeyStroke.getKeyStroke('T',InputEvent.CTRL_MASK));
      biaoge.setAccelerator(KeyStroke.getKeyStroke('B',InputEvent.CTRL_MASK));
      exit.setAccelerator(KeyStroke.getKeyStroke("F4"));
      wenben.setAccelerator(KeyStroke.getKeyStroke('W',InputEvent.CTRL_MASK));
      jmu.add(open);
      jmu.add(on);
      jmu.addSeparator();
      jmu.add(biaoge);
      jmu.addSeparator();
      jmu.add(exit);
      jmb.add(jmu);
      jip.add(wenben);
      qq.addActionListener(action3);
      jip.add(qq);
      caipiao.addActionListener(action3);
      jip.add(caipiao);
      jmb.add(jip);      
      setResizable(false);      
      setVisible(true);      
       Imageimage=new ImageIcon("2.png").getImage();
      setIconImage(image);      
      setTitle("CARDLAYOUt布局测试");   
      setJMenuBar(jmb);
      setSize(780,560);   
//        while (this.width < 780) {
//            width++;
//            height++;
//            this.setSize(width,height);
//            repaint();
//            try {
//            Thread.sleep(5);
//            } catch (InterruptedExceptione) {
//            e.printStackTrace();
//            }}
      setDefaultCloseOperation(EXIT_ON_CLOSE);   
      this.setLocationRelativeTo(null);
    }
    publicstatic void main(String args[]){
      
       newCardLyouta().init();
    }
    public voidaddRow(String[] arr) {
       // TODOAuto-generated methodstub    
    }
    public voidaddRow(String string) {
       // TODOAuto-generated method stub
      
    }
    publicString getRowCount() {
       // TODOAuto-generated method stub
       returnnull;
      
}
===================================================================
package Rong;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class AddDialog extends JDialog{
    publicCardLyouta jf;
    privateJLabel account,name,sex,phone,password;
    privateJTextField jaccount,jname,jsex,jphone,jpassword;
    privateJButton ok,res;
    privateJLabel fu,fu1,fu2;
    privateCardLyouta  modelForm;
    public voidinit(){
       JPanel north= new JPanel();
      north.setPreferredSize(new Dimension(0,10));
       JPanelcenter = new JPanel();
      center.setLayout(null);
       account=newJLabel("帐 号:");
       name=newJLabel("姓 名:");
       sex=newJLabel("性 别:");
       phone=newJLabel("电 话:");
       password=newJLabel("密 码:");
      account.setBounds(22, 5, 70, 25);
      name.setBounds(22, 31, 70, 25);
      sex.setBounds(22, 54, 70, 25);
      phone.setBounds(22, 77, 70, 25);
      password.setBounds(22, 100, 70, 25);
      jaccount=new  JTextField();
      jname=new  JTextField();
      jsex=new  JTextField();
      jphone=new  JTextField();
      jpassword=new  JTextField();
      jaccount.setBounds(71, 8, 255, 19);
      jname.setBounds(71, 34, 255, 19);
       String[]abc={"男","女"};
       JComboBoxjc=new JComboBox(abc);
      jc.setBounds(71, 57, 255, 19);
      jc.setSelectedIndex(1);
      jphone.setBounds(71,80, 255, 19);
      jpassword.setBounds(71,103, 255, 19);
       fu=newJLabel(" *");
      fu.setForeground(Color.red);
      fu.setBounds(333, 8, 22, 19);
       fu1=newJLabel(" *");
      fu1.setForeground(Color.red);
      fu1.setBounds(333, 80, 22, 19);
       fu2=newJLabel(" *");
      fu2.setForeground(Color.red);
      fu2.setBounds(333, 103, 22, 19);
       ok=newJButton("确定");
      ok.setBounds(99,140, 70, 24);
       res=newJButton("重置");
      res.setBounds(188,140, 70, 24);
      center.add(ok);
      center.add(res);
      center.add(account);
      center.add(name);
      center.add(sex);
      center.add(phone);
      center.add(password);
      center.add(jaccount);
      center.add(jname);
      center.add(jc);
      center.add(jphone);
      center.add(jpassword);
      center.add(fu);
      center.add(fu1);
      center.add(fu2);
      center.add(ok);
      center.add(res);
      setLayout(newBorderLayout());   
      add(north,BorderLayout.NORTH);
      add(center,BorderLayout.CENTER);      
       //事件处理
      ActionListener action=new ActionListener()
       {
          public voidactionPerformed(ActionEvent e ){
            if(e.getSource()==ok){
                String s=jaccount.getText().trim()+""+"-"+jname.getText().trim()+""+"-"+jsex.getSelectedText()+""+"-"+jphone.getText().trim()+""+"-"+jpassword.getText().trim()+"";
                Stringfen="\\-";
                Patternpat=Pattern.compile(fen);
                String[]str=pat.split(s);
                for(inti=0;i<str.length;i++){   
                  System.out.println(str);
                  modelForm.addRow(str);
                        
//                String str1=jaccount.getText();
//                String str2=jname.getText();
//                String str3=jsex.getSelectedText();
//                String str4=jphone.getText();
//                String str5=jpassword.getText();
//                String[] str = {str1, str2, str3, str4,str5};
//                String[] str1 = {"str1", "str2", "str3", "str4","str5"};
//                System.out.println(str1);             
             }
             elseif(e.getSource()==res){
               jaccount.setText(" ");
               jname.setText(" ");
               jphone.setText(" ");
               jpassword.setText(" ");
                System.out.println("重置测试通过");
             }
          }
       };
      res.addActionListener(action);
      ok.addActionListener(action);

      setModal(true);
      setSize(430   ,220);   
      setResizable(false);   
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      setVisible(true);
    }

    publicJTextField getJaccount() {
         return jaccount;
       }
    publicJTextField getJname() {
         return jname;
       }
    publicJTextField getJsex() {
         return jsex;
       }
    publicJTextField getJpjone() {
         return jphone;
       }
    publicJTextField getJpassword() {
         return jpassword;
       }
    publicObject getText() {
       // TODOAuto-generated method stub
       returnnull;
    }
    public voidsetText(String string) {
       // TODOAuto-generated method stub
      
    }

    publicObject getSelectedText() {
       // TODOAuto-generated method stub
       returnnull;
    }


   
}



原创粉丝点击