您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaGUI菜单栏、文本、密码及文本域组件如何使用

2025/4/22 21:42:27发布21次查看
1.菜单栏1.一级菜单栏创建菜单条 jmenubar
创建菜单 jmenu
创建菜单项 jmenuitem
总结:菜单项依附菜单、菜单依附菜单条
创建菜单栏
创建菜单
创建菜单项
菜单添加一个菜单项
菜单栏添加菜单
示例:
import javax.swing.*;import java.awt.*;public class jmenu { public static void main(string[] args){ jframe jf=new jframe("jmenu"); jf.setbounds(400,300,400,200); jf.setlayout(new flowlayout(flowlayout.center)); jmenubar bar=new jmenubar(); jmenu menu=new jmenu("菜单一"); jmenuitem item=new jmenuitem("选项1"); jmenuitem item2=new jmenuitem("选项2"); jmenuitem item3=new jmenuitem("选项3"); menu.add(item); menu.add(item2); menu.add(item3); bar.add(menu); jf.add(bar); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }}
2.多级菜单栏创建菜单栏
创建多个菜单
创建多个菜单项
菜单添加多个菜单项
菜单栏添加菜单
import javax.swing.*;import java.awt.*;public class jmenu { public static void main(string[] args){ jframe jf=new jframe("jmenu"); jf.setbounds(400,300,400,200); jf.setlayout(new flowlayout(flowlayout.center)); jmenubar bar=new jmenubar(); jmenu menu=new jmenu("菜单一"); jmenu menu2=new jmenu("菜单二"); jmenu menu3=new jmenu("菜单三"); jmenuitem item=new jmenuitem("选项1"); jmenuitem item2=new jmenuitem("选项2"); jmenuitem item3=new jmenuitem("选项3"); jmenuitem item4=new jmenuitem("选项4"); jmenuitem item5=new jmenuitem("选项5"); jmenuitem item6=new jmenuitem("选项6"); menu.add(item); menu.add(item2); menu.add(item3); menu2.add(item4); menu3.add(item5); menu3.add(item6); bar.add(menu); bar.add(menu2); bar.add(menu3); jf.add(bar); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }}
示例:
2.文本类:jtextfield
构造函数:
new jtextfield();
new jtextfield(string text);//指定默认文字
new jtextfield(int fieldwidth);//指定文本框长
new jtextfield(string text,int fieldwidth);//指定默认文字+指定文本框长度
new jtextfield(documented docmodel,string text, int fieldwidth);//指定文本框模式+指定默认文字+指定文本框长度
示例:
import javax.swing.*;import java.awt.*;public class jtextfield { public static void main(string[] args){ jframe jf=new jframe("jtextfiled"); jf.setlayout(new flowlayout(flowlayout.center)); jf.setbounds(400,300,400,300); jtextfield text=new jtextfield("请输入",20); jf.add(text); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }}
3.密码框类:jpasswordfield
构造函数:
new jpasswordfield();
new jpasswordfield(string text);//指定默认文字
new jpasswordfield(int fieldwidth);//指定文本框长
new jpasswordfield(string text,int fieldwidth);//指定默认文字+指定文本框长度
new jpasswordfield(documented docmodel,string text, int fieldwidth);//指定文本框模式+指定默认文字+指定文本框长度
其他常用方法:
setechochar("*");//设置回显字符
示例:
import javax.swing.*;import java.awt.*;public class jpasswordfield{ public static void main(string[] args){ jframe jf=new jframe("jtextfiled"); jf.setlayout(new flowlayout(flowlayout.left)); jf.setbounds(400,300,400,300); jlabel jl=new jlabel("密码"); jpasswordfield password=new jpasswordfield("",20); jf.add(jl); jf.add(password); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }}
4.文本域类:jtextarea
构造函数:
new jtextarea();
new jtextarea(string text);//指定默认文字
new jtextarea(int rows,int cols);//指定行 列
new jtextfieldarea(documented docmodel);//指定文本框模式
new jtextarea(documented docmodel,string text, int rows,int cols);//指定文本框模式+指定默认文字+指定行 列
其他常用方法:
setlinewrap();//设置文本域是否自动换行
示例:
import javax.swing.*;import java.awt.*;public class jtextarea { public static void main(string[] args) { jframe jf = new jframe("jtextarea"); jf.setlayout(new flowlayout(flowlayout.left)); jf.setbounds(400, 300, 400, 300); jtextarea area=new jtextarea(20,10); area.setlinewrap(true); jf.add(area); jf.setvisible(true); jf.setdefaultcloseoperation(jframe.exit_on_close); }}
以上就是javagui菜单栏、文本、密码及文本域组件如何使用的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product