GUI编程
组件
- 窗口
- 弹窗
- 面板
- 文本框
- 列表框
- 按钮
- 图片
- 监听事件
- 鼠标
- 键盘事件
- 破解工具(idea 破解)
1. 简介
- Gui核心技术:Swing,AWT(界面不美观,jre几百兆,可能几兆的游戏就得下个jre)
但是可以了解MVC架构和监听!
2.AWT
-
AWT介绍
-
包含很多类和接口
-
元素:窗口,按钮,文本框
-
组件 component(button,textarea,label…)
-
容器Container(Window-frame和dialog)(panel面板-applet)
-
2.1组件和容器
1.frame
//frame
public class TestFrame{
public static void main(String[] args){
//frame.jdk
Frame frame=new Frame("图形界面窗口");
//设置可见性
frame.setVisiable(true);
//大小
frame.setSize(400,400);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//弹出窗的位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
}
}
2.panel
//panel
public class TestPanel{
public static void main(String[] args){
//frame.jdk
Frame frame=new Frame("图形界面窗口");
Panel panel=new Panel();
//设置布局
frame.setLayout(null);
frame.setBounds(300,300,500,500);
//设置背景颜色
frame.setBackground(new Color(85,150,68));
//PANEL设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(193,15,60));
//讲panel加入到frame中
frame.add(panel);
//设置可见性
frame.setVisiable(true);
//监听事件,监听窗口关闭
//适配器模式
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
})
}
}
3.布局管理器
-
流式布局
public class test2 { public static void main(String[] args) { Frame frame=new Frame(); //组件按钮 Button bt1=new Button(); Button bt2=new Button(); Button bt3=new Button(); //设置为流式布局 frame.setLayout(new FlowLayout()); frame.setSize(200,200); frame.add(bt1); frame.add(bt2); frame.add(bt3); } }
-
东西南北中
public class test2 { public static void main(String[] args) { Frame frame=new Frame(); //组件按钮 Button bt1=new Button("1"); Button bt2=new Button("2"); Button bt3=new Button("3"); frame.setSize(200,200); frame.add(bt1,BorderLayout.WEST); frame.add(bt2,BorderLayout.EAST); frame.add(bt3,BorderLayout.NORTH); } }
-
表格布局
public class test2 { public static void main(String[] args) { Frame frame=new Frame(); //组件按钮 Button bt1=new Button("1"); Button bt2=new Button("2"); Button bt3=new Button("3"); //设置为流式布局 frame.setLayout(new GridLayout(3,1)); frame.setSize(200,200); frame.add(bt1); frame.add(bt2); frame.add(bt3); frame.setVisible(true); frame.pack();//选一个最优位置 } }
4.事件监听
public class test2 {
public static void main(String[] args) {
Frame frame=new Frame();
//组件按钮
Button bt1=new Button("1");
bt1.addActionListener(new MyAcitonListener());
frame.add(bt1,BorderLayout.CENTER);
frame.setVisible(true);
frame.pack();//选一个最优位置
windowClose(frame);
}
private static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
});
}
}
class MyAcitonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("1111");
}
}
public class test2 {
public static void main(String[] args) {
Frame frame=new Frame();
//组件按钮
Button bt1=new Button("start");
Button bt2=new Button("stop");
bt1.setActionCommand("bt2-stop");
bt1.addActionListener(new MyAcitonListener());
bt2.addActionListener(new MyAcitonListener());
frame.add(bt1,BorderLayout.NORTH);
frame.add(bt1,BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();//选一个最优位置
}
}
class MyAcitonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
e.getActionCommand();//bt2-stop 默认值是label里面对应的值
}
}
5.输入框监听
public class test2 {
public static void main(String[] args) {
new Myframe();
}
}
class Myframe extends Frame{
public Myframe(){
TextField textField = new TextField();
add(textField);
//监听文本框的数字
NumberListener numberListener = new NumberListener();
textField.addActionListener(numberListener);
//设置替换编码
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class NumberListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField field= (TextField) e.getSource();//获取资源
System.out.print(field);
}
}
6.画笔
public class test2 {
public static void main(String[] args) {
new Myframe().loadPaint();
}
}
class Myframe extends Frame{
public void loadPaint(){
setVisible(true);
setBounds(200,200,500,500);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.drawOval(100,100,100,100);
}
}
7.鼠标监听
public class test2 {
public static void main(String[] args) {
new Myframe("paint");
}
}
class Myframe extends Frame {
//画画需要画笔和监听鼠标的位置,还需要存储对应的点
//存鼠标点击的点
private ArrayList ponit;
public Myframe(String title) throws HeadlessException {
super(title);
setBounds(200, 200, 500, 500);
ponit = new ArrayList();
setVisible(true);
//鼠标监听器
this.addMouseListener(new myMouseListener());
}
@Override
public void paint(Graphics g) {
ponit.forEach(e->{
Point point= (Point) e;
g.setColor(Color.blue);
g.fillOval(point.x,point.y,5,5);
});
}
private class myMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
Myframe myframe = (Myframe) e.getSource();
//当前的点
myframe.addPoint(new Point(e.getX(), e.getY()));
//每次点击都重新画
myframe.repaint();
}
}
public void addPoint(Point point){
ponit.add(point);
}
}
8.窗口监听
public class test2 {
public static void main(String[] args) {
new WindowFrame("paint");
}
}
class WindowFrame extends Frame {
public WindowFrame(String name) {
setVisible(true);
setBackground(Color.black);
setBounds(100,100,100,100);
addWindowListener(new MyWindowListener());
}
class MyWindowListener extends WindowAdapter{
@Override
public void windowClosing(WindowEvent e) {
setVisible(false); //隐藏
System.exit(0);//正常退出
}
}
}
9.键盘监听
public class test2 {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame {
public KeyFrame() throws HeadlessException {
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();//获取按键码
if (keyCode == KeyEvent.VK_UP){
System.out.print("上");
}
}
});
}
}
3.SWING
JFrame extends Frame
public class test2 {
public void init(){
JFrame frame=new JFrame("顶级窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
JLabel label=new JLabel("文本标签");
frame.add(label);
Container contentPane = frame.getContentPane();//获得容器
contentPane.setBackground(Color.red);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new test2().init();
}
}
JDialog,Icon,ImageIcon,