按钮控件实例:
一、JButton
package cn.tl.buttontest; import java.awt.*; import javax.swing.*; public class Button1 { static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args) { //创建顶层框架类(设置标题和框架大小) JFrame jf = new JFrame(); jf.setSize(WIDTH, HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setTitle("welcome to java swing"); //通过ToolKit类中的方法,将顶层框架放置到屏幕中央 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y); //添加两个按钮jb1,jb2 JButton jb1=new JButton("是"); JButton jb2=new JButton("否"); Panel panel=new Panel(); panel.add(jb1); panel.add(jb2); jf.add(panel); jf.show();// //显示顶层框架类对象 } }
显示效果:
二、JRadioButton
package cn.tl.buttontest; import java.awt.*; import javax.swing.*; public class Button2 { static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args) { //创建顶层框架类(设置标题和框架大小) JFrame jf = new JFrame(); jf.setSize(WIDTH, HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setTitle("welcome to java swing"); //通过ToolKit类中的方法,将顶层框架放置到屏幕中央 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y); JRadioButton jr1 = new JRadioButton("忽略"); JRadioButton jr2 = new JRadioButton("继续"); JRadioButton jr3 = new JRadioButton("跳过"); Panel p = new Panel(); //添加对象jr1,jr2,jr3到面板对象p里 p.add(jr1); p.add(jr2); p.add(jr3); jf.add(p, BorderLayout.SOUTH); jf.show(); } }
显示效果:
三、JRadioButton、ButtonGroup
package cn.tl.buttontest; import java.awt.*; import javax.swing.*; public class Button3 { static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args) { //创建顶层框架类(设置标题和框架大小) JFrame jf = new JFrame(); jf.setSize(WIDTH, HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setTitle("welcome to java swing"); //通过ToolKit类中的方法,将顶层框架放置到屏幕中央 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y); JRadioButton jr1 = new JRadioButton("忽略"); JRadioButton jr2 = new JRadioButton("继续"); JRadioButton jr3 = new JRadioButton("跳过"); ButtonGroup bg = new ButtonGroup(); //创建一个按钮组对象bg Panel p = new Panel(); //添加三个 单选按钮jr1,jr2和jr3到按钮组对象bg bg.add(jr1); bg.add(jr2); bg.add(jr3); //添加三个 单选按钮jr1,jr2和jr3到面板组对象p p.add(jr1); p.add(jr2); p.add(jr3); jf.add(p, BorderLayout.SOUTH); jf.show(); } }
显示效果:
四、JToggleButton
package cn.tl.buttontest; import java.awt.*; import javax.swing.*; public class Button4 { static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args) { // 创建顶层框架类(设置标题和框架大小) JFrame jf = new JFrame(); jf.setSize(WIDTH, HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setTitle("welcome to java swing"); // 通过ToolKit类中的方法,将顶层框架放置到屏幕中央 Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y); JToggleButton jt1 = new JToggleButton("忽略"); JToggleButton jt2 = new JToggleButton("继续"); JToggleButton jt3 = new JToggleButton("跳过"); Panel p = new Panel(); p.add(jt1); p.add(jt2); p.add(jt3); jf.add(p, BorderLayout.SOUTH); jf.show(); } }
显示效果:
文本控件实例:
JTextField jt = new JTextField(10);
JPasswordField jp = new JPasswordField(10);
JTextArea jt = new JTextArea(5, 5);
5行5列;
标签组件实例:
JLabel jl=new JLabel("欢迎您",JLabel.RIGHT);
选择组件实例:
String[] a = new String[5]; a[0] = "丽丽"; a[1] = "米米"; a[2] = "毛毛"; a[3] = "妮妮"; a[4] = "欢欢"; JComboBox jc = new JComboBox(a);
显示效果:
菜单组件实例:
package com.cjgong.caidan; import javax.swing.*; import java.awt.*; public class Test{ static final int WIDTH = 300; static final int HEIGHT = 200; public static void main(String[] args) { JFrame jf = new JFrame(); jf.setSize(WIDTH, HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setTitle("记事本"); Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int width = screenSize.width; int height = screenSize.height; int x = (width - WIDTH) / 2; int y = (height - HEIGHT) / 2; jf.setLocation(x, y); JMenuBar menubar1 = new JMenuBar(); jf.setJMenuBar(menubar1); JMenu menu1 = new JMenu("文件"); JMenu menu2 = new JMenu("编辑"); JMenu menu3 = new JMenu("视图"); JMenu menu4 = new JMenu("帮助"); menubar1.add(menu1); menubar1.add(menu2); menubar1.add(menu3); JMenuItem item1 = new JMenuItem("打开"); JMenuItem item2 = new JMenuItem("保存"); JMenuItem item3 = new JMenuItem("打印"); JMenuItem item4 = new JMenuItem("退出"); menu1.add(item1); menu1.add(item2); menu1.addSeparator(); //添加分隔条 menu1.add(item3); menu1.addSeparator(); //添加分隔条 menu1.add(item4); jf.show(); } }
显示效果:
对话框组件实例:
package com.cjgong.diag; import java.awt.*; import javax.swing.*; public class Test extends Frame { public static void main(String[] args) { JOptionPane.showConfirmDialog(null,"确定吗?","描述!",JOptionPane.YES_NO_CANCEL_OPTION); } }
显示效果: