图像用户界面:AWT包中提供了3大类:容器类,ui组件类,帮助类
ui组件:文本框,复选框,单选按钮,标签,下拉列表框,按钮。
容器:实际上是component的子类,因此容器本身也是一个组件,具有组件的所有性质,另外还具有容纳其他组件和容器的功能,通过容器来组织其它界面成分和元素。
帮助类:Graphics类,布局管理类(LayoutManager):为容器设置布局管理类时,可调用容器类中的setLayout()方法。
容器分为顶层容器和非顶层容器两大类。
如何设置布局:每个容器都有一个与他相关的缺省的布局管理器。
按钮:wat提供的按钮类位button,他是从compon类直接继承而来
代码:
package com.imau.gui;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame {
private JLabel lname;
private JLabel lpass;
private JTextField tname;
private JPasswordField tpass;
private JButton login;
private void initEvnet(){
login.addActionListener(new MyListener());
}
private void init(){
lname=new JLabel("用户名");
lpass=new JLabel("密 码");
tname=new JTextField(14);
tpass=new JPasswordField(14);
login=new JButton("登录");
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(lname);
this.add(tname);
this.add(lpass);
this.add(tpass);
this.add(login);
initEvnet();
//获取屏幕大小getScrrenSize获取屏幕大小
//设置大小
Dimension dim=getToolkit().getScreenSize();
this.setResizable(false);//窗口大小不可调整
this.setTitle("QQ登录");//标题
this.setSize(250,140);//大小
this.setLocation(dim.width/2,dim.height/2);//位置
this.setVisible(true);//不可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public Login(){
init();
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("单机了按钮。。。"+tname.getText()+","+tpass.getText());
}
}
}
package com.imau.gui;
public class Text {
public static void main(String[] args){
new EventDemo();
}
}
package com.imau.gui;
import javax.swing.;
import java.awt.;
public class PanelDemo extends JFrame {
//容器
private JPanel panel;
//文本框
private JTextArea area;
//按钮
private JButton button;
//复选框
private JCheckBox box1,box2;
//单选按钮
private JRadioButton r1,r2;
//列表框
private JList list;
//下拉框
private JComboBox combobox;
//进度条
private JProgressBar progressbar;
//菜单
private JMenuBar menubar;
private JMenu menu1,menu2;
private JMenuItem item1,item2,item3,item4;
private ButtonGroup group;//单选按钮必须放进一个按钮组
//滚动面板
private JScrollPane scrollPane,scrollPane2;
private void init(){
//菜单
menubar=new JMenuBar();
menu1=new JMenu("文件");
menu2=new JMenu("编辑");
item1=new JMenuItem("新建");//菜单项
item2=new JMenuItem("关闭");
item3=new JMenuItem("复制");
item4=new JMenuItem("粘贴");
menu1.add(item1);//菜单栏
menu1.addSeparator();//添加分隔符
menu1.add(item2);
//菜单项添加到菜单栏
menu2.add(item3);
menu2.add(item4);
//菜单栏添加到工具栏
menubar.add(menu1);
menubar.add(menu2);
this.setJMenuBar(menubar);//工具栏添加到面板顶部
combobox=new JComboBox<>(new String[]{"a","b","c","d","e"});
combobox.addItem("f");//下拉列表框添加项目
//下拉列表框获取选项
//combobox.getSelectedItem();
//combobox.getSelectedIndex();
box1=new JCheckBox("运动");
box2=new JCheckBox("读书");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
group=new ButtonGroup();
//进度条:无参 五进度显示
progressbar=new JProgressBar();
//设置显示提示
progressbar.setStringPainted(true);
//设置进度
//progressbar.setValue(66);
//设置是否精确进度条false精确 true不精确
//progressbar.setIndeterminate(true);
//列表框
list= new JList(new String[]{"a","b","c","d","e"});
group.add(r1);
group.add(r2);
this.add(r1);
this.add(r2);
this.add(combobox);
this.add(progressbar);
//list设置可见几项,配合滚动面板
list.setVisibleRowCount(3);
area=new JTextArea("多行文本框",5,10);//显示文本,行数,列数
scrollPane=new JScrollPane(area);
scrollPane2=new JScrollPane(list);
this.setLayout(new FlowLayout());
this.add(scrollPane);
this.add(box1);
this.add(box2);
this.add(scrollPane2);
//r1.isSelected();//判断单选按钮是否被选中
//box1.isSelected();//多选按钮,判断是否被选中
//button=new JButton("按钮");
//panel=new JPanel();//流式布局,居中对齐
//panel.add(button);
//panel.setBackground(Color.BLUE);
//this.add(panel);
Dimension dim=getToolkit().getScreenSize();
this.setResizable(false);//窗口大小不可调整
this.setTitle("QQ登录");//标题
this.setSize(400,300);//大小
this.setLocation(dim.width/2-250/2,dim.height/2-140/2);//位置
this.setVisible(true);//不可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PanelDemo(){
init();
}
}
package com.imau.gui;
/*
先导入包
/
import java.awt.;
import javax.swing.*;
public class FrameDemo extends JFrame{
private JLabel label;
private JButton button;
private JTextField field;
private JButton bts[]=new JButton[16];
private void init(){
label=new JLabel(“姓名”);
label.setBounds(10, 10, 40, 40);
//label.setHorizontalAlignment(JLabel.CENTER);
//label.setForeground(Color.ORANGE);//标签字体颜色
//label.setLocation(10, 10);
//label.setSize(40,40);
button=new JButton("按钮");
//button.setSize(60,60);
//button.setLocation(60, 10);
//button.setEnabled(false);//是否可被点击
for(int i=0;i
}
public FrameDemo(){
init();
this.add(label);//添加标签
this.add(field);//添加文本框
//this.add(button);//添加按钮
//for(int i=0;i
}
//用对象形式创建
public void test(){
JFrame frame= new JFrame();
frame.setTitle(“这是一个窗口”);
}
}
package com.imau.gui;
import java.awt.;
import javax.swing.;
import java.awt.event.*;
public class EventDemo extends JFrame {
/*private JTextField tf;
private JTextField tf2;
private JPanel panel;*/
/*private void initEvent(){
tf.addFocusListener(new MyListener());
tf.addKeyListener(new MyListener());
panel.addMouseListener(new MyListener());
panel.addMouseMotionListener(new MyListener());
}*/
private JButton button;
//绘图方法:画一次
@Override
public void paint(Graphics arg0) {
arg0.setColor(Color.PINK);
arg0.setFont(new Font("宋体",Font.BOLD,30));
/*//绘制字
arg0.drawString("hello", 30, 100);
//绘制直线
arg0.drawLine(30, 110, 100, 110);
//绘制空心长方体
arg0.drawRect(30, 120, 30, 60);
//绘制实心长方体
arg0.fillRect(30, 190, 30, 60);*/
}
//重新绘制
@Override
public void repaint() {
//调用paint方法
}
public EventDemo(){
init();
}
private void init(){
//添加图片
/*ImageIcon icon=new ImageIcon("image/logo.png");
button=new JButton("按钮",icon);
this.add(button);*/
/*panel=new JPanel();
tf=new JTextField(10);
tf2=new JTextField(10);
this.add(tf);
this.add(tf2);
this.add(panel);
panel.setSize(200,200);
panel.setBackground(Color.blue);*/
this.setLayout(new FlowLayout(FlowLayout.CENTER));
//获取屏幕大小getScrrenSize获取屏幕大小
//设置大小
Dimension dim=getToolkit().getScreenSize();
//initEvent();
this.setResizable(false);//窗口大小不可调整
this.setTitle("事件案例");//标题
this.setSize(500,300);//大小
this.setLocation(dim.width/2,dim.height/2);//位置
this.setVisible(true);//不可见
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*class MyListener implements FocusListener,KeyListener,MouseListener,MouseMotionListener{
@Override
public void focusGained(FocusEvent arg0) {
System.out.println("aa");
}
@Override
public void focusLost(FocusEvent arg0) {
//getCompoent() 获取当前组件对象
//requestFocus() 重新获得焦点
if(tf.getText().length()<=8){
System.out.print("必须8位");
tf.requestFocus();
}
}
@Override
public void keyPressed(KeyEvent e) {
//e.getKeyChar()打印键盘按键
//e.getKeyCode()打印键盘按键Ascii码
System.out.println("按下键盘"+e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("释放键盘");
}
@Override
public void keyTyped(KeyEvent e) {
}
//e.getButton点击左键还是右键左键:1 右键:3 滑轮:2
//e.getx e.gety获取坐标
@Override//按住鼠标拖拽
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//鼠标移动
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//单击鼠标
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//鼠标进入
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//鼠标移出
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//按下鼠标
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override//释放鼠标
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}*/
}