java窗口程序初学组件小总结

容器(可以放组件)JPanel默认的布局管理器是FlowLayout:
JPanel panel=new JPanel();


按钮JButton(可以为汉字 也可以是图片):
JButton button=new JButton("按钮");
或JButton button=new JButton(new ImageIcon(""));//""里为图片路径


复选按钮JCheckBox(可以选多个)
JCheckBox muti_choice1=new JCheckBox("选项1")
JCheckBox muti_choice2=new JCheckBox("选项2")


单选按钮JRadioButton(只能且“必须”选一个):
JRadioButton radio_button1=new JRadioButton("男");
JRadioButton radio_button2=new JRadioButton("女");
ButtonGroup choose=new ButtonGroup();
choose.add(radio_button1);choose.add(radio_button2);
//后边添加到JPanel中还是添加radio_button1。


标签JLabel(只是单纯显示 不可以操作,可以为汉字 也可以是图片)
JLabel label=new JLabel("标签");
或 JLabel label=new JLabel(new ImageIcon(""));


文本框JTextField(可以输入用户名)
JTextField text=new JTextField();


密码框JPasswordField(用来输入密码显示星号)
JPasswordField password=new JPasswordField();


文本域JTextArea(用来输入大量内容,例如记事本内容)
JTextArea text_area=new JTextArea();


容器存放JTabbedPane(呈现容器界面多选一显示)
JTabbedPane choice=new JTabbedPane();
choice.add(panel1);choice.add(panel2);
choice.add(panel3);


滚动条JScrollPane(输入的文本超出后增添滚动条)
JScrollPane roll=new JScrollPane(sth)
sth可以是文本域的对象,下拉列表的对象等等;


下拉列表框JComboBox
JComboBox combobox;
String[] place={"广州","上海","保定","石家庄"};
comboBox =new JComboBox(place);

滚动条框JList
JList list;
String [] degree={"高中","大专","本科","硕士","博士"};
list =new JList(degree);
list.setVisibleRowCount(3);//一次显示的数目
roll=new JScrollPane(list);//滚动效果


工具栏JToolBar
ToolBar tool_bar=new ToolBar();
tool_bar.add(sth);
sth为按钮


菜单JMenu:不参与布局 位置是固定的
JMenuItem是最终的 JMenu 不是最终的可以有下级菜单
JMenu menu =new JMenu("文件");
JMenuItem menu_item=new JMenuItem();
menu.add(menu_item);


菜单栏JMenuBar 存放菜单JMenu
JMenuBar menu_bar=new JMenuBar();
menu_bar.add(menu);

 

posted @ 2017-08-22 09:30  旁光  阅读(394)  评论(0编辑  收藏  举报