如果您只是想通過訪問其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。
你想達到什麼目的?只是有一個創建JFrame的類而另一個類實例化它?創建JFrame的類必須有一個構造函數,它接受Strings for Labels,每次實例化它時,都可以給它一個不同的標籤。 –
我也很好奇這將如何被有經驗的人回答。 – djangofan
http://www.quickmeme.com/meme/3tvfu8/ – leppie