2014-11-23 34 views
0

我是java中的bigginer。 我試圖在jframe1和jframe2中創建一個文本框的按鈕。 所以我想點擊它時在按鈕上的按鈕在jframe2的文本框中編寫一個文本 請幫助我,謝謝。 我traied這樣的:如何使用java訪問其他jframe中的文本框?

String adress = jTextField1.getText(); //in the first jframe1 
+0

向我們展示你如何試圖做到這一點。發佈您的代碼。 – 2014-11-23 20:17:43

+0

完成。上面的代碼 – Ahmed 2014-11-23 20:20:09

+0

您可能需要在您當前的Frame中有其他Frame的引用或其他任何... – nbro 2014-11-23 20:20:12

回答

0

下面是一個例子(如果我理解正確你的問題):

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Intro { 

    static int count = 0; 

    public static void main(String[] args) { 

     try { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException 
       | UnsupportedLookAndFeelException e) { 
      e.printStackTrace(); 
     } 

     final JFrame frame1 = new JFrame("frame 1"); 
     final JFrame frame2 = new JFrame("frame 2"); 
     final JButton button = new JButton("Button"); 
     final JTextField textFieald = new JTextField("0"); 

     button.setPreferredSize(new Dimension(100, 40)); 
     button.setFocusable(false); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       textFieald.setText(Integer.toString(++count)); 
      } 
     }); 

     frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame1.getContentPane().add(button); 
     frame1.pack(); 
     frame1.setLocationRelativeTo(null); 
     frame1.setVisible(true); 

     frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame2.getContentPane().add(textFieald); 
     frame2.pack(); 
     frame2.setLocationRelativeTo(null); 
     frame2.setVisible(true); 

    } 
} 
相關問題