2013-12-08 7 views
-1

아주 많은 텍스트 필드가있는 애플리케이션을 만들었습니다. 내가 응용 프로그램을 실행할 때, 응용 프로그램은 abolutely 잘 작동합니다. 그런 다음 메뉴를 만들었습니다.이 메뉴는 응용 프로그램을 열거 나 FileWriter가 작성한 txt를 열 수 있습니다. 이제 메뉴를 사용하여 응용 프로그램을 실행하면 첫 번째 텍스트 필드에 초점을 맞 춥니 다. 첫 번째 textfield에 초점을 두지 않고 메뉴를 응용 프로그램에서 실행하게하려면 어떻게해야합니까 ?? 내가 말했듯이, 내가 메뉴없이 응용 프로그램을 실행할 때, 초점이 .. 희망 u는 도울 수있다! 최고 감사합니다.Java JTextfield 포커스

메뉴 :

public class menu extends JFrame{ 

    private JFrame fr; 
    private JButton b1; 
    private JButton b2; 

    private JPanel pa; 

    public static void main(String[] args){ 
     new menu(); 

    } 

    public menu(){ 
     gui(); 
    } 
    public void gui(){ 

     fr = new JFrame("menu"); 
     fr.setVisible(true); 
     fr.setSize(200,63); 
     fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     fr.setLocationRelativeTo(null); 

     pa = new JPanel(new GridLayout(1,2)); 
     b1 = new JButton("Infostore"); 
     b2 = new JButton("log"); 

     pa.add(b1); 
     pa.add(b2); 
     fr.add(pa); 

     Eventhandler eh = new Eventhandler(); 
     b1.addActionListener(eh); 
     b2.addActionListener(eh); 
    } 
    private class Eventhandler implements ActionListener{ 

     public void actionPerformed(ActionEvent event){ 


      if(event.getSource()==b1){ 
       IS is = new IS(); 
       fr.setVisible(false); 
      } 

      if(event.getSource()==b2){ 

       try{ 
        Runtime.getRuntime().exec("C:\\Windows\\System32\\notepad C:\\Applications\\Infostore.txt"); 
        }catch(IOException ex){ 
         ex.printStackTrace(); 
        } 
       System.exit(1); 

      } 


     } 

    } 
} 

응용 프로그램 : 당신은 초점을 얻을 수있는 텍스트 필드의 모든 부모를 강제해야한다

public class IS extends JFrame{ 

    private JLabel fname; 
    private JTextField name; 
    private JTextField lname; 
    private JLabel birth; 
    private JTextField date; 
    private JTextField month; 
    private JTextField year; 
    private JTextField age; 
    private JButton cont; 
    private JLabel lcon; 
    private JTextField email; 
    private JTextField numb; 

    String inumb; 

    int[] check = {0,0,0,0,0,0,0}; 
    int sum=0; 

    private JPanel p; 
    private JFrame f; 


    public static void main(String[] args){ 
     new IS(); 
       } 


    public IS(){ 
     gui(); 
    } 

