java IP地址,端口校验+下拉菜单选框监听事件+模拟QQ发送按钮旁的小箭头功能

package com.frame;

import java.awt.BorderLayout;
import javax.swing.text.AttributeSet;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import java.text.ParseException;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.InputVerifier;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
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.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultFormatter;
import javax.swing.text.DocumentFilter;

//import org.jvnet.substance.skin.SubstanceOfficeSilver2007LookAndFeel;

import com.frame.TextVerifier.PortVerifier;

public class Opertionbak extends JFrame implements ItemListener{
 private String [] textArray;
 private JPanel panel; 
 private JPanel panel1; //放置 登陆模块
 private JMenuItem menuItem_oracle,menuItem_mysql; //添加菜单选项为oracle;
 
 private JComboBox combox_jdbc;  
 private JTextField text_user,text_pass,text_database;
 private String  s="jdbc:mysql://localhost:3306";
 private String sb="jdbc:oracle:thin:@";
 private String s1,s2,s3,s4,s5;
 private JButton button_user;        

 private JLabel label_welcome,label_ip,label_database,label_pass;
 private JTextArea text_url; //显示url连接方式
 private JLabel label_tips,label_tips2,label_tips3;  //提示错误
 private JPopupMenu menu;
 private JFormattedTextField text_ip = null;
 private JFormattedTextField text_port = null;
 
 
 public Opertionbak(){
  
    
     menu= new JPopupMenu();
   text_user= new JTextField();
   textArray = new String[]{"数据库名字1","数据库名字2","数据库名字3"};
   for(String s : textArray){
    JMenuItem item = new JMenuItem(s);
    
    item.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
     
                  JMenuItem item = (JMenuItem) e.getSource();
                  text_user.setText(item.getText());
                  label_tips.setVisible(false);
                  
      } });
    menu.add(item);
    
   }
      
  
  
     
     
   panel= new JPanel();
   panel.setLayout(null);
         panel.setBounds(0, 0, 500, 400);
         
         this.setTitle("增删改查功能实现");
         this.setBounds(100, 100, 500, 400);

         init();
         this.add(panel,BorderLayout.CENTER);
         this.setLocationRelativeTo(null);
         this.setResizable(false);
         this.setVisible(true);
         
         //为点击添加关闭事件
         this.addWindowListener(new java.awt.event.WindowAdapter() {
         public void windowClosing(java.awt.event.WindowEvent e){
          System.exit(0);
         } 
         });    
 }
 //返回URL 地址
 public URL getRes(String str){   
        return this.getClass().getClassLoader().getResource(str);   
   } 
 
  
 private void init(){
  panel1 = new JPanel();
  panel1.setLayout(null);
  JLabel label_jdbc = new JLabel("选择数据库类型:");
  label_jdbc.setFont(new Font("黑体",Font.PLAIN,15));
  combox_jdbc = new JComboBox();
 
    
  combox_jdbc.setEditable(false);
     
  combox_jdbc.addItem("Oracle数据库");
  combox_jdbc.addItem("MySql数据库");
  
  
  
  
  label_jdbc.setBounds(8, 12, 120, 30);
  combox_jdbc.setBounds(90, 40, 100, 25);
  
  JLabel label_tip = new JLabel();
  //label_tip.setIcon(new ImageIcon(getRes("com/pccw/image/tip.jpg")));
  
  //label_tip.setBounds(5, 170, 55, 45);
  
  
  JLabel label_bottom = new JLabel();
  //label_bottom.setIcon(new ImageIcon(getRes("com/pccw/image/back.jpg")));
  label_welcome = new JLabel("欢迎使用!!");
  label_bottom.setBounds(10, 245, 140, 100);
  label_welcome.setBounds(100, 230, 120, 30);
  
  JLabel label_user = new JLabel("用户名:");
  label_user.setBounds(10, 15, 50, 25);
 

    
  text_user.addFocusListener(new FocusAdapter(){
        
   public void focusLost(final FocusEvent e){
    if(text_user.getText().equals("")){
     label_tips.setVisible(true);
     label_tips.setText("用户没有填写");
    }else{
     //sb.append(sb+s1+s2);
     label_tips.setVisible(false);
     //text_url.setText(sb.toString());
     
    }
   }
  });
      
  
  text_user.setBounds(70, 15, 140, 25);
  button_user= new JButton("...");
 
  
  
  button_user.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
     if(e.getSource()==button_user){
      
      menu.show(button_user,0,button_user.getHeight());
   }
               else{
                JMenuItem item = (JMenuItem) e.getSource();
                text_user.setText(item.getText());
               }
               
     
    } 
   });
  button_user.setBounds(245, 15, 25, 25);
  
  
  JLabel label_pass = new JLabel("口  令:");
  text_pass = new JTextField();
  label_pass.setBounds(10, 55, 50, 25);
  text_pass.setBounds(70, 55, 140, 25);
  text_pass.addFocusListener(new FocusAdapter(){
   
   public void focusLost(final FocusEvent e){
    if(text_pass.getText().equals("")){
     label_tips2.setVisible(true);
     label_tips2.setText("密码没有填写");
    }
    else{
     label_tips2.setVisible(false);
    }
   }
  });
  
  
  label_tips=new JLabel();
  label_tips2=new JLabel();
  label_tips3=new JLabel();
     label_tips.setBounds(100, 40, 80, 12);
    label_tips2.setBounds(100, 80, 80, 12);
    label_tips3.setBounds(150, 105, 100, 12);
     
  JLabel label_database = new JLabel("数据库:");
  text_database = new JTextField();
  label_database.setBounds(10, 95, 50, 25);
  text_database.setBounds(70, 95, 70, 25);
  text_database.addFocusListener(new FocusAdapter(){
   
   public void focusLost(final FocusEvent e){
    if(text_database.getText().equals("")){
     label_tips3.setVisible(true);
     label_tips3.setText("数据库没有填写");
     
     
    }
    else{
     String database;
     label_tips3.setVisible(false);
     String s1=text_ip.getText().toString();
     String s2=text_port.getText().toString();
     String s3=text_database.getText().toString();
     database="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
     text_url.setText(database);
     
    }
   }
  });
  
  
  JLabel label_ip = new JLabel("I  P:");
  
  
    // IP地址文本域的校验
    
  class IPAddressVerifier extends InputVerifier {

    private boolean b = false;
    public boolean verify(JComponent component) {
     // TODO Auto-generated method stub
     JFormattedTextField field = (JFormattedTextField) component;
     b = field.isEditValid();
     System.out.println(b);
     if (b == false) {
      String opt = new String("确定");

      Object[] options = { "确定" };
      JOptionPane.showOptionDialog(null, "IP地址输入有误请重新输入", "警告",
        JOptionPane.DEFAULT_OPTION,
        JOptionPane.WARNING_MESSAGE, null, options, options[0]);
      field.setText(null);
     }
     return field.isEditValid();
    }

  

   }
  

  
   
    //IP地址校验
    
