一个窗体,一个按钮,最简单的java代码怎写?

如题所述

public class Demo extends JFrame
{
    JButton jb; //一个按钮
    public static void main(String []args){
        new Demo();
    }
    public Demo()
    {
        this.setLayout(new FlowLayout());
        jb=new JButton("按扭");
        this.add(jb);
        this.setSize(400,300);
        this.setVisible(true);
        this.setLocation(500, 200);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-09
简单的 杨辉三角的 打印
public class Test {

/**
* 杨辉三角
*/
public static void main(String[] args) {
/*int [][] a = new int[9][];

for(int i = 0; i < a.length;i ++){
a[i] = new int[i];
for(int j = 0; j < a[i].length; j++){
if(( j == 0) || j == a[i].length -1 ){
a[i][j] = 1;
}else {
a[i][j] = a[i-1][j] + a[i-1][j-1];
}
}
}

for(int i = 0; i < a.length;i ++){
for(int j = 0; j < a[i].length; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}*/

int [][] a = new int[4][3];
for(int i = 0; i < a.length; i ++){
for(int j = 0; j < a[i].length; j ++){
a[i][j] = i*j;
}
}

for(int i = 0; i < a.length; i ++){
for(int j = 0; j < a[i].length; j ++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}

}
第2个回答  2013-06-09
import java.awt.*;
import java.awt.event.*;

public class zb {
public static void main(String[] args) {
new MyFrame("JAVA2011记事本");
}
}

class MyFrame extends Frame {

MyFrame(String s) {
super(s);
Label lblPlus1 = new Label("登陆");
TextField tf1 = new TextField(10);
Label lblPlus2 = new Label("密码");
TextField tf2 = new TextField(10);

tf2.setEchoChar('*');
add(lblPlus1);
add(tf1);
add(lblPlus2);
add(tf2);

pack();

Button b1 = new Button("取消");
Button b2 = new Button("登录");
Button b3 = new Button("注册");
Button b4 = new Button("注销");
setLayout(new FlowLayout());
add(b1);
add(b2);
add(b3);
add(b4);
setBounds(300, 300, 320, 120);
setVisible(true);
b1.addWindowListener(new Buttonsb1()); //我想在button b1 添加一个功能,一按取消就会退出窗口
this.addWindowListener(new MyWindowMonitor());
}

class MyWindowMonitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}

class Buttonsb1 extends WindowAdapter { //这个是button b1 的取消按钮
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
}
第3个回答  推荐于2017-09-05
public class Test extends JFrame
{
JButton jb;
public static void main(String []args){
new Test();
}
public Test()
{
jb=new JButton("按扭");
this.add(jb);
this.setSize(400,300);
this.setVisible(true);
}
}本回答被提问者和网友采纳
第4个回答  2013-06-09
不是吧,太简单了,无人教你吗?
相似回答