    public void gui(){ 

     f = new JFrame("Infostore"); 
     f.setVisible(true); 
     f.setSize(200,340); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLocationRelativeTo(null); 

     p = new JPanel(); 
     fname = new JLabel("Name "); 
     name = new JTextField("Name",12); 
     lname = new JTextField("Last name",12); 
     birth = new JLabel("Birthday"); 
     date = new JTextField("Date",12); 
     month = new JTextField("Month",12); 
     year = new JTextField("Year",12); 
     age = new JTextField("Your age",12); 
     lcon = new JLabel("Contact"); 
     email = new JTextField("E-mail",12); 
     numb = new JTextField("Phone (optional)",12); 
     cont = new JButton("Store Information"); 

     p.add(fname); 
     p.add(name); 
     p.add(lname); 
     p.add(birth); 
     p.add(date); 
     p.add(month); 
     p.add(year); 
     p.add(age); 
     p.add(lcon); 
     p.add(email); 
     p.add(numb); 
     p.add(cont); 


     f.add(p); 


     f.setVisible(true); 

     name.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jname = name.getText(); 
       if(jname.equalsIgnoreCase("name")){ 
        name.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jname = name.getText(); 
       if(jname.equals("")){ 
        name.setText("Name"); 
        check[0] = 0; 
       }else{ 
        check[0] = 1; 
       } 
      }}); 

     lname.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jlname = lname.getText(); 
       if(jlname.equalsIgnoreCase("last name")){ 
       lname.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jlname = lname.getText(); 
       if(jlname.equals("")){ 
        lname.setText("Last name"); 
        check[1] = 0; 
       }else{ 
        check[1] = 1; 
       } 
       }}); 

     date.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jdate = date.getText(); 
       if(jdate.equalsIgnoreCase("date")){ 
        date.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jdate = date.getText(); 
       if(jdate.equals("")){ 
        date.setText("Date"); 
        check[2] = 0; 
       }else{ 
        check[2] = 1; 
       } 
       }}); 

     month.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jmonth = month.getText(); 
       if(jmonth.equalsIgnoreCase("month")){ 
        month.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jmonth = month.getText(); 
       if(jmonth.equals("")){ 
        month.setText("Month"); 
        check[3] = 0; 
       }else{ 
        check[3]=1; 
       } 
       }}); 

     year.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jyear = year.getText(); 
       if(jyear.equalsIgnoreCase("year")){ 
        year.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jyear = year.getText(); 
       if(jyear.equals("")){ 
        year.setText("Year"); 
        check[4] = 0; 
       }else{ 
        check[4] = 1; 
       } 
       }}); 

     age.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jage = age.getText(); 
       if(jage.equalsIgnoreCase("your age")){ 
        age.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jage = age.getText(); 
       if(jage.equals("")){ 
        age.setText("Your age"); 
        check[5] = 0; 
       }else{ 
        check[5] = 1; 
       } 
       }}); 

     email.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jemail = email.getText(); 
       if(jemail.equalsIgnoreCase("e-mail")){ 
        email.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jemail = email.getText(); 
       if(jemail.equals("")){ 
        email.setText("E-mail"); 
        check[6]=0; 
        }else{ 
         check[6]=1; 
        } 
       }}); 

     numb.addFocusListener(new FocusListener(){ 

      public void focusGained(FocusEvent e) {    
       String jnumb = numb.getText(); 
       if(jnumb.equalsIgnoreCase("phone (optional)")){ 
        numb.setText(""); 
       } 
      } 
      public void focusLost(FocusEvent e) { 
       String jnumb = numb.getText(); 
       if(jnumb.equals("")){ 
        numb.setText("Phone (optional)"); 
        }else{ 
        } 
       } 
      }); 



     eventh eh = new eventh(); 
     cont.addActionListener(eh); 
     } 


    private class eventh implements ActionListener{ 

     public void actionPerformed(ActionEvent event){ 

      sum = check[0] + check[1] + check[2] + check[3] + check[4] + check[5] + check[6]; 

      if(event.getSource()==cont&&sum==7){ 
       String sname = name.getText(); 
       String slname = lname.getText(); 
       String sdate = date.getText(); 
       String smonth = month.getText(); 
       String syear = year.getText(); 
       String sage = age.getText(); 
       String semail = email.getText(); 
       String snumb = numb.getText(); 

       if(snumb.equalsIgnoreCase("Phone (optional)")){ 
        numb.setText(""); 
       } 

       String inumb = ("Phone: "+numb.getText()); 

       String info = ("Name: "+sname+" "+slname+" "+"Birthday: "+sdate+"-"+smonth+"-"+syear+" "+" Age:"+sage+" "+"E-mail:"+" "+ semail+" "+inumb); 
       JOptionPane.showMessageDialog(null,info); 

       filewriter fw = new filewriter(); 
       fw.setName(info); 
       fw.writeFile(); 

       try{ 
        Thread.sleep(150); 
       }catch(InterruptedException ex){ 
        Thread.currentThread().interrupt(); 
       } 
       System.exit(0); 

        }else{ 
         JOptionPane.showMessageDialog(null,"Please fill out all fields"); 
        } 


      } 
    } 





    } 
+0

여기서'filewriter'는 무엇을 의미합니까? 그 기능이 내장되어 있습니까? 내 말은'FileWriter'가 아닌가? – vivek1794

답변

1

은, 우리가 텍스트 필드

Java Textfield focus

에 포커스를 설정할 수 있습니다
+0

이 문제가 해결되었습니다! 감사 :-)! –