開発環境 |
Java SE 7u55 |
|
Eclipse IDE for Java Developers |
実行環境 |
Microsoft Windows 8.1 (64bit) |
目安としてマルチプラットフォームを意識するならSwing、しないならAWT。必要があればSWT。
参考
Abstract Window Toolkit(AWT)
AwtHello.java
package awthello;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AwtHello extends Frame {
public AwtHello() {
super("AwtHello");
addWindowListener(new MyWindowListener());
setSize(240, 120);
setLayout(new FlowLayout());
Label l1 = new Label("hello, world");
add(l1);
setVisible(true);
}
public static void main(String[] args) {
new AwtHello();
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Swing
SwingHello.java
package swinghello;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SwingHello extends JFrame {
public SwingHello() {
getContentPane().setLayout(new FlowLayout());
JLabel label = new JLabel("hello, world");
getContentPane().add(label);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("SwingHello");
setSize(240, 120);
setVisible(true);
}
public static void main(String[] args) {
new SwingHello();
}
}
Standard Widget Toolkit(SWT)
SWTライブラリの設定
Display display = new Display();
と入力したところでDisplayに赤いアンダーラインが付くので、マウスカーソルを合わせてFix project setupを選択する。
Add variable entry 'SWT' to build path of 'SwtHello'
のように表示されるので「OK」ボタンを押すと自動的にBuild PathにSWTが追加される。
手動で追加する場合
+
|
... |
- Project ExplorerからSwtHelloを右クリックし、「Build Path」→「Configure Build Path」を選択する。
- 「Properties for SwtHello」ダイアログの「Libraries」タブを選択する。
- 「Add Variable」ボタンを押す。
- 「New Variable Classpath Entry」ダイアログの「Configure Variables」ボタンを押す。
- 「Preferences (Filtered)」ダイアログの「New」ボタンを押す。
- Nameに「SWT」、Pathに「C:/etc/eclipse/plugins/org.eclipse.swt.win32.win32.x86_64_3.102.1.v20140206-1358.jar ※」を入力し「OK」ボタンを押す。
※環境に合わせ読み替えること
|
参考
SwtHello.java
package swthello;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SwtHello {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SwtHello");
shell.setSize(240, 120);
Label label = new Label(shell, SWT.NONE);
label.setText("hello, world");
label.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
最終更新:2014年04月23日 22:27