Java Swing程序设计实战
程序员文章站
2022-06-16 09:16:57
一、按钮组件1.1 提交按钮组件package swing;import java.awt.*;import java.awt.event.*;import java.net.*;import jav...
一、按钮组件
1.1 提交按钮组件
package swing; import java.awt.*; import java.awt.event.*; import java.net.*; import javax.swing.*; public class jbuttontest extends jframe { /** * */ private static final long serialversionuid = 1l; public jbuttontest() { url url = jbuttontest.class.getresource("imagebutton.jpg"); icon icon = new imageicon(url); setlayout(new gridlayout(3, 2, 5, 5)); // 设置网格布局管理器 container c = getcontentpane(); // 创建容器 for (int i = 0; i < 5; i++) { // 创建按钮,同时设置按钮文字与图标 jbutton j = new jbutton("button" + i, icon); c.add(j); // 在容器中添加按钮 if (i % 2 == 0) { j.setenabled(false); // 设置其中一些按钮不可用 } } jbutton jb = new jbutton(); // 实例化一个没有文字与图片的按钮 jb.setmaximumsize(new dimension(90, 30)); // 设置按钮与图片相同大小 jb.seticon(icon); // 为按钮设置图标 jb.sethideactiontext(true); jb.settooltiptext("图片按钮"); // 设置按钮提示为文字 jb.setborderpainted(false); // 设置按钮边界不显示 jb.addactionlistener(new actionlistener() { // 为按钮添加监听事件 public void actionperformed(actionevent e) { // 弹出确认对话框 joptionpane.showmessagedialog(null, "弹出对话框"); } }); c.add(jb); // 将按钮添加到容器中 settitle("创建带文字与图片的按钮"); setsize(350, 150); setvisible(true); setdefaultcloseoperation(windowconstants.dispose_on_close); } public static void main(string args[]) { new jbuttontest(); } }
1.2 复选框组件
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class checkboxtest extends jframe { /** * */ private static final long serialversionuid = 1l; private jpanel panel1 = new jpanel(); private jpanel panel2 = new jpanel(); private jtextarea jt = new jtextarea(3, 10); private jcheckbox jc1 = new jcheckbox("1"); private jcheckbox jc2 = new jcheckbox("2"); private jcheckbox jc3 = new jcheckbox("3"); public checkboxtest() { container c = getcontentpane(); c.setlayout(new borderlayout()); c.add(panel1, borderlayout.north); final jscrollpane scrollpane = new jscrollpane(jt); panel1.add(scrollpane); c.add(panel2, borderlayout.south); panel2.add(jc1); jc1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (jc1.isselected()) jt.append("复选框1被选中\n"); } }); panel2.add(jc2); jc2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (jc2.isselected()) jt.append("复选框2被选中\n"); } }); panel2.add(jc3); jc3.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (jc3.isselected()) jt.append("复选框3被选中\n"); } }); setsize(200, 160); setvisible(true); settitle("复选框的使用"); setdefaultcloseoperation(windowconstants.exit_on_close); } public static void main(string[] args) { new checkboxtest(); } }
二、列表组件
2.1 jcombobox类
import java.awt.*; import javax.swing.*; public class jcomboboxmodeltest extends jframe { private static final long serialversionuid = 1l; jcombobox<string> jc = new jcombobox<>(new mycombobox()); jlabel jl = new jlabel("请选择证件"); public jcomboboxmodeltest() { setsize(new dimension(160, 180)); setvisible(true); settitle("在窗口中设置下拉列表框"); setdefaultcloseoperation(windowconstants.dispose_on_close); container cp = getcontentpane(); cp.setlayout(new flowlayout()); cp.add(jl); cp.add(jc); } public static void main(string[] args) { new jcomboboxmodeltest(); } } class mycombobox extends abstractlistmodel<string> implements comboboxmodel<string> { /** * */ private static final long serialversionuid = 1l; string selecteditem = null; string[] test = { "身份证", "军人证", "学生证", "工作证" }; public string getelementat(int index) { return test[index]; } public int getsize() { return test.length; } public void setselecteditem(object item) { selecteditem = (string) item; } public object getselecteditem() { return selecteditem; } public int getindex() { for (int i = 0; i < test.length; i++) { if (test[i].equals(getselecteditem())) return i; } return 0; } }
2.2 列表框组件
import java.awt.*; import javax.swing.*; public class jlisttest extends jframe { /** * */ private static final long serialversionuid = 1l; public jlisttest() { container cp = getcontentpane(); cp.setlayout(null); jlist<string> jl = new jlist<>(new mylistmodel()); jscrollpane js = new jscrollpane(jl); js.setbounds(10, 10, 100, 100); cp.add(js); settitle("在这个窗体中使用了列表框"); setsize(200, 150); setvisible(true); setdefaultcloseoperation(windowconstants.dispose_on_close); } public static void main(string args[]) { new jlisttest(); } } class mylistmodel extends abstractlistmodel<string> { /** * */ private static final long serialversionuid = 1l; private string[] contents = { "列表1", "列表2", "列表3", "列表4", "列表5", "列表6" }; public string getelementat(int x) { if (x < contents.length) return contents[x++]; else return null; } public int getsize() { return contents.length; } }
三、文本组件
3.1 文本框组件
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jtextfieldtest extends jframe { /** * */ private static final long serialversionuid = 1l; public jtextfieldtest() { setsize(250, 100); setdefaultcloseoperation(windowconstants.dispose_on_close); container cp = getcontentpane(); getcontentpane().setlayout(new flowlayout()); final jtextfield jt = new jtextfield("aaa", 20); final jbutton jb = new jbutton("清除"); cp.add(jt); cp.add(jb); jt.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { // todo 自动生成方法存根 jt.settext("触发事件"); } }); jb.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { jt.settext(""); jt.requestfocus(); } }); setvisible(true); } public static void main(string[] args) { new jtextfieldtest(); } }
3.2 密码框
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class jtextfieldtest extends jframe { /** * */ private static final long serialversionuid = 1l; public jtextfieldtest() { setsize(250, 100); setdefaultcloseoperation(windowconstants.dispose_on_close); container cp = getcontentpane(); getcontentpane().setlayout(new flowlayout()); // final jtextfield jt=new jtextfield("aaa",20); jpasswordfield jp = new jpasswordfield("", 20); jp.setechochar('*'); final jbutton jb = new jbutton("清除"); cp.add(jp); cp.add(jb); jp.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { // todo 自动生成方法存根 jp.settext("触发事件"); } }); jb.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { jp.settext(""); jp.requestfocus(); } }); setvisible(true); } public static void main(string[] args) { new jtextfieldtest(); } }
3.3 文本域组件
import java.awt.*; import javax.swing.*; public class jtextareatest extends jframe { public jtextareatest() { setsize(200, 100); settitle("定义自动换行的文本域"); setdefaultcloseoperation(windowconstants.exit_on_close); container cp = getcontentpane(); jtextarea jt = new jtextarea("文本域", 6, 6); jt.setlinewrap(true); cp.add(jt); setvisible(true); } public static void main(string[] args) { new jtextareatest(); } }
四、常用时间监听器
4.1 动作事件监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class simpleevent extends jframe { /** * */ private static final long serialversionuid = 1l; private jbutton jb = new jbutton("我是按钮,点击我"); public simpleevent() { setlayout(null); setsize(200, 100); setdefaultcloseoperation(windowconstants.dispose_on_close); container cp = getcontentpane(); cp.add(jb); jb.setbounds(10, 10, 100, 30); jb.addactionlistener(new jbaction()); setvisible(true); } class jbaction implements actionlistener { public void actionperformed(actionevent arg0) { jb.settext("我被单击了"); } } public static void main(string[] args) { new simpleevent(); } }
4.2 焦点事件监听器
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class focuseventtest extends jframe { /** * */ private static final long serialversionuid = 1l; public focuseventtest() { setsize(250, 100); setdefaultcloseoperation(windowconstants.exit_on_close); container cp = getcontentpane(); getcontentpane().setlayout(new flowlayout()); final jlabel label = new jlabel(); getcontentpane().add(label); jtextfield jt = new jtextfield("请单击其他文本框", 10); jtextfield jt2 = new jtextfield("请单击我", 10); cp.add(jt); cp.add(jt2); jt.addfocuslistener(new focuslistener() { // 组件失去焦点时调用的方法 public void focuslost(focusevent arg0) { joptionpane.showmessagedialog(null, "文本框失去焦点"); } // 组件获取键盘焦点时调用的方法 public void focusgained(focusevent arg0) { } }); setvisible(true); } public static void main(string[] args) { new focuseventtest(); } }
到此这篇关于javaswing实现程序界面设计的文章就介绍到这了,更多相关javaswing程序界面设计内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!