GUI
;容器
;JFrame ;, ;JPanel ;, ;JScrollPane
;
组件
;JButton按钮 ;, ;JLable文字 ;, ;JTextFiled但行文本框 ;, ;JTextArea多行文本框 ;,JRadioButton单选按钮 ;, ;JCheckBox复选按钮 ;, ;JComboBox下拉列表.
;
JFrame窗口
;- setVisible(true): ;设置窗体可见
- setLocationRelativeTo(null);窗体居中显示
- setResizable(false);设置窗体不可缩放
- setLayout(null);绝对定位
- setBackground(Color.CYAN);设置容器的背景颜色
- 添加WindowListener监听器,复写windowClosing方法,System.exit(0)设置窗体自动关闭
JScrollPane滚动条
;<pre>JScrollPane ;scrollPane ;= ;new ;JScrollPane();; ; ; ; ; ; ; ;scrollPane.setBounds(180, ;100, ;390, ;300);
; ; ; ; ; ; ; ;jPanel.add(scrollPane);
; ; ; ; ; ; ; ;//设置自动出现
; ; ; ; ; ; ; ;scrollPane.setHorizontalScrollBarPolicy(
; ; ; ; ; ; ; ; ; ; ; ;JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); ;
; ; ; ; ; ; ; ;scrollPane.setVerticalScrollBarPolicy( ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setViewportView(testContent);//行文本框添加滚动条</pre> ;
JButton按钮
;setEnabled(false);默认不可点击 ;setBounds(x,y,width,height);设置绝对定位的xy和宽高 ;getText();返回按钮的文字
;
JLable文字
;setFont(new ;Font("楷体",Font.PLAIN,15));设置文字字体, ;setForeground(Color.RED);设置文字颜色
;
JTextFiled单行文本域
;setEditable(false);设置文本域不可以编辑 ;setText();/getText(); ;setColumns();设置显示长度
;
JTextArea多行文本域
;setColumns();/setRows();设置行列 ;textArea.setLineWrap(true);自动换行:
;
JRadioButton单选按钮
;JRadioButton(String ;str);创建一个单选按钮, ;并指定其文字 ;isSelected();返回是否被选中 ;setSelected(boolean ;b);设置是否被选中 ;ButtonGroup ;group ;= ;new ;ButtonGroup(); ;group.add(answerA);group.add(answerB);//将单选按钮ABCD放到按钮组中
;
JCheckBox复选按钮
;方法和单选差不多
;
ComboBox下拉列表
;addItem(String ;str);添加下拉项,也可以在创建comboBox的时候直接设置 ;setEditable();设置是否可用 ;getItemAt(int ;Index);获取对应索引下拉项
;
布局管理器
;FlowLayout流式布局 ;BorderLayout ;绝对定位布局JFrame.setLayout(null); ;setBounds(x,y,width,height);
;
事件监听器
;事件处理类所在包:java.awt.event ;事件源(组件):谁发生的 ;事件(event):发生的动作 ;事件处理者(Listener):谁时时刻刻的 ;盯着 ;事件源,注意事件源的一举一动
;
编程步骤:
;1)创建组件对象,如 ;JFrame ;jframe ;= ;new ;JFrame(); ;2)创建一个事件处理类,它实现某种监听器接口 ;class ;DoJFrame ;implements ;WindowListener{…} ;3)将监听器类对象注册到组件对象上 ;buttonOK.addActionListener(new ;DoJFrame ;()); ;或者直接组件addXXXLinseter(){}
;
常用事件
;窗口事件:WindowEvent ;对应的监听者 ;WindowListener ;点击事件:ActionEvent ;对应的监听者 ;ActionListener
;
更换java图标
;<pre> ; ; ; ;ImageIcon ;img ;= ;new ;ImageIcon("imgs/luo.png");; ; ; ;jFrame.setIconImage(img.getImage());</pre> ;