这段代码是什么意思啊 这段代码是什么意思啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是窗体程序,即GUI应用。是一个登陆界面程序,和一个功能选择程序。 登录,注册(包括选择爱好和性别)。我这里显示有点问题,长度太小导致中文显示不了。你知道这个意思就行了。import java.awt.Button;import java.awt.Checkbox;import java.awt.CheckboxGroup;import java.awt.Choice;import java.awt.Color;import java.awt.Dialog;import java.awt.Frame;import java.awt.Label;import java.awt.Panel;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JOptionPane;public class Gui10 extends Panel { public Gui10(String title) { Frame frame = new Frame(title); frame.setSize(200, 400); frame.setLocationRelativeTo(null); frame.setBackground(Color.GREEN); frame.setResizable(true); // frame.setUndecorated(true); init(); frame.add(this); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // if(JOptionPane.showConfirmDialog(null, "退出","确定关闭吗?", // JOptionPane.YES_NO_OPTION,JOptionPane // .QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ System.exit(0); // } } }); } private void init() { final TextArea textArea = new TextArea(10, 20); textArea.setBackground(Color.CYAN); textArea.setText("hello,fkjava"); // textArea.setIgnoreRepaint(isEnabled()); this.add(textArea); Label label = new Label("用户名:", Label.RIGHT); this.add(label); final TextField userName = new TextField(10); userName.setEchoChar('*'); this.add(userName); Label label2 = new Label("密 码:", Label.RIGHT); this.add(label2); final TextField userPassword = new TextField(10); userPassword.setEchoChar('*'); this.add(userPassword); Button loginBtn = new Button("登录"); loginBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (userName.getText().trim().length() == 0 || userPassword.getText().trim().length() == 0) { JOptionPane.showMessageDialog(null, "用户名密码不允许为空"); // return; textArea.setText(""); } else if (userName.getText().trim().equals("1") && userPassword.getText().trim().equals("1")) { JOptionPane.showMessageDialog(null, "登录成功"); textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText()); userName.setText(""); userPassword.setText(""); // System.exit(0); } else { JOptionPane.showMessageDialog(null, "用户名或密码错误"); textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText()); userName.setText(""); userPassword.setText(""); } } }); loginBtn.setBackground(Color.blue); Button resetBtn = new Button("退出"); resetBtn.setBackground(Color.RED); this.add(loginBtn); this.add(resetBtn); resetBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (JOptionPane.showConfirmDialog(null, "退出", "确定关闭吗?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { System.exit(0); } } }); Checkbox hooby1 = new Checkbox("看书"); Checkbox hooby2 = new Checkbox("看小美女"); Checkbox hooby3 = new Checkbox("看美女"); Checkbox hooby4 = new Checkbox("看大美女", true); this.add(hooby1); this.add(hooby2); this.add(hooby3); this.add(hooby4); CheckboxGroup cg = new CheckboxGroup(); Checkbox sex = new Checkbox("男", true, cg); Checkbox sex2 = new Checkbox("女", false, cg); this.add(sex); this.add(sex2); Choice choice = new Choice(); choice.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { final Dialog dialog = new Dialog(null, "对话框", Dialog.DEFAULT_MODALITY_TYPE); // dialog.setTitle("你好"); dialog.setLocationRelativeTo(null); dialog.setResizable(false); dialog.setSize(10, 100); dialog.setBackground(Color.RED); dialog.add(new Label(e.getItem().toString())); Button ok = new Button("您选择的是:" + e.getItem().toString()); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "确定关闭吗?"); dialog.setVisible(false); } }); dialog.add(ok); dialog.setVisible(true); } }); choice.addItem("小学"); choice.addItem("中学"); choice.addItem("大学"); choice.addItem("没学"); this.add(choice); } public static void main(String[] args) { new Gui10("注册"); }} 这段代码使用的是java awt界面UI技术。代码主要创建了一个登陆窗口,窗口设定了初始大小(dialog.setSize(10,100)),不能改变大小(dialog.setResizable(false)),设置了背景颜色(dialog.setBackground(Color.RED))窗口可退出;创建了一个对话框(Dialog),按钮(Button),一组复选框(CheckboxGroup和Checkbox)和下拉列表(choice),建议楼主学习一下java awt和swt,上边那些代码都是基本知识。 import java.awt.Button;import java.awt.Checkbox;import java.awt.CheckboxGroup;import java.awt.Choice;import java.awt.Color;import java.awt.Dialog;import java.awt.Frame;import java.awt.Label;import java.awt.Panel;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.ImageIcon;import javax.swing.JOptionPane;public class Gui10 extends Panel { public Gui10(String title) { Frame frame = new Frame(title); frame.setSize(200, 400); //设置窗口大小,200px*400px frame.setLocationRelativeTo(null);// 将窗口置于屏幕的中央 frame.setBackground(Color.GREEN);//设置窗口背景颜色 frame.setResizable(false);// 设置窗体大小不能调整 frame.setIconImage(new ImageIcon("C:/Users/hp-/Documents/" + "美图图库/Snapshot_20131024_1_副本.jpg").getImage());//设置窗口显示的图像 // frame.setUndecorated(true); init();//调用init() frame.add(this); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { // if(JOptionPane.showConfirmDialog(null, "退出","确定关闭吗?", // JOptionPane.YES_NO_OPTION,JOptionPane // .QUESTION_MESSAGE)==JOptionPane.YES_OPTION){ System.exit(0); // } } }); } private void init() { final TextArea textArea = new TextArea(10, 20); textArea.setBackground(Color.CYAN); //设置背景色为青色 textArea.setText("hello,fkjava");//设置文本内容 // textArea.setIgnoreRepaint(isEnabled()); this.add(textArea);//将这个区域添加到上面的窗口 Label label = new Label("用户名:", Label.RIGHT); //新建一个"用户名",右对齐 this.add(label);//添加到窗口 final TextField userName = new TextField(10);//新建一个文本框,这个是用来输入"用户名"的 userName.setEchoChar('*');//设置显示内容为"*",就是输入的用户名显示为"*" this.add(userName);//添加到窗口 Label label2 = new Label("密 码:", Label.RIGHT);//新建一个"密码",右对齐 this.add(label2);//添加到窗口 final TextField userPassword = new TextField(10);;//新建一个文本框,这个是用来输入"密码"的 userPassword.setEchoChar('*');//设置显示内容为"*",就是输入的密码显示为"*" this.add(userPassword);//添加到窗口 Button loginBtn = new Button("登录");//添加一个"登陆"按钮 loginBtn.addActionListener(new ActionListener() { //这是对“登陆”按钮的一些操作 public void actionPerformed(ActionEvent e) { // 点击"登陆时",触发该事件 if (userName.getText().trim().length() == 0 || userPassword.getText().trim().length() == 0) { // 如果"用户名"和"密码",去掉前后的空格为空 JOptionPane.showMessageDialog(null, "用户名密码不允许为空"); //弹出一个对话框,提示"用户名密码不允许为空" // return; textArea.setText(""); //清空窗口的内容 } else if (userName.getText().trim().equals("1") && userPassword.getText().trim().equals("1")) {// 如果用户名和密码,去掉前后的空格为"1" JOptionPane.showMessageDialog(null, "登录成功");//弹出一个对话框,提示"登录成功" textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText());//在窗口中显示"您输入的用户名是:","您输入的密码是:" userName.setText("");//清空"用户名"对应的文本框 userPassword.setText("");//清空"密码"对应的文本框 // System.exit(0); } else { JOptionPane.showMessageDialog(null, "用户名或密码错误");//弹出一个对话框,提示"用户名或密码错误" textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText());//在窗口中显示"您输入的用户名是:","您输入的密码是:" userName.setText("");//清空"用户名"对应的文本框 userPassword.setText("");//清空"密码"对应的文本框 } } }); loginBtn.setBackground(Color.blue);//设置"登陆"按钮为蓝色 Button resetBtn = new Button("退出");//添加一个"退出"按钮 resetBtn.setBackground(Color.RED);//设置"退出"按钮为红色 this.add(loginBtn);//添加"登陆"按钮到窗口 this.add(resetBtn);//添加"退出"按钮到窗口 resetBtn.addActionListener(new ActionListener() {//"退出"按钮的一些操作 /* * 点击"退出"时弹出一个对话框,提示"退出", "确定关闭吗?"点击"是",停掉虚拟机,退出程序。点击"否",退回到原界面 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { if (JOptionPane .showConfirmDialog(null, "退出", "确定关闭吗?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) { System.exit(0); } } }); // 做了4个复选框,默认选中"看大美女" Checkbox hooby1 = new Checkbox("看书"); Checkbox hooby2 = new Checkbox("看小美女"); Checkbox hooby3 = new Checkbox("看美女"); Checkbox hooby4 = new Checkbox("看大美女", true); // 将4个复选框添加到窗口 this.add(hooby1); this.add(hooby2); this.add(hooby3); this.add(hooby4); // 也是复选框的一种,只能选中一个,默认为"男" CheckboxGroup cg = new CheckboxGroup(); Checkbox sex = new Checkbox("男", true, cg); Checkbox sex2 = new Checkbox("女", false, cg); // 将2个复选框添加到窗口 this.add(sex); this.add(sex2); Choice choice = new Choice(); // 新建一个下拉列表 choice.addItemListener(new ItemListener() { // 剩下的操作跟上面的差不多,自己看下吧 public void itemStateChanged(ItemEvent e) { final Dialog dialog = new Dialog(null, "对话框", Dialog.DEFAULT_MODALITY_TYPE); // dialog.setTitle("你好"); dialog.setLocationRelativeTo(null); dialog.setResizable(false); dialog.setSize(10, 100); dialog.setBackground(Color.RED); dialog.add(new Label(e.getItem().toString())); Button ok = new Button("您选择的是:" + e.getItem().toString()); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "确定关闭吗?"); dialog.setVisible(false); } }); dialog.add(ok); dialog.setVisible(true); } }); choice.addItem("小学"); choice.addItem("中学"); choice.addItem("大学"); choice.addItem("没学"); this.add(choice); } public static void main(String[] args) { new Gui10("注册"); }}注释写的差不多了,楼主自己看吧 有关ActionListener 的一个错误太奇怪了,急,请教高手!! 如何用XML帮定TXT数据 一个基础问题(但是现在俺还没弄懂) 我们技术主任说Java有现成的框架,所以不需要你去设计.什么意思? 请教数据库连接的速度问题 如何把窗口置顶 数据库的问题pqds快来 小弟一点不懂Java!请教!!! 为什么这段程序,运行时要报错啊?? Java写的界面程序, AWT的, 在Mac下运行的权限问题 文件编码转换问题 【Java UDP网络编程死锁问题,在线等......】
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JOptionPane;public class Gui10 extends Panel {
public Gui10(String title) {
Frame frame = new Frame(title);
frame.setSize(200, 400);
frame.setLocationRelativeTo(null);
frame.setBackground(Color.GREEN);
frame.setResizable(true);
// frame.setUndecorated(true); init();
frame.add(this);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// if(JOptionPane.showConfirmDialog(null, "退出","确定关闭吗?",
// JOptionPane.YES_NO_OPTION,JOptionPane
// .QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0);
// }
}
});
} private void init() {
final TextArea textArea = new TextArea(10, 20);
textArea.setBackground(Color.CYAN);
textArea.setText("hello,fkjava");
// textArea.setIgnoreRepaint(isEnabled());
this.add(textArea); Label label = new Label("用户名:", Label.RIGHT);
this.add(label);
final TextField userName = new TextField(10);
userName.setEchoChar('*');
this.add(userName);
Label label2 = new Label("密 码:", Label.RIGHT);
this.add(label2);
final TextField userPassword = new TextField(10);
userPassword.setEchoChar('*');
this.add(userPassword);
Button loginBtn = new Button("登录");
loginBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userName.getText().trim().length() == 0 || userPassword.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
// return;
textArea.setText("");
} else if (userName.getText().trim().equals("1") && userPassword.getText().trim().equals("1")) {
JOptionPane.showMessageDialog(null, "登录成功");
textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText());
userName.setText("");
userPassword.setText(""); // System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");
textArea.setText("您输入的用户名是:" + userName.getText() + "\n" + "您输入的密码是:" + userPassword.getText());
userName.setText("");
userPassword.setText(""); }
}
});
loginBtn.setBackground(Color.blue);
Button resetBtn = new Button("退出");
resetBtn.setBackground(Color.RED);
this.add(loginBtn);
this.add(resetBtn);
resetBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (JOptionPane.showConfirmDialog(null, "退出", "确定关闭吗?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
System.exit(0);
} } });
Checkbox hooby1 = new Checkbox("看书");
Checkbox hooby2 = new Checkbox("看小美女");
Checkbox hooby3 = new Checkbox("看美女");
Checkbox hooby4 = new Checkbox("看大美女", true);
this.add(hooby1);
this.add(hooby2);
this.add(hooby3);
this.add(hooby4);
CheckboxGroup cg = new CheckboxGroup();
Checkbox sex = new Checkbox("男", true, cg);
Checkbox sex2 = new Checkbox("女", false, cg);
this.add(sex);
this.add(sex2);
Choice choice = new Choice();
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
final Dialog dialog = new Dialog(null, "对话框", Dialog.DEFAULT_MODALITY_TYPE);
// dialog.setTitle("你好");
dialog.setLocationRelativeTo(null);
dialog.setResizable(false);
dialog.setSize(10, 100);
dialog.setBackground(Color.RED);
dialog.add(new Label(e.getItem().toString()));
Button ok = new Button("您选择的是:" + e.getItem().toString());
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "确定关闭吗?");
dialog.setVisible(false); }
});
dialog.add(ok);
dialog.setVisible(true);
}
});
choice.addItem("小学");
choice.addItem("中学");
choice.addItem("大学");
choice.addItem("没学");
this.add(choice); } public static void main(String[] args) {
new Gui10("注册");
}}
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.ImageIcon;
import javax.swing.JOptionPane;public class Gui10 extends Panel {
public Gui10(String title) {
Frame frame = new Frame(title);
frame.setSize(200, 400); //设置窗口大小,200px*400px
frame.setLocationRelativeTo(null);// 将窗口置于屏幕的中央
frame.setBackground(Color.GREEN);//设置窗口背景颜色
frame.setResizable(false);// 设置窗体大小不能调整
frame.setIconImage(new ImageIcon("C:/Users/hp-/Documents/"
+ "美图图库/Snapshot_20131024_1_副本.jpg").getImage());//设置窗口显示的图像
// frame.setUndecorated(true); init();//调用init()
frame.add(this);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// if(JOptionPane.showConfirmDialog(null, "退出","确定关闭吗?",
// JOptionPane.YES_NO_OPTION,JOptionPane
// .QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
System.exit(0);
// }
}
});
} private void init() {
final TextArea textArea = new TextArea(10, 20);
textArea.setBackground(Color.CYAN); //设置背景色为青色
textArea.setText("hello,fkjava");//设置文本内容
// textArea.setIgnoreRepaint(isEnabled());
this.add(textArea);//将这个区域添加到上面的窗口 Label label = new Label("用户名:", Label.RIGHT); //新建一个"用户名",右对齐
this.add(label);//添加到窗口
final TextField userName = new TextField(10);//新建一个文本框,这个是用来输入"用户名"的
userName.setEchoChar('*');//设置显示内容为"*",就是输入的用户名显示为"*"
this.add(userName);//添加到窗口
Label label2 = new Label("密 码:", Label.RIGHT);//新建一个"密码",右对齐
this.add(label2);//添加到窗口
final TextField userPassword = new TextField(10);;//新建一个文本框,这个是用来输入"密码"的
userPassword.setEchoChar('*');//设置显示内容为"*",就是输入的密码显示为"*"
this.add(userPassword);//添加到窗口
Button loginBtn = new Button("登录");//添加一个"登陆"按钮
loginBtn.addActionListener(new ActionListener() { //这是对“登陆”按钮的一些操作
public void actionPerformed(ActionEvent e) { // 点击"登陆时",触发该事件
if (userName.getText().trim().length() == 0
|| userPassword.getText().trim().length() == 0) { // 如果"用户名"和"密码",去掉前后的空格为空
JOptionPane.showMessageDialog(null, "用户名密码不允许为空"); //弹出一个对话框,提示"用户名密码不允许为空"
// return;
textArea.setText(""); //清空窗口的内容
} else if (userName.getText().trim().equals("1")
&& userPassword.getText().trim().equals("1")) {// 如果用户名和密码,去掉前后的空格为"1"
JOptionPane.showMessageDialog(null, "登录成功");//弹出一个对话框,提示"登录成功"
textArea.setText("您输入的用户名是:" + userName.getText() + "\n"
+ "您输入的密码是:" + userPassword.getText());//在窗口中显示"您输入的用户名是:","您输入的密码是:"
userName.setText("");//清空"用户名"对应的文本框
userPassword.setText("");//清空"密码"对应的文本框 // System.exit(0);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误");//弹出一个对话框,提示"用户名或密码错误"
textArea.setText("您输入的用户名是:" + userName.getText() + "\n"
+ "您输入的密码是:" + userPassword.getText());//在窗口中显示"您输入的用户名是:","您输入的密码是:"
userName.setText("");//清空"用户名"对应的文本框
userPassword.setText("");//清空"密码"对应的文本框 }
}
});
loginBtn.setBackground(Color.blue);//设置"登陆"按钮为蓝色
Button resetBtn = new Button("退出");//添加一个"退出"按钮
resetBtn.setBackground(Color.RED);//设置"退出"按钮为红色
this.add(loginBtn);//添加"登陆"按钮到窗口
this.add(resetBtn);//添加"退出"按钮到窗口
resetBtn.addActionListener(new ActionListener() {//"退出"按钮的一些操作
/*
* 点击"退出"时弹出一个对话框,提示"退出", "确定关闭吗?"点击"是",停掉虚拟机,退出程序。点击"否",退回到原界面
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if (JOptionPane
.showConfirmDialog(null, "退出", "确定关闭吗?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
System.exit(0);
} } });
// 做了4个复选框,默认选中"看大美女"
Checkbox hooby1 = new Checkbox("看书");
Checkbox hooby2 = new Checkbox("看小美女");
Checkbox hooby3 = new Checkbox("看美女");
Checkbox hooby4 = new Checkbox("看大美女", true);
// 将4个复选框添加到窗口
this.add(hooby1);
this.add(hooby2);
this.add(hooby3);
this.add(hooby4);
// 也是复选框的一种,只能选中一个,默认为"男"
CheckboxGroup cg = new CheckboxGroup();
Checkbox sex = new Checkbox("男", true, cg);
Checkbox sex2 = new Checkbox("女", false, cg);
// 将2个复选框添加到窗口
this.add(sex);
this.add(sex2);
Choice choice = new Choice(); // 新建一个下拉列表
choice.addItemListener(new ItemListener() {
// 剩下的操作跟上面的差不多,自己看下吧
public void itemStateChanged(ItemEvent e) {
final Dialog dialog = new Dialog(null, "对话框",
Dialog.DEFAULT_MODALITY_TYPE);
// dialog.setTitle("你好");
dialog.setLocationRelativeTo(null);
dialog.setResizable(false);
dialog.setSize(10, 100);
dialog.setBackground(Color.RED);
dialog.add(new Label(e.getItem().toString()));
Button ok = new Button("您选择的是:" + e.getItem().toString());
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "确定关闭吗?");
dialog.setVisible(false); }
});
dialog.add(ok);
dialog.setVisible(true);
}
});
choice.addItem("小学");
choice.addItem("中学");
choice.addItem("大学");
choice.addItem("没学");
this.add(choice); } public static void main(String[] args) {
new Gui10("注册");
}}注释写的差不多了,楼主自己看吧