AWT:

1 创建框架

2 加入容器

3 容器里添加布局方式

      

Frame frame=new Frame("AWT窗口");  //创建Frame框架
Container con=new Container(); //获得窗口的底层容器
con.setLayout(new FlowLayout());//容器里的布局方式
Button btn=new Button("确定");//生成按钮
con.add(btn);
Button btn1=new Button("取消");
con.add(btn1);

frame.add(con);
       frame.setSize(300,300);//设置窗口大小
       frame.setVisible(true);//设置窗口是否可见,默认不可见
       frame.setLocation(200, 200);//设置窗口当前位置



 类继承:extends Applet,  Applet是java编写的一些小应用程序,嵌入在页面中支持浏览器解释执行,比如简易的计算器,网页的广告

//在程序加载的时候自动调用
@Override
public void init() {
Button btn=new Button("确定");
this.add(btn);
super.init();
}


//程序启动的时候调用
@Override
public void start() {

super.start();
}


//程序停止的时候调用
@Override
public void stop() {

super.stop();
}


//程序销毁的时候调用
@Override
public void destroy() {

super.destroy();
}



Swing:控件前面加J

 //Jframe继承Frame,支持Swing组件

JFrame jframe=new JFrame();
//设置容器
Container con=jframe.getContentPane();
//设置布局
con.setLayout(new FlowLayout());

JButton

JButton btn=new JButton("确定");
btn.setText("设置文字");
String btnText=btn.getText();
//设置按钮不可用
//btn.setEnabled(false);
//设置按钮文字颜色
btn.setForeground(Color.blue);
con.add(btn);




JLabel
JLabel lab=new JLabel("lala");
lab.setForeground(Color.red);
con.add(lab);



文本行
JTextField field=new JTextField(5);
field.setText("文本行");
//设置对其方式
field.setHorizontalAlignment(JTextField.RIGHT);
//不能编辑
field.setEditable(false);
con.add(field);



密码框
JPasswordField pwd=new JPasswordField(2);
//设置回写字符,输入什么都是同一个字符
pwd.setEchoChar('6');
//获得密码框中的内容,得到的是char[]
String pw=pwd.getPassword().toString();
con.add(pwd);



文本域
JTextArea area=new JTextArea(3,5);//三行两列
//设置文本域自动换行
area.setLineWrap(true);
//加滚动条,创建滚动面板
JScrollPane scrol=new JScrollPane(area);
//滚动条一直出现
scrol.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
con.add(scrol);



单选按钮
JRadioButton rb1=new JRadioButton("男");
JRadioButton rb2=new JRadioButton("女");
//添加逻辑分组
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);
bg.add(rb2);
con.add(rb2);
con.add(rb1);



多选框
JCheckBox box1=new JCheckBox("篮球");
JCheckBox box2=new JCheckBox("足球");
JCheckBox box3=new JCheckBox("棒球");
con.add(box1);
con.add(box2);
con.add(box3);



下拉列表
String[] pro={"杭州","北京","上海"};
JComboBox cb=new JComboBox(pro); 
//设置初始显示的值
// cb.setSelectedIndex(1);
//设置初始显示的值
cb.setSelectedItem("上海");
//增加选项
cb.addItem("深圳");
//获得当前选项的值
String value=cb.getSelectedItem().toString();
System.out.println(value);
con.add(cb);



菜单栏
JMenuBar bar=new JMenuBar();
//创建菜单项,如果有下一集子菜单用JMenu,没有则用JMenuItem
JMenu menu=new JMenu("File");
JMenu newP=new JMenu("New");
JMenuItem jp=new JMenuItem("java project");
JMenuItem close=new JMenuItem("close");

newP.add(jp);
menu.add(newP);
//添加分隔符
menu.addSeparator();
menu.add(close);
bar.add(menu);
//添加菜单栏
jframe.setJMenuBar(bar);




设置表格
创建表格模型
Object[] obj={"学号","姓名","年龄","成绩"};
DefaultTableModel model=new DefaultTableModel(obj,0);
//通过制定表格模型创建表格
JTable table=new JTable(model);
JScrollPane pane=new JScrollPane(table);
//往表格末尾添加数据
Object[] rowDate1={"1001","蝴蝶","111","79"};
Object[] rowDate2={"1002","萌萌","221","71"};
Object[] rowDate3={"1003","拉拉","124","89"};
Object[] rowDate4={"1004","醉醉","121","76"};

