2015-11-20 16 views
0

我想知道,如果有可能,每JObjects保存在一個類和another.Something使用這樣的:保存JObjects在一個類和另一個使用(初級)

這是mainc類:

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

    public class Main extends JFrame implements ActionListener { 

     public static void main(String[] args){ 
      new Main(); 
     } 
     public Main(){ 
      field = new JTextField(12); 
      button = new JButton("Click"); 
      add(field); 
      add(button) 


    setTitle("main"); 
      getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      setSize(920, 420); 
      setLocation(100, 100); 
      setVisible(true); 
     } 

     @Override 
     public void actionPerformed(ActionEvent ae) { 
      throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
     } 
    } 

和第二類另存:

// all imports 
    public class Save{ 
     public JTextField field; 
     public JButton button; 
} 

回答

0

當然這是可能的。

... 
    public Main(){ 
     Save saveObject = new Save(); 
     saveObject.field = new JTextField(12); 
     saveObject.button = new JButton("Click"); 
     add(saveObject.field); 
     add(saveObject.button); 
     this.save = saveObject;//assuming you have a class field of type Save named "save" 
... 

FYI

你不應該允許直接保存類的字段進行修改。 OOP中的通用慣例是使用getter和setter方法。

+0

明白了很多 –

相關問題