2017-05-30 4 views
0

私はあなたの助けを必要としています。私はそれをやろうとしています。 私は2つのクラスのMainFormとClass2を持っています。 Class1のメソッド内にJLablelがあり、Class2のボタンを押して変更したいと考えています。voidメソッド内のJLabelを変更します

public class MainForm { 


    private JFrame frame; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        MainForm window = new MainForm(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public MainForm() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(400, 200, 488, 322); 
     frame.setTitle("ShutDown"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 
     frame.setResizable(false); 


/** 
     * Time BOX 
     */ 
     JComboBox<String> timeBox = new JComboBox<String>(); 
     timeBox.setBounds(73, 142, 90, 20); 
     timeBox.addItem("Days"); 
     timeBox.addItem("Hours"); 
     timeBox.addItem("Minutes"); 
     timeBox.addItem("Seconds"); 
     timeBox.addItem("Right now"); 
     timeBox.setSelectedItem("Minutes"); 
     frame.getContentPane().add(timeBox); 


     String getTimeBox = timeBox.getSelectedItem().toString(); 



     /** 
     * The label info. 
     */ 
     JLabel labelInfo = new JLabel(""); 
     labelInfo.setBounds(73, 209, 310, 14); 
     frame.getContentPane().add(labelInfo); 
     labelInfo.setText(getTimeBox); 
} 

とクラス2

Class2 

JButton okButton = new JButton("OK"); 
     okButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

        } 

私は多くの試み、オールウェイズdoesntの仕事、私はタイムボックス(コンボボックス)からのSelectedItemを取得するために、そのボタンに記述する必要がいただきましたコード とそれを置きますそのラベルに?

答えて

0

あなたがそれを行うには2
簡単な方法はinitialize方法のMainFormの代わりに、地元のメンバーで、それぞれのプライベートメンバーは、コンストラクタにそれらを渡す&宣言することですクラスの両方 JComboBox & JLableオブジェクトの参照を必要とするすべての最初のまたはsetterメソッドのアプローチを使用します。

参照名がそれぞれjComboBox & jLableであるとします。

は今、あなたはこの構文Class2.this.jComboBox & Class2.this.jLable(JComboBoxの& jLableがあるオブジェクト参照)を使用して、匿名クラスのactionPerformed方法からそれらを参照するには、次の構文を使用することができます。

関連する問題