model.addRow(rowDate1);
model.addRow(rowDate2);
model.addRow(rowDate3);
model.addRow(rowDate4);
//获得指定单元格数据
Object oo=model.getValueAt(2, 3);
//修改指定单元格内容
model.setValueAt("浩浩", 1, 1);
//获得行数
int rows=table.getRowCount();
int col=table.getColumnCount();
//删除行,根据行的索引来删除
//model.removeRow(0);
//删除所有
//model.setRowCount(0);
for(int i=0;i<rows;i++){
model.removeRow(0);
}
con.add(pane);



对话框
//普告知用户某事已发声,: 第一个是frame,第二个是参数消息
JOptionPane.showMessageDialog(null,"你已死亡");
JOptionPane.showMessageDialog(null, "你已死亡", "提示", JOptionPane.INFORMATION_MESSAGE);

//对话框,只询问一个确认问题:JOptionPane.YES_NO_OPTION  ; 
//返回:是-0,否-1,取消-2
JOptionPane.showConfirmDialog(null,"确定吗?","请选择",JOptionPane.YES_NO_CANCEL_OPTION);
//输入框,提示要求某些输入
String name=JOptionPane.showInputDialog("请输入姓名:");
//上述三项的大统一:showOptionDialog




//文件选择框
JFileChooser chooser = new JFileChooser();
		// 打开文件选择框,null为默认
		chooser.showOpenDialog(null);
		// 获得选中的文件
		File file = chooser.getSelectedFile();
		// 保存文件选择框
		chooser.showSaveDialog(null);
		File file1 = chooser.getSelectedFile();
		if (!file1.exists()) {
			try {
				file1.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}



j
JFileChooser chooser = new JFileChooser();
		// 打开文件选择框,null为默认
		chooser.showOpenDialog(null);
		// 获得选中的文件
		File file = chooser.getSelectedFile();
		// 保存文件选择框
		chooser.showSaveDialog(null);
		File file1 = chooser.getSelectedFile();
		if (!file1.exists()) {
			try {
				file1.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

布局:

JFrame frame=new JFrame("布局");
Container con=frame.getContentPane();
//流布局
con.setLayout(new FlowLayout());
con.setLayout(new FlowLayout(FlowLayout.LEFT));//左对齐
con.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));//对其方式和间隙

//网格布局
con.setLayout(new GridLayout(5,5));//五行五列
con.setLayout(new GridLayout(4,5,5,5));//四行五列,间隙
for (int i = 0; i <10;i++) {
JButton btn=new JButton("按钮"+i);
con.add(btn);
}

//边界布局
con.setLayout(new BorderLayout());

JButton btn1=new JButton("东");
JButton btn2=new JButton("南");
JButton btn3=new JButton("西");
JButton btn4=new JButton("北");
JButton btn5=new JButton("中");

//中间区域没有组件会被空出来,其他区域没有组件则被填充
con.add(btn1,BorderLayout.EAST);
con.add(btn2,BorderLayout.SOUTH);
con.add(btn3,BorderLayout.WEST);
con.add(btn4,BorderLayout.NORTH);
// con.add(btn5,BorderLayout.CENTER);

//添加面板组件,默认流式布局,剧中对其
JPanel pan=new JPanel();
//也可以设置布局
pan.setLayout(new FlowLayout(FlowLayout.RIGHT));
pan.add(btn5);
con.add(pan,BorderLayout.CENTER);

frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


————————————————

事件

事件监听器:ActionListener

1 实现接口  ActionListener

2 实现方法:

@Override
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
System.out.println(string);
if (string.equals("确定")) {

}else {

}
}



鼠标监听事件:

1 实现接口:MouseListener

2  实现方法:

@Override
public void mouseClicked(MouseEvent e) {


}


@Override
public void mousePressed(MouseEvent e) {

System.out.println("我被按住");
int i=e.getButton();//左键是1,右键是3,滚轮是2
int n=e.getClickCount();//点击的次数
Point p=e.getPoint();
System.out.println(p);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}


鼠标事件的抽象适配器:MouseAdapter

1 继承接口MouseAdapter

2  虽然是一个接口,但没有必须实现的方法,可以根据需要重写MouseListener里面的方法,MouseAdapter是继承MouseAdapter接口


键盘监听事件: KeyListener

1  创建一个类实现接口KeyListener

class MyActionKey implements KeyListener

2  实现里面的方法:

@Override
public void keyTyped(KeyEvent e) {

char c=e.getKeyChar();
System.out.println(c);
}
@Override
public void keyPressed(KeyEvent e) {


}
@Override
public void keyReleased(KeyEvent e) {
}



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