[英]What is the shortest way of an Java AWT HelloWorld example?
This example uses Swing to render a message dialog. 本示例使用Swing渲染消息对话框。 I wondered if there is a comparable solution using the AWT only.
我想知道是否存在仅使用AWT的可比解决方案。 It should involve a minimum of classes/code.
它应该涉及最少的类/代码。 No applets please.
请不要小程序。
Edit: I know AWT is very old, but its for playing around and having fun. 编辑:我知道AWT很老,但是它可以玩耍并很有趣。
Edit2: 编辑2:
So far I came up with such code from http://www.jan.newmarch.name/java/xadvisor/dialogs/dialogs.html : 到目前为止,我从http://www.jan.newmarch.name/java/xadvisor/dialogs/dialogs.html想到了这样的代码:
import java.awt.*;
public class AWTHello extends Frame {
public static void main(String argv[]) {
new AWTHello().show();
}
AWTHello() {
add("Center", new InvokeDialog(this));
pack();
}
}
class InvokeDialog extends Button {
Frame frame;
InvokeDialog(Frame fr) {
super("Show dialog");
frame = fr;
}
public boolean action(Event evt, Object what) {
Dialog d = new Dialog(frame, false);
d.add("Center", new Label("Hello"));
d.pack();
d.show();
return true;
}
}
Try this: 尝试这个:
import java.awt.Dialog;
import java.awt.Label;
import java.awt.Window;
public class Main {
public static void main(String[] args) {
Dialog d = new Dialog(((Window)null),"Hello world!");
d.setBounds(0, 0, 180, 70);
d.add(new Label("Hello world!"));
d.setVisible(true);
}
}
ByteBit's solution is very short but will not close. ByteBit的解决方案很短,但不会关闭。 With the anonymous class as Mr. P suggested I got this.
在P先生 建议的匿名班上,我明白了。
import java.awt.*;
import java.awt.event.*;
public class AWTHello {
public static void main(String argv[]) {
Frame f = new Frame( "Hello world!" );
f.addWindowListener( new WindowAdapter(){ public void windowClosing( WindowEvent e ){ System.exit( 0 ); } } );
f.setSize( 300, 100 );
f.show();
}
}
You should not use it but if you really want to try: 您不应该使用它,但是如果您真的想尝试:
package awt;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Hello {
public static void main(String[] args) {
Frame f=new Frame("Hello World example of awt application");
Label label1=new Label("Hello World", Label.CENTER);
f.add(label1);
f.setSize(300,100);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.