2012-12-26 1 views
1

메뉴 항목을 클릭하여 (말하기) 새 창을 표시하고 싶습니다. 새 창에는 두 개의 TextField와 한 개의 취소 버튼과 하나의 Ok 버튼이 있습니다. 사용자가 텍스트 필드에 데이터를 입력하고 Ok를 누르면 부모 창에서 사용자가 지정한 값을 받아야합니다.javafx2의 새 창에서 사용자 데이터를받는 방법

어떻게하면됩니까? 예제 코드를 보여주십시오. 감사

여기

답변

-1

이 솔루션입니다, 당신은 다음 새 창을 클릭하여 창 메뉴를 클릭하고 새 창이 두 개의 텍스트 필드 및 확인 열립니다 및 취소 버튼, 내가 먼저 텍스트 필드에서 텍스트 값을 얻을 부모 창에 표시됩니다. 질문이 있으면 알려주세요. MVC와 함께 싱글 톤 디자인 패턴을 사용했습니다. 코드를 실행할 질문이 있으면 알려주십시오.

/* 

     * To change this template, choose Tools | Templates 
     * and open the template in the editor. 
     */ 
     package stackoverflow; 

     import java.awt.event.MouseEvent; 
     import java.awt.event.MouseListener; 
     import javax.swing.JFrame; 

     /** 
     * 
     * @author Burak Firik 
     */ 
     public class ButtonListener implements MouseListener{ 

      String value; 

      public ButtonListener(String str){ 
       value=str; 
      } 

      @Override 
      public void mouseClicked(MouseEvent e) { 

       MainSingleton singleton=MainSingleton.getMainSingleton(); 
       InputFrame inputFrame=singleton.getinputGUI(); 
       MainFrame mainGUI=singleton.getGUI(); 
       mainGUI.setLabelValue(inputFrame.getTextField1().getText()); 
       mainGUI.repaint(); 


      } 

      @Override 
      public void mousePressed(MouseEvent e) { 
       MainSingleton singleton=MainSingleton.getMainSingleton(); 
       InputFrame inputFrame=singleton.getinputGUI(); 
       inputFrame.setVisible(false); 
      } 

      @Override 
      public void mouseReleased(MouseEvent e) { 

      } 

      @Override 
      public void mouseEntered(MouseEvent e) { 

      } 

      @Override 
      public void mouseExited(MouseEvent e) { 

      } 

     } 

그리고 팝업 JFrame의 확인을 위해 두 개의 텍스트 필드와 두 개의 버튼을 표시하고

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackoverflow; 

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

/** 
* 
* @author Burak Firik 
*/ 
public class InputFrame extends JFrame { 

    JTextField txt1; 
    JTextField txt2; 
    JButton  btnOk; 
    JButton  btnCancel; 
    public InputFrame(){ 
     initGUI(); 
     initHandlers(); 
    } 

    void initGUI(){ 
     this.setLayout(new BorderLayout()); 
     this.setSize(300,300); 
     this.setLocation(200,200); 
     txt1=new JTextField(10); 
     txt2=new JTextField(10); 
     btnOk=new JButton("OK"); 
     btnCancel=new JButton("Cancel"); 

     JPanel northPanel=new JPanel(); 
     northPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     northPanel.add(txt1); 
     northPanel.add(txt2); 

     JPanel centerPanel=new JPanel(); 
     centerPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     centerPanel.add(btnOk); 
     centerPanel.add(btnCancel); 

     add(northPanel,BorderLayout.NORTH); 
     add(centerPanel, BorderLayout.CENTER); 

    } 
    void initHandlers(){ 
     ButtonListener btnListern=new ButtonListener(txt1.getText()); 
     btnOk.addMouseListener(btnListern); 
     CancelButtonListener btnCancelListen=new CancelButtonListener(); 
     btnCancel.addActionListener(btnCancelListen); 
    } 

    JFrame getInputFrame(){ 
     return this; 
    } 

    JTextField getTextField1(){ 
     return this.txt1; 
    } 
    JTextField getTextField2(){ 
     return this.txt2; 
    } 

    public static void main(){ 
     InputFrame frame=new InputFrame(); 
     frame.setVisible(false); 
    } 

} 

메인 프레임 취소됩니다 팝업 창

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackoverflow; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

/** 
* 
* @author Burak Firik 
*/ 
public class CancelButtonListener implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     MainSingleton singleton=MainSingleton.getMainSingleton(); 
     InputFrame inputFrame=singleton.getinputGUI(); 
     inputFrame.setVisible(false); 
    } 

} 

그리고 inputFrame을위한 버튼 리스너를 취소 곳 newWindow ManuItem이 있습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackoverflow; 

