質問に答えるとするとこうでしょうか。(理解しやすいコードに落としてあるので,良い子は真似しない様に。)
以前にもコメントした通り,GUIアプリとしては余り良い設計ではないですね。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SampleModel {
private String search;
private String result;
public void setSearch(String value) { search=value; }
public String getSearch() { return search; }
public void setResult(String value) { result=value; }
public String getResult() { return result; }
}
abstract class AbstractSampleFrame extends JFrame {
private SampleModel model;
public AbstractSampleFrame(String title) { super(title); }
public void setModel(SampleModel value) { model=value; }
public SampleModel getModel() { return model; }
abstract void initializeComponents();
}
class SearchFrame extends AbstractSampleFrame implements ActionListener {
private JTextArea textSearch;
private JButton buttonSearch;
public SearchFrame() {
super("SampleFrame");
}
public void initializeComponents() {
Container contentPane;
textSearch = new JTextArea(getModel().getSearch(),3,30);
buttonSearch = new JButton("search");
buttonSearch.addActionListener(this);
contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add("Center",textSearch);
contentPane.add("South",buttonSearch);
this.pack();
}
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ResultFrame frame;
SampleModel model = getModel();
frame = new ResultFrame();
model.setSearch(textSearch.getText());
frame.setModel(model);
frame.initializeComponents();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(false);
frame.setVisible(true);
dispose();
}
});
}
}
class ResultFrame extends AbstractSampleFrame {
private JTextArea textSearch;
private JTextArea textResult;
private JButton buttonOutput;
public ResultFrame() {
super("ResultFrame");
}
public void initializeComponents() {
Container contentPane;
SampleModel model = getModel();
model.setResult("dummy result.");
textSearch = new JTextArea(model.getSearch(),3,30);
textSearch.setEnabled(false);
textResult = new JTextArea(model.getResult(),3,30);
textResult.setEnabled(false);
buttonOutput = new JButton("output");
buttonOutput.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
OutputFrame frame;
frame = new OutputFrame();
frame.setModel(getModel());
frame.initializeComponents();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(false);
frame.setVisible(true);
dispose();
}
});
}
});
contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add("North",textSearch);
contentPane.add("Center",textResult);
contentPane.add("South",buttonOutput);
this.pack();
}
}
class OutputFrame extends AbstractSampleFrame {
private JTextArea textMessage;
private JButton buttonBack;
public OutputFrame() {
super("OutputFrame");
}
public void initializeComponents() {
Container contentPane;
textMessage = new JTextArea("",3,30);
textMessage.setEnabled(false);
textMessage.setText("output complete.");
buttonBack = new JButton("back");
buttonBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SearchFrame frame;
frame = new SearchFrame();
frame.setModel(getModel());
frame.initializeComponents();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(false);
frame.setVisible(true);
dispose();
}
});
}
});
contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add("Center",textMessage);
contentPane.add("South",buttonBack);
this.pack();
}
}
public class Sample {
public static void main(String[] args) {
SearchFrame frame;
frame = new SearchFrame();
frame.setModel(new SampleModel());
frame.initializeComponents();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
http://q.hatena.ne.jp/