- - PR -
swing 別画面からの内容変更
1
| 投稿者 | 投稿内容 | ||||
|---|---|---|---|---|---|
|
投稿日時: 2004-02-04 13:06
サラリーマンです。よろしくおねがいします。
画面Aから画面Bのラベルを変更したいのですが、 うまくいきません・・・。 画面Bが起動していなければ起動させ、起動中なら 起動中の画面Bに対して変更を反映させたいのです。 どなたか教え頂けないでしょうか? よろしくお願いします。 以下サンプルソース /** * 画面Aクラス */ public class TestFrame_A extends JFrame { JPanel panel = new JPanel(); JButton button = new JButton(); public TestFrame_A() throws HeadlessException { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setTitle("ラベル変更テスト"); this.setDefaultCloseOperation(HIDE_ON_CLOSE); this.setState(Frame.NORMAL); //パネルサイズ指定 panel.setBounds(new Rectangle( 0, 0, 500, 85)); panel.setLayout(null); //ボタン詳細設定 button.setBounds(new Rectangle(15, 53, 60, 25)); button.setBorder(null); button.setVerifyInputWhenFocusTarget(true); button.setText("変更"); button.addActionListener(this); panel.add(button, null); this.getContentPane().add(panel, null); } void actionPerformed(ActionEvent e) { TestFrame_B frame = new TestFrame_B(); frame.setLabelText("変更成功"); } } /** * 画面Bクラス */ public class TestFrame_B extends JFrame { JPanel panel = new JPanel(); JLabel label = new JLabel(); public TestFrame_B() throws HeadlessException { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { this.getContentPane().setLayout(null); this.setTitle("受け取りテスト"); this.setDefaultCloseOperation(HIDE_ON_CLOSE); this.setState(Frame.NORMAL); //パネルサイズ指定 panel.setBounds(new Rectangle( 0, 0, 500, 85)); panel.setLayout(null); //ボタン詳細設定 label.setBounds(new Rectangle(15, 53, 60, 25)); label.setBorder(null); label.setText("テキスト"); panel.add(label, null); this.getContentPane().add(panel, null); } public void setLabelText(String str) { label.setText(str); } } | ||||
|
投稿日時: 2004-02-04 13:25
ボタン押下ごとにTestFrame_Bのインスタンスを生成しなおしていらっしゃるようですが、これでは望みの動作は得られません。これでは、単に画面に表示しているTestFrame_Bとは"別の" (表示されていない)TestFrame_Bをこの瞬間に作成し、そこのラベルの文字列を変更していることになります。 TestFrame_Aに、"すでに表示されているTestFrame_B"への参照をあらかじめ持たせておいて、actionPerformed()では、その参照先にぶら下がっているTestFrame_Bが所有するラベルにsetLabelText()しにいってください。 | ||||
|
投稿日時: 2004-02-04 13:47
シュンさん、早速のお返事ありがとうございます。
勉強不足で申し訳ございません。 引用: -------------------------------------------------------------------------------- TestFrame_Aに、"すでに表示されているTestFrame_B"への参照 -------------------------------------------------------------------------------- についてですが、具体的にどのようにすればよいのでしょうか? TestFrame_Aに private JFrame frame = new TestFrame_B(); でよいのでしょうか? 本当に申し訳ございませんが、よろしくお願いします。 | ||||
|
投稿日時: 2004-02-04 15:11
そうですね。
それから、ActionPerformed()メソッド内で if(!frame.isVisible()) frame.setVisible(true); else frame.setLabelText("XXX"); のような処理を記述すればOKではないでしょうか。 | ||||
|
投稿日時: 2004-02-04 16:14
シュンさん、ありがとうございます!!
内容変更の処理が出来ました。 本当にありがとうございました!! | ||||
1
