2013-04-12 27 views
3

我對理解如何與來自另一個類的GUI進行通信存在問題。如何與來自另一個類的GUI進行通信

我們有一個類,whitch創建框架:

public class TestFrame extends javax.swing.JFrame { 

    javax.swing.JLabel label; 

    public TestFrame() { 
     initComponents(); 
     label=sampleLabel; 
    } 

    private void initComponents() { 

     sampleLabel = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
     getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); 

     sampleLabel.setText("anotherText"); 
     getContentPane().add(sampleLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 60, -1, -1)); 

     pack(); 
    } 

    private javax.swing.JLabel sampleLabel; 

} 

Main class instantiate CreateFrame and tryes to overwrite label text: 

public class Main { 

    public static void main(String[] args) { 

     TestFrame frame = new TestFrame(); 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       frame.setVisible(true); 
      } 
     }); 
     frame.label.setText("anotherText"); 
    } 
} 

當然,上面的代碼不起作用。但我在Java中新的,我需要以某種方式從另一個類訪問標籤(如圖例子......)

最新編輯:上面的代碼不工作:)希望幫助...

繼圖片顯示相同的框架。唯一改變的是JLabel文本。

Sample picture

請幫助。

+0

你想達到什麼目的?只是有一個創建JFrame的類而另一個類實例化它?創建JFrame的類必須有一個構造函數,它接受Strings for Labels,每次實例化它時,都可以給它一個不同的標籤。 –

+0

我也很好奇這將如何被有經驗的人回答。 – djangofan

+0

http://www.quickmeme.com/meme/3tvfu8/ – leppie

回答

2

如果您只是想通過訪問其setText()方法來更改標籤。所有你需要做的是以下(保持一切相同):

public void makeFrames() { 
     CreateFrame frame = new CreateFrame("Label1"); 
     frame.label.setText("new Label"); 
} 

下面是一個快速黑客看到標籤的動作變化:

public class Main { 

    JButton button; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Main object = new Main(); 
       object.makeFrames(); 
      } 
     }); 
    } 

    public void makeFrames() { 
     final CreateFrame frame = new CreateFrame("Label1"); 

     button = new JButton("Click"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       frame.label.setText("new Label"); 
      } 
     }); 
     frame.frame.add(BorderLayout.NORTH, button); 
    } 
} 

標籤更改當你點擊按鈕時會出現一個新的。

編輯2:(main()的所做的更改,按鈕聲明爲靜態的,因此它可以從內部的main()來訪問

public class Main { 

    static JButton button; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       final CreateFrame frameFromMain = new CreateFrame("Label1"); 

       button = new JButton("Click"); 
       button.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         frameFromMain.label.setText("new Label"); 
        } 
       }); 
       frameFromMain.frame.add(BorderLayout.NORTH, button); 
      } 
     }); 
    } 
} 

請記住,您訪問CreateFrame類的標籤,就像你訪問任何。一類的其他成員你可以直接訪問它,如果你聲明的變量靜態的,像這樣:

public class CreateFrame { 
    JFrame frame; 
    static JLabel label; 
    // the rest of the class remains the same 
} 

public class Main { 

    static JButton button; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       CreateFrame frameFromMain = new CreateFrame("Label1"); 

       button = new JButton("Click"); 
       button.addActionListener(new ActionListener() { 
        @Override 
        public void actionPerformed(ActionEvent e) { 
         CreateFrame.label.setText("new Label"); 
        } 
       }); 
       frameFromMain.frame.add(BorderLayout.NORTH, button); 
      } 
     }); 
    } 
} 

編輯3:

如果你不希望按鈕,刪除該代碼的按鈕,這樣做:

public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       CreateFrame frameFromMain = new CreateFrame("Label1"); 
       CreateFrame.label.setText("new Label"); // accessing label directly from main() 

      } 
     }); 
    } 

編輯4:使用OP代碼:

public class TestFrame extends javax.swing.JFrame { 

    javax.swing.JLabel label; 

    public TestFrame() { 
     initComponents(); 
     label=sampleLabel; 
    } 

    private void initComponents() { 

     sampleLabel = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     sampleLabel.setText("anotherText"); 
     add(sampleLabel); 
     pack(); 
    } 

    private javax.swing.JLabel sampleLabel; 
} 

然後你的主()看起來是這樣的:

public class Main { 
    public static void main(String[] args) { 

     final TestFrame frame = new TestFrame(); 

     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       frame.setVisible(true); 
      } 
     }); 
     frame.label.setText("Text From Main"); 
    } 
} 

我做了很少的改動。此代碼運行完美,並按照你的要求。我擺脫了你的「getContentPane」,因爲你不需要它在Java 6,7中。我也擺脫了你的佈局設置,因爲我自己並不熟悉它們。您需要學習導入Java類。

看起來你很早就學習Java。我建議你堅持使用命令行程序,直到找出Java如何工作,然後再轉向Swing。

+0

我看到了,我的英語可能是在非常低的水平:(非常感謝您的回答,但這不是我想要的,我會盡力讓它更清楚:問題是如何訪問JLabel實例.setText ()方法從不同的類/線程。這個不同的類是例如位於不同的包... – BigT

+0

@BigT「問題是如何從不同的類/線程訪問JLabel實例.setText()方法」。我試圖理解但仍然不能,讓我知道我是否正確,你想從JLabel類訪問setText()方法,並改變已經運行的框架上的標籤?或者你是否希望能夠使用標籤創建新框架你通過setText()方法給他們? –

+0

@BigT在我的答案中看到下面的文本「編輯:」,並知道如果這就是你想要的。 –

0

我相信有一種方法可以通過使用JSON http請求來完成。我發現a web article有一些想法,儘管您需要在GUI中實現http請求,並實現JSON服務器來爲請求響應提供服務。

+0

嗨。謝謝你的回答,但這不是我的意思。也許我錯誤地表達了自己。我只想在一個類中構建一個帶有一個標籤的簡單框架,並以某種方式訪問​​和更改來自不同類的標籤的文本(位於不同的包中,也許......)。 – BigT

相關問題