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() に加えられた変更、ボタンは static と宣言されているため、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;
}
そして、 main() は次のようになります。
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");
}
}
最小限の変更を加えました。このコードは完全に実行され、要求どおりに実行されます。Java 6、7では必要ないため、「getContentPane」を削除しました。また、私自身が慣れていないため、レイアウト設定も削除しました。Java クラスをインポートする方法を学ぶ必要があります。
あなたは Java の学習の初期段階にいるようです。Swing に移る前に、Java がどのように機能するかを理解するまでは、コマンド行プログラムに固執することをお勧めします。