class IPAddressFormatter extends DefaultFormatter {

    public String valueToString(Object value) throws ParseException {
     if (!(value instanceof byte[])) {
      throw new ParseException("Not a byte[]", 0);
     }
     byte[] a = (byte[]) value;
     if (a.length != 4)
      throw new ParseException("Length!=4", 0);
     StringBuilder builder = new StringBuilder();
     for (int i = 0; i < 4; i++) {
      int b = a[i];
      if (b < 0)
       b += 256;
      builder.append(String.valueOf(b));
      if (i < 3)
       builder.append('.');
     }
     return builder.toString();
    }

    public Object stringToValue(String text) throws ParseException {
     StringTokenizer tokenizer = new StringTokenizer(text, ".");
     byte[] a = new byte[4];
     for (int i = 0; i < 4; i++) {
      int b = 0;
      if (!tokenizer.hasMoreElements()) {
       System.out.println("few");
       throw new ParseException("Too few bytes", 0);
      }
      try {
       b = Integer.parseInt(tokenizer.nextToken());
      } catch (NumberFormatException e) {
       throw new ParseException("Not an integer", 0);
      }
      if (b < 0 || b >= 256)
       throw new ParseException("Byte out of range", 0);
      a[i] = (byte) b;
     }
     if (tokenizer.hasMoreElements())
      throw new ParseException("Too many bytes", 0);
     return a;
    }
   }
     text_ip = new JFormattedTextField(new IPAddressFormatter());
     text_ip.setInputVerifier(new IPAddressVerifier());
     text_ip.setValue(new byte[] { (byte) 10, (byte) 87, 66, (byte) 30 });
     label_ip.setBounds(10, 135, 40, 25);
  text_ip.setBounds(55, 135, 100, 25);
  text_ip.addFocusListener(new FocusAdapter(){
   
   public void focusLost(final FocusEvent e){
    if(text_ip.getText().equals("")){
     label_tips3.setVisible(true);
     label_tips3.setText("IP没有填写");
     
     
    }
    else{
     String sbr;
     label_tips3.setVisible(false);
     
     String s1=text_ip.getText().toString();
     String s2=text_port.getText().toString();
     String s3=text_database.getText().toString();
     sbr="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
     
     
     
     text_url.setText(sbr);
     
    }
   }
  });
  
  
   //  端口校验
    
   class PortFilter extends DocumentFilter {
    public void replace(FilterBypass fb, int offset, int length,
       String string, AttributeSet attr) throws BadLocationException {
      System.out.println("replace");
      if (string != null) {
       StringBuilder builder = new StringBuilder(string);
       for (int i = builder.length() - 1; i >= 0; i--) {
        int cp = builder.codePointAt(i);
        if (!Character.isDigit(cp)) {
         builder.deleteCharAt(i);
         if (Character.isSupplementaryCodePoint(cp)) {
          i--;
          builder.deleteCharAt(i);
         }
        }
       }
       string = builder.toString();
      }
      super.replace(fb, offset, length, string, attr);
     }
    }
   class PortVerifier extends InputVerifier {

     private boolean b = false;

     @Override
     public boolean verify(JComponent component) {
      // TODO Auto-generated method stub
      JFormattedTextField field = (JFormattedTextField) component;
      b = field.isEditValid();
      if (b == true) {
       try {
        int n = Integer.parseInt(field.getText());
        if (n < 1024 || n > 65535) {
         Object[] options = { "确定" };
         JOptionPane.showOptionDialog(null,
           "端口必须在1024-65535之间,请重新输入!", "Warning",
           JOptionPane.DEFAULT_OPTION,
           JOptionPane.WARNING_MESSAGE, null, options,
           options[0]);
         field.setText("");
         b = false;
        }
       } catch (NumberFormatException e) {

        // 如果输入的字符串在转换为整数时超出整数范围由此异常处理
        Object[] options = { "确定" };
        JOptionPane.showOptionDialog(null,
          "端口必须在1024-65535之间,请重新输入!", "Warning",
          JOptionPane.DEFAULT_OPTION,
          JOptionPane.WARNING_MESSAGE, null, options,
          options[0]);
        field.setText("");
        b = false;
       }
      }

      return b;
     }
    }
   JLabel label_port = new JLabel("端 口:");
   label_port.setBounds(165, 135, 40, 25);
 
 
 
  text_port = new JFormattedTextField(new DefaultFormatter() {
    private DocumentFilter filter = new PortFilter();

    protected DocumentFilter getDocumentFilter() {
     return filter;
    }
   });
  text_port.setBounds(205, 135, 60, 25);
  text_port.setText("1521"); 
  text_port.setInputVerifier(new PortVerifier());
  text_port.setEditable(true);
  text_port.addFocusListener(new FocusAdapter(){
  
  public void focusLost(final FocusEvent e){
   String sbrt;
      String s1=text_ip.getText().toString();
   String s2=text_port.getText().toString();
   String s3=text_database.getText().toString();
   sbrt="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
   text_url.setText(sbrt);
  }
 });
 
  
  JLabel label_url1 = new JLabel("URL连接方式:");
  label_url1.setFont(new Font("宋体",Font.PLAIN,12));
  text_url = new JTextArea(sb);
  text_url.setLineWrap(true);
  JScrollPane scrollpane = new JScrollPane(text_url);
  label_url1.setBounds(20, 175, 140, 25);
  scrollpane.setBounds(100, 180, 165, 60);
  //为两个菜单项添加响应事件
  combox_jdbc.addItemListener(this);  
  
  //把用户名等添加到panel1面板中
  
  panel1.add(label_port);
  panel1.add(label_tips);
  panel1.add(label_tips2);
  panel1.add(label_tips3);
  panel1.add(label_url1);
  panel1.add(scrollpane);
  panel1.add(label_ip);
  panel1.add(text_ip);
  panel1.add(text_port);
  panel1.add(label_port);
  panel1.add(text_database);
  panel1.add(label_database);
  panel1.add(label_pass);
  panel1.add(text_pass);
  panel1.add(button_user);
  panel1.add(label_user);
  panel1.add(text_user);
  panel.add(menu);
  //panel.add(text_url);
  
  JButton button_tail=new JButton("设置");
  button_tail.setBounds(190, 300, 50, 20);
  JButton button_reset=new JButton("重置");
  button_reset.setBounds(250, 300, 50, 20);
  JButton button_cummit=new JButton("提交");
  button_cummit.setBounds(310, 300, 50, 20);
  
  //点击设置按钮跳转到详细设置页面
  button_tail.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    new Detail();
    
   }
      
     });
  
  //点击提交按钮事件
   button_cummit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
     
    } 
   });
  
  //点击重置按钮数据项清空
  button_reset.addActionListener(new ActionListener(){

   public void actionPerformed(ActionEvent e) {
    
    label_welcome.setText("重置信息!!");
    text_user.setText("");
    text_pass.setText("");
   }
   
  });
  
  
  
  panel1.setBounds(200, 15, 280, 255);
  panel1.setBorder(BorderFactory.createEtchedBorder());
  panel1.setVisible(true);
  
 
 
  panel.add(button_tail);
  panel.add(button_reset);
  panel.add(button_cummit);
  panel.add(panel1);
  panel.add(label_welcome);
 
 
  panel.add(combox_jdbc);
  panel.setVisible(true);
 }
 
   public static void main(String[] args) {   
         JFrame.setDefaultLookAndFeelDecorated(true); 
         JDialog.setDefaultLookAndFeelDecorated(true);
         try {   
             UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());  
         } catch (UnsupportedLookAndFeelException e) {   
             e.printStackTrace();   
         }   
         
         new Opertionbak();
       
     }
  
 @Override
 public void itemStateChanged(ItemEvent e) {
  if(e.getStateChange()==ItemEvent.SELECTED){
   if(e.getItem().toString().equals("Oracle数据库")){
   text_port.setText("1521");
   text_url.setText(sb);
   
   //System.out.println("sssssssssssss");
   text_database.addFocusListener(new FocusAdapter(){
    
    public void focusLost(final FocusEvent e){
     if(text_database.getText().equals("")){
      label_tips3.setVisible(true);
      label_tips3.setText("数据库没有填写");
      
      
     }
     else{
      String database;
      label_tips3.setVisible(false);
      String s1=text_ip.getText().toString();
      String s2=text_port.getText().toString();
      String s3=text_database.getText().toString();
      database="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
      text_url.setText(database);
      
     }
    }
   });
   
   text_port.addFocusListener(new FocusAdapter(){
    
    public void focusLost(final FocusEvent e){
     String sbrt;
        String s1=text_ip.getText().toString();
     String s2=text_port.getText().toString();
     String s3=text_database.getText().toString();
     sbrt="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
     text_url.setText(sbrt);
    }
   });
    text_ip.addFocusListener(new FocusAdapter(){
     
     public void focusLost(final FocusEvent e){
      if(text_ip.getText().equals("")){
       label_tips3.setVisible(true);
       label_tips3.setText("IP没有填写");
       
       
      }
      else{
       String sbr;
       label_tips3.setVisible(false);
       
       String s1=text_ip.getText().toString();
       String s2=text_port.getText().toString();
       String s3=text_database.getText().toString();
       sbr="jdbc:oracle:thin:@"+s1+":"+s2+":"+s3;
       
       
       
       text_url.setText(sbr);
       
      }
     }
    });
   
   
   
   }
   else{
    text_url.setText(s);
       text_port.setText("3306");
       text_port.addFocusListener(new FocusAdapter(){
     
     public void focusLost(final FocusEvent e){
      String sbrt;
        
      String s2=text_port.getText().toString();
      
      sbrt="jdbc:mysql://localhost:"+s2;
      text_url.setText(sbrt);
     }
    });
        text_ip.addFocusListener(new FocusAdapter(){
     
     public void focusLost(final FocusEvent e){
      
     
       String sbr;
       label_tips3.setVisible(false);
       
       
       sbr="jdbc:mysql://localhost:3306";
       
       
       
       text_url.setText(sbr);
       
      
     }
    });
        text_database.addFocusListener(new FocusAdapter(){
      
      public void focusLost(final FocusEvent e){
       
       String sbrt;
          
       String s2=text_port.getText().toString();
       
       sbrt="jdbc:mysql://localhost:"+s2;
       text_url.setText(sbrt);
       
      }
     });
       
    
   }
  
  }
   
} 
}


   可怜图片不会插入。。。。
  直接放到类里面运行就可以了!~示例图搞不上来!!
 
   
 


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值