2012-12-26 29 views
1

我想通過單擊(說)菜單項來顯示新窗口。新窗口將包含兩個TextFields和一個取消按鈕以及一個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) { 

      } 

     } 

和Cancel按鈕偵聽器彈出窗口

/* 
* 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在彈出的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); 
    } 

} 

大型機,其中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希望JavaFX中的示例代碼不在Swing中。 –