import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;import java.text.*;import java.io.*;
public class FindFrame extends JFrame
{
JRadioButton rb1=new JRadioButton("按标题查找");
JRadioButton rb2=new JRadioButton("按时间查找");
JRadioButton rb3=new JRadioButton("按日期查找");

JLabel lab1=new JLabel("按标题查找:");
JLabel lab2=new JLabel("按时间查找:");
    JLabel lab3=new JLabel("按日期查找:");
    
    JTextField  tf1=new JTextField();
    JTextField  tf2=new JTextField();
    JTextField  tf3=new JTextField(); 
    
    JButton  bt1=new JButton("确定");
    JButton  bt2=new JButton("取消 ");
    
    ButtonGroup group=new ButtonGroup();
    
  public FindFrame()
  {
   super();
   setSize(500,450);
Container con=getContentPane();
con.setSize(600,400);
rb1.setFont(new Font("宋体",Font.BOLD,15));
rb2.setFont(new Font("宋体",Font.BOLD,15));
rb3.setFont(new Font("宋体",Font.BOLD,15));

lab1.setFont(new Font("宋体",Font.BOLD,15));
lab2.setFont(new Font("宋体",Font.BOLD,15));
lab3.setFont(new Font("宋体",Font.BOLD,15));

rb1.setBounds(20,20,120,20);
    rb2.setBounds(200,20,120,20);
    rb3.setBounds(380,20,120,20);
    
    group.add(rb1);
    group.add(rb2);
    group.add(rb3);
    
    lab1.setBounds(20,80,120,20);
    lab2.setBounds(20,120,120,20);
    lab3.setBounds(20,160,120,20);
    
    tf1.setBounds(140,80,120,20);
    tf2.setBounds(140,120,120,20);
    tf3.setBounds(140,160,120,20);
    
    bt1.setBounds(120,200,90,20);
    bt2.setBounds(300,200,90,20);
    
    JLabel lab4=new JLabel("g");
    lab4.setBounds(290,390,0,0);
    
    
    
       
    con.add(rb1);
    con.add(rb2);
    con.add(rb3);
    con.add(lab1);
    con.add(tf1);
    con.add(lab2);
    con.add(tf2);
    con.add(lab3);
    con.add(tf3);
    con.add(bt1);
    con.add(bt2);
    con.add(lab4);
    
    ButtonHandler handler=new ButtonHandler();
    bt1.addActionListener(handler);
    
    bt2.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e)
     {
     System.exit(0);
     }
    });
    
    rb1.addActionListener(new ActionListener(){   
                public void actionPerformed(ActionEvent e)   
                {    tf1.setEnabled(true);
                     tf2.setEnabled(false); 
                     tf3.setEnabled(false); 
                                
                }   
                });   
                
        rb2.addActionListener(new ActionListener(){   
                public void actionPerformed(ActionEvent e)   
                {   
                     tf1.setEnabled(false); 
                     tf2.setEnabled(true);
                     tf3.setEnabled(false); 
                                
                }   
                }); 
                
         rb3.addActionListener(new ActionListener(){   
             public void actionPerformed(ActionEvent e)   
             {   
                 tf1.setEnabled(false); 
                 tf2.setEnabled(false); 
                 tf3.setEnabled(true);
                               
              }   
              });   
                  
    
    
  
  }
  public void rePaint()
  {
   tf1.setText(" ");
   tf2.setText(" ");
   tf3.setText(" ");
  }
  
  
  private class ButtonHandler implements ActionListener
  {
   public void actionPerformed(ActionEvent e)
        {
         ResultSet rs;
         String sql="";
         if(e.getSource()==bt1){
        
         try{
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                   String source="jdbc:odbc:xie";
                   Connection  con=DriverManager.getConnection(source);
                   Statement stmt=con.createStatement();
            
                   if(rb1.isSelected())
                   {
                  
                 sql="SELECT * FROM ji WHERE  lab LIKE "+tf1.getText();
            
                   }
                   else if(rb2.isSelected())
                   {
                 
            
                 sql="SELECT * FROM ji WHERE time LIKE "+tf2.getText();
            
                   }
                   else if(rb3.isSelected())
                   {
                  
            
                 sql="SELECT * FROM ji WHERE date LIKE "+tf3.getText();
                    }
            
                     rs=stmt.executeQuery(sql);
                     
                     MyFrame.riji.setText(rs.getString(3));
             }catch(Exception ew)
                  {
                   ew.printStackTrace();
                  }
                  }
        }
  }
  
  public static void main(String[] args)
{
FindFrame  findframe=new FindFrame();
findframe.setVisible(true);

findframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

}
若我想在这个frame中调用另外一个frame中的一个Textarea来显示数据库中的数据,该怎么办?

解决方案 »

  1.   

    原理一样,还是做监听,比如说你有一个button,那么button.addActionListener( new ActionListener(){
        public void actionPerformed( ActionEvent e )
        {
            Frame anotherFrame = new Frame();
            //put new string message into the frame
            anotherFrame.setVisible( true );
        }});       
      

  2.   

    我是想按下上述代码中的bt1按钮后,就在另一个frame的textarea中显示我要显示的内容,你刚说的那个我试了下,不行啊.
      

  3.   

    还是这段代码中,出现查询说"列名xxxx无效"这怎么改?
      

  4.   

    字段名没有错啊,你帮我看下是不是我的那个sql语句有错,我弄了个下午都没成功啊