2013-02-22 78 views
0

我正在开发虚拟键盘模块。点击呼叫对象并等待处理

KeyBoardModule.java

KeyBoardModule kbm = new KeyBoardModule("small", false, null); 

被称为在其他jForm(MainFrame.java)当是文本框 clickevent然后我得到新JFrame与键盘(它像弹出窗口), 当JButton按Enter键将其保存的数据变量textFieldValue from textarea of KeyBoardModule。 比frame.disponse()

主类调用MainFrame和点击键盘主机呼叫,我需要从键盘到主机返回的值..

没有使用大型机的ActionListener(用于输入按钮)

+0

你可以发布你的代码吗? – 2013-02-22 17:32:57

回答

1

要直接从GUI1向另一个GUI2返回值,GUI1必须具有对GUI2对象的引用。因此,无论何时想要将GUI1中的任何消息传递给GUI2,都可以通过调用GUI2的适当方法来完成。例如考虑下面给出的代码。虽然MainFrame创造InputBoard的对象,我们传递的MainFrameInputBoard's构造当前对象,以便InputBoard可以使用MainFrame适当public方法的输入传递给MainFrameGUI。这里MainFrame点击按钮打开InputBoard框架。并且每当一些输入被传递到JTextFieldInputBoard时,它被反映在的MainFrame中。

import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.JTextArea; 
import javax.swing.JScrollPane; 
import javax.swing.JButton; 
import javax.swing.SwingUtilities; 
import java.awt.FlowLayout; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.event.DocumentEvent; 

class MainFrame extends JFrame implements ActionListener 
{ 
    private JButton button; 
    private JTextArea tArea; 
    private InputBoard inBoard; 
    public void prepareAndShowGUI() 
    { 
     setTitle("Main Frame"); 
     tArea = new JTextArea(10,30); 
     button = new JButton("Click Me"); 
     inBoard = new InputBoard(this); 
     inBoard.prepareGUI(); 
     JScrollPane tFieldPane = new JScrollPane(tArea); 
     tArea.setLineWrap(true); 
     tArea.setWrapStyleWord(true); 
     tArea.setEditable(false); 
     button.addActionListener(this); 
     getContentPane().add(tFieldPane); 
     getContentPane().add(button,BorderLayout.SOUTH); 
     pack(); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     button.requestFocus(); 
    } 
    @Override 
    public void actionPerformed(ActionEvent evt) 
    { 
     if (!inBoard.isVisible()) 
     { 
      inBoard.setVisible(true); 
     } 
     inBoard.toFront(); 
    } 
    public void setText(final String s) 
    { 
     tArea.setText(s); 
    } 
    public static void main(String[] st) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       MainFrame mf = new MainFrame(); 
       mf.prepareAndShowGUI(); 
      } 
     }); 
    } 
} 
class InputBoard extends JFrame implements DocumentListener 
{ 
    MainFrame mainFrame ; 
    JTextField inField; 
    public InputBoard(MainFrame mainFrame) 
    { 
     this.mainFrame = mainFrame; 
    } 
    public void prepareGUI() 
    { 
     setTitle("Input Board"); 
     inField = new JTextField(40); 
     getContentPane().setLayout(new FlowLayout()); 
     getContentPane().add(inField); 
     inField.getDocument().addDocumentListener(this); 
     setLocationRelativeTo(mainFrame); 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     pack(); 
    } 
    @Override 
    public void changedUpdate(DocumentEvent evt) 
    { 
     mainFrame.setText(inField.getText()); 
    } 
    @Override 
    public void insertUpdate(DocumentEvent evt) 
    { 
     mainFrame.setText(inField.getText()); 
    } 
    @Override 
    public void removeUpdate(DocumentEvent evt) 
    { 
     mainFrame.setText(inField.getText()); 
    } 
} 
+0

感谢您的解释,这非常有帮助。 : - )...你帮了很多,现在我知道更多:-) – 2013-02-23 12:53:27

+0

不客气。 :)。我很高兴它帮助你了解更多.. – 2013-02-23 17:00:10