2017-05-30 47 views
0

請我需要你的幫助,即時嘗試做到這一點,我想現在3-4天,我不能。 我有2個類MainForm和Class2。 我在Class1中的方法內有一個JLablel,我想通過按下Class2中的按鈕來修改它。修改一個無效方法內的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) { 

        } 

我想多和永諾不工作,什麼我需要在該按鈕寫代碼即可獲得從時間盒(組合框)的selectedItem屬性 ,並把它到那個標籤?

回答

0

首先你需要在課堂上都 JComboBox & JLable對象的引用2.
簡單的方法來做到這一點是在MainForm代替initialize方法的本地成員各自的私有成員聲明&它們傳遞到構造或者使用setter方法。

考慮參考名稱分別是jComboBox & jLable

現在你可以用以下語法使用此語法Class2.this.jComboBox & Class2.this.jLable(JComboBox中& jLable是對象的引用)從匿名類的actionPerformed方法中引用它們。