import java.awt.BorderLayout; 
import javax.swing.ImageIcon; 
import javax.swing.JCheckBoxMenuItem; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JRadioButtonMenuItem; 

/** 
* 
* @author Burak Firik 
*/ 
public class MainFrame extends JFrame{ 

    private JMenuBar menuBar; 
    private JMenu  menuWindow; 
    private JMenuItem menunewWindow; 

    private  JLabel   labelInputValue; 


    public MainFrame(){ 
     this.setVisible(true); 
     this.setSize(300,300); 
     initGUI(); 
     initHandlers(); 
    } 

    private void initGUI() { 
       // Create the menu bar 
       this.setLayout(new BorderLayout()); 
     menuBar = new JMenuBar(); 
       menuWindow = new JMenu("Window"); 
     menuWindow.setMnemonic('N'); 
     menuBar.add(menuWindow); 


       menunewWindow = CreateMenuItem(menuWindow, ITEM_PLAIN, 
           "New Window", null, 'N', null); 
       add(menuBar, BorderLayout.NORTH); 

       labelInputValue=new JLabel("SELAM"); 
       add(labelInputValue, BorderLayout.CENTER); 

    } 

    public JMenuItem CreateMenuItem(JMenu menu, int iType, String sText, 
           ImageIcon image, int acceleratorKey, 
           String sToolTip) 
    { 
     // Create the item 
     JMenuItem menuItem; 

     switch(iType) 
     { 
      case ITEM_RADIO: 
       menuItem = new JRadioButtonMenuItem(); 
       break; 

      case ITEM_CHECK: 
       menuItem = new JCheckBoxMenuItem(); 
       break; 

      default: 
       menuItem = new JMenuItem(); 
       break; 
     } 

     // Add the item test 
     menuItem.setText(sText); 

     // Add the optional icon 
     if(image != null) 
      menuItem.setIcon(image); 

     // Add the accelerator key 
     if(acceleratorKey > 0) 
      menuItem.setMnemonic(acceleratorKey); 

     // Add the optional tool tip text 
     if(sToolTip != null) 
      menuItem.setToolTipText(sToolTip); 

     // Add an action handler to this menu item 


     menu.add(menuItem); 

     return menuItem; 
    } 

    private final int ITEM_PLAIN = 0; // Item types 
    private final int ITEM_CHECK = 1; 
    private final int ITEM_RADIO = 2; 

    private void initHandlers() { 
     MenuListener menuListen=new MenuListener(); 
     menunewWindow.addActionListener(menuListen); 


    } 

    public void setLabelValue(String str){ 
     labelInputValue.setText(str); 
     this.repaint(); 

    } 

} 

이 클래스는 단일 개체가 당신이 리스너가 호출됩니다 새 창을 클릭하면이 메뉴 리스너입니다)

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackoverflow; 

/** 
* 
* @author Burak Firik 
*/ 
public class MainSingleton { 

    private static MainSingleton singleton=null; 

    private MainFrame gui; 

    private InputFrame inputGUI; 

     //private constructor for singleton design pattern 
    private MainSingleton(){} 



    public static MainSingleton getMainSingleton() 
    { 
     // ONLY CONSTRUCT THE SINGLETON THE FIRST TIME 
     if (singleton == null) 
     { 
      singleton = new MainSingleton(); 
     } 

     // GET THE SINGLETON NO MATTER WHAT 
     return singleton; 
    } 

    public static void main(String[] args) { 

     MainSingleton mainFrame=MainSingleton.getMainSingleton(); 
     mainFrame.init(); 


    } 

    public void init(){ 

     // INITALIZE THE GUI 
     gui = new MainFrame(); 

     inputGUI=new InputFrame(); 

    } 

    public void requestExit() 
    { 
     // WE MAY HAVE TO SAVE CURRENT WORK 
     boolean continueToExit = true; 


     // IF THE USER REALLY WANTS TO EXIT THE APP 
     if (continueToExit) 
     { 
      // EXIT THE APPLICATION 
      System.exit(0); 
     } 
    } 

    public MainFrame getGUI() { return gui; } 
    public InputFrame getinputGUI() { return inputGUI; } 

} 

: 게으른 프로그래머를위한 매우 빠른 포함되어 있습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package stackoverflow; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.Action; 

/** 
* 
* @author Burak Firik 
*/ 
public class MenuListener implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     MainSingleton singleton=MainSingleton.getMainSingleton(); 
     InputFrame inputFrame=singleton.getinputGUI(); 
     inputFrame.setVisible(true); 
    } 

} 
+0

OP는 Swing이 아닌 JavaFX에서 예제 코드를 원합니다. –