初めに
* Javaでモジュールを提供する場合、PromptやEclipseで提供するよりも
画面で起動したほうが何かと都合がいいので、その際にSwingを使用してみる。
(別に、デモ、社内用、テスト(スタブ/ドライバー)用ならSwingでもいいかと)
サンプル
* Start/Stopボタンを付け、標準出力表示用にTextAreaを付けてみた。
SwingBase.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class SwingBase extends JFrame implements ActionListener {
/** */
private static final long serialVersionUID = -4658271370655854940L;
private static final String TITLE = "This is title";
private static final String ICON = "etc/icon.jpg";
private static final int X_COORDINATE_SCREEN = 600;
private static final int Y_COORDINATE_SCREEN = 200;
private static final int WIDTH_SCREEN = 800;
private static final int HEIGHT_SCREEN = 500;
private static final int ROW_TEXT_AREA = WIDTH_SCREEN - 50;
private static final int COLUMN_TEXT_AREA = HEIGHT_SCREEN - 50;
JButton strartButton;
JButton stopButton;
JTextArea outoutTextArea;
public SwingBase() {
JPanel northPanel = new JPanel();
JPanel westPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel eastPanel = new JPanel();
JPanel southPanel = new JPanel();
// For North Panel
JLabel outputLabel = new JLabel("<html><h1>[Output Log]</h1></html>",
JLabel.CENTER);
northPanel.add(outputLabel);
// For West Panel
westPanel.setVisible(false);
// For East Panel
eastPanel.setVisible(false);
// For Center Panel
this.outoutTextArea = new JTextArea();
this.redirectConsole(this.outoutTextArea);
JScrollPane outoutScrollPane = new JScrollPane(this.outoutTextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
outoutScrollPane.setPreferredSize(new Dimension(ROW_TEXT_AREA,
COLUMN_TEXT_AREA));
centerPanel.add(outoutScrollPane);
// For South Panel
this.strartButton = new JButton("Start");
this.stopButton = new JButton("Stop");
southPanel.add(this.strartButton);
southPanel.add(this.stopButton);
this.strartButton.addActionListener(this);
this.stopButton.addActionListener(this);
// For Layout
this.setLayout(new BorderLayout());
this.getContentPane().add(northPanel, BorderLayout.NORTH);
this.getContentPane().add(westPanel, BorderLayout.WEST);
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
this.getContentPane().add(eastPanel, BorderLayout.EAST);
this.getContentPane().add(southPanel, BorderLayout.SOUTH);
this.setTitle(TITLE);
this.setBounds(X_COORDINATE_SCREEN, Y_COORDINATE_SCREEN, WIDTH_SCREEN,
HEIGHT_SCREEN);
// Title ICON
ImageIcon icon = new ImageIcon(ICON);
this.setIconImage(icon.getImage());
// For Close
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
stop();
} catch (Exception err) {
}
}
});
}
public static void main(String args[]) {
SwingBase userInterface = new SwingBase();
userInterface.showScreen();
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
Object control = actionEvent.getSource();
if (control == this.strartButton) {
this.strat();
} else if (control == this.stopButton) {
this.stop();
}
}
public void showScreen() {
this.setVisible(true);
}
public void strat() {
//TODO Do something
}
public void stop() {
//TODO Do something
}
private void redirectConsole(JTextArea textarea) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream() {
@Override
public synchronized void flush() throws IOException {
textarea.setText(toString());
}
};
PrintStream out = new PrintStream(bytes, true);
System.setErr(out);
System.setOut(out);
}
}