JAVASE——全面总结JAVA中的GUI编程

网友投稿 719 2022-05-29

前言

万字深入总结Java中的swing编程

@[toc]

1.简介

GUI的核心技术

Swing和AWT

界面不美观

需要jre环境!

可以写出自己心中想要的小工具

工作的时候,也可能需要维护到Swing界面,概率极小!

了解MVC架构,了解监听!

组件:

窗口

弹出

面板

文本框

列表框

按钮

图片

监听事件

鼠标事件

键盘事件

2、AWT

new 类!

包含了很多类和接口!

用于GUI编程图像用户界面

元素:窗口、按钮、文本框

java.awt包

单窗口示例

public static void main(String[] args) { Frame frame = new Frame("我的第一个Java图形界面窗口"); //设置窗口可见性 frame.setVisible(true); //设置窗口大小 frame.setSize(400,400); //设置窗口颜色 frame.setBackground(Color.CYAN); //设置窗口的出现位置 frame.setLocation(300,300); //是否可调整大小 frame.setResizable(false); }

多窗口示例

public class OneFrame { public static void main(String[] args) { MyFrame l1 = new MyFrame(100, 100, 200, 200, Color.yellow); MyFrame l2 = new MyFrame(300, 100, 200, 200, Color.CYAN); MyFrame l3 = new MyFrame(100, 300, 200, 200, Color.black); MyFrame l4 = new MyFrame(300, 300, 200, 200, Color.blue); } } class MyFrame extends Frame { static int n = 0; public MyFrame(int x, int y, int w, int h, Color color) { super("l" + (n++)); setVisible(true); setBounds(x, y, w, h); setBackground(color); setResizable(false); } }

public static void main(String[] args) { Frame frame = new Frame(); Panel panel = new Panel(); //设置布局 frame.setLayout(null); //坐标 frame.setBounds(100,100,100,100); frame.setBackground(new Color(40,40,40)); //panel设置坐标,相对于frame panel.setBounds(50,50,50,50); panel.setBackground(new Color(199,199,199)); frame.add(panel); frame.setVisible(true); //监听事件,监听创口关闭事件,System.exit(0);适配器模式 frame.addWindowListener(new WindowAdapter() { //窗口点击关闭的时候需要做的事情 public void windowClosing(WindowEvent e) { System.exit(0); } }); }

流式布局

public static void main(String[] args) { Frame frame = new Frame("FlowLayout"); //组件——>按钮 Button button1 = new Button("Button1"); Button button2 = new Button("Button2"); Button button3 = new Button("Button3"); //设置为流式布局 frame.setLayout(new FlowLayout());//默认居中 frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左 frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右 frame.setSize(400,400); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); }

东西南北中布局

public static void main(String[] args) { Frame frame = new Frame("BorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); //自动选择 frame.pack(); frame.setVisible(true); }

表格布局

public static void main(String[] args) { Frame frame = new Frame("GridLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack();//Java函数! 自动选择一个最优的位置来确定 frame.setVisible(true); }

多种布局结合:

public static void main(String[] args) { Frame frame = new Frame(); frame.setVisible(true); frame.setBackground(Color.WHITE); frame.setBounds(600,600,200,200); frame.setLayout(new GridLayout(2,1)); Panel panel1 = new Panel(new BorderLayout()); Panel panel2 = new Panel(new GridLayout(2,1)); Panel panel3 = new Panel(new BorderLayout()); Panel panel4= new Panel(new GridLayout(2,2)); panel1.add(new Button("East"),BorderLayout.EAST); panel1.add(new Button("West"),BorderLayout.WEST); panel2.add(new Button("panel2")); panel2.add(new Button("panel2")); panel1.add(panel2,BorderLayout.CENTER); panel3.add(new Button("East"),BorderLayout.EAST); panel3.add(new Button("West"),BorderLayout.WEST); for(int i=0;i<4;i++){ panel4.add(new Button("panel4")); } panel3.add(panel4,BorderLayout.CENTER); frame.add(panel1); frame.add(panel3); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); }

总结:

Frame是一个顶级窗口

Panel 无法单独显示,必须添加到某个容器中

布局管理器

流式布局

东西南北中布局

表格式布局

public static void main(String[] args) { //按下按钮,触发一些事件 Frame frame = new Frame(); Button button = new Button(); //因为addActionListener()需要addActionListener 所以我们需要构造一个addActionListener MyActionListener myActionListener = new MyActionListener(); button.addActionListener(myActionListener); frame.add(button, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); WindownClose(frame);//调用关闭窗口 } //关闭窗口事件 private static void WindownClose(Frame frame) { frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } //事件监听 class MyActionListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { System.out.println("66666!"); } }

public static void main(String[] args) { //两个按钮实现同一个监听 //开始----停止 Frame frame = new Frame("开始-停止"); Button button1 = new Button("start"); Button button2 = new Button("stop"); //可以显示的定义触发会返回命令,如果不显示定义,则会走默认的值 //可以多个按钮只写一个监听类 button2.setActionCommand("stop"); MyMonitor myMonitor = new MyMonitor(); button1.addActionListener(myMonitor); button2.addActionListener(myMonitor); frame.add(button1,BorderLayout.NORTH); frame.add(button2,BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } class MyMonitor implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //e.getActionCommand()获取按钮上的信息 System.out.println("按钮被点击了:msg=>"+ e.getActionCommand()); if(e.getActionCommand().equals("start")){ } } }

public static void main(String[] args) { //main里只管启动 new MyFrame2(); } class MyFrame2 extends Frame{ public MyFrame2(){ TextField textField = new TextField(); //因为extends Frame 所有Frame可以不用写 add(textField); //监听这个文本框输入的文字 MyActionListener2 myActionListener2 = new MyActionListener2(); //按钮下回车就会触发这个输入框的事件 textField.addActionListener(myActionListener2); //设置编码 textField.setEchoChar('*');//密码框 setVisible(true); pack(); } } class MyActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField field=(TextField)e.getSource();//获得一些资源,返回一个对象 System.out.println(field.getText());//获得输入框文本 field.setText""); //不能NULL } }

面向过程

public static void main(String[] args) { new Calculator(); } } //计算器类 class Calculator extends Frame{ public Calculator() { //3个文本框 TextField num1 = new TextField(10);//字符数 TextField num2 = new TextField(10);//字符数 TextField num3= new TextField(20);//字符数 //1个按钮 Button button = new Button("="); //监听 button.addActionListener(new MyCalculatorListener(num1,num2,num3 )); //1个标签 Label label = new Label("+"); //布局 setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //-类 class MyCalculatorListener implements ActionListener{ //获取三个变量 private TextField num1,num2,num3;//需要在这也写这个 public MyCalculatorListener( TextField num1, TextField num2, TextField num3){ this.num1 = num1; this.num2 = num2; this.num3 = num3; } public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 int n1 = Integer.parseInt(num1.getText());//将字符串类型转换为int类型 int n2 = Integer.parseInt(num2.getText()); //2.将这个值加法运算后放入第三个框中 num3.setText(""+(n1+n2)); //3.清除前两个框的内容 num1.setText(""); num2.setText(""); } }

面向对象

public static void main(String[] args) { new Calculator().loadFrame(); } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; //方法 public void loadFrame(){ num1 = new TextField(10); num2 = new TextField(10); num3= new TextField(20); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener(this)); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } } //-类 class MyCalculatorListener implements ActionListener{ //获取计算器这个对象,在一个类中组合另外一个类 Calculator calculator=null; public MyCalculatorListener(Calculator calculator){ this.calculator=calculator; } @Override public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 int n1=Integer.parseInt(calculator.num1.getText()); int n2=Integer.parseInt(calculator.num2.getText()); //2.将这个值加法运算后放入第三个框中 calculator.num3.setText(""+(n1+n2)); //3.清除前两个框的内容 calculator.num1.setText(""); calculator.num2.setText(""); } }

内部类:更好的包装

public static void main(String[] args) { new Calculator().loadFrame(); } //计算器类 class Calculator extends Frame{ //属性 TextField num1,num2,num3; //方法 public void loadFrame(){ num1 = new TextField(10); num2 = new TextField(10); num3= new TextField(20); Button button = new Button("="); Label label = new Label("+"); button.addActionListener(new MyCalculatorListener()); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add(num3); pack(); setVisible(true); } //-类 //内部类最大的好处就是可以畅通无阻的访问外部的属性和方法 private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { //1.获得加数和被加数 int n1=Integer.parseInt(num1.getText()); int n2=Integer.parseInt(num2.getText()); //2.将这个值加法运算后放入第三个框中 num3.setText(""+(n1+n2)); //3.清除前两个框的内容 num1.setText(""); num2.setText(""); } } }

public static void main(String[] args) { new MyPaint().loadFrame(); } class MyPaint extends Frame{ public void loadFrame(){ setBounds(200,200,600,400); setVisible(true); } //画笔 @Override public void paint(Graphics g) { //画笔,需要有颜色,可以画画 g.setColor(Color.red); g.drawOval(100,100,100,100); g.fillOval(100,100,100,100);//实心圆 g.setColor(Color.green); g.fillRect(150,150,100,100);//四边形 //画笔用完,还原到最初的颜色 } }

public static void main(String[] args) { new MyFrame("画图"); } class MyFrame extends Frame { //画画需要画笔,需要监听鼠标当前的位置,需要集合来存储画出来的东西 ArrayList points; public MyFrame(String title) { super(title); setBounds(200, 200, 400, 400); //存储鼠标点击的点 points = new ArrayList<>(); setVisible(true); //鼠标-,针对窗口 this.addMouseListener(new MyMouseListener()); } //画画需要重写画笔 @Override public void paint(Graphics g) { //画画、需要监听鼠标事件 Iterator iterator = points.iterator(); while (iterator.hasNext()) { Point point = (Point) iterator.next(); g.setColor(Color.red); g.fillOval(point.x, point.y, 10, 10); } } //添加点到界面上 public void addPaint(Point point){ points.add(point); } //适配器模式 private class MyMouseListener extends MouseAdapter { //鼠标按下 弹起 按住不放 public void mousePressed(MouseEvent e) { MyFrame frame = (MyFrame) e.getSource(); //这里我们点击的时候,就会在界面上产生一个点-->画 frame.addPaint(new Point(e.getX(), e.getY())); //这个点就是鼠标的点 //每次点击鼠标 都需要重写画一遍 frame.repaint();//刷新 } } }

//鼠标监听事件 public static void main(String[] args) { new WindowFrame(); } class WindowFrame extends Frame { public WindowFrame() { setBounds(100, 100, 200, 200); setBackground(Color.GREEN); setVisible(true); //addWindowListener(new MyWindownListener()); this.addWindowListener( //匿名内部类 new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); } //关闭窗口 @Override public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); System.exit(0); } @Override public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); } //激活窗口 @Override public void windowActivated(WindowEvent e) { WindowFrame source = (WindowFrame) e.getSource(); source.setTitle("被激活了"); System.out.println("windowActivated"); } } ); } }

public static void main(String[] args) { new KeyFrame(); } class KeyFrame extends Frame{ public KeyFrame() { setBounds(1, 2, 300, 400); setVisible(true); this.addKeyListener(new KeyAdapter() { //键盘按下 @Override public void keyPressed(KeyEvent e) { //键盘按下的键是哪一个 int keyCode = e.getKeyCode();//获取当前键盘的码 System.out.println(keyCode); //不需要记住,直接使用VK_XXX if (keyCode==KeyEvent.VK_UP){ System.out.println("你按下了上键"); } //根据按下的键,产生不同的结果 } }); } }

3、Swing

public static void main(String[] args) { new MyJFrame2().init(); } class MyJFrame2 extends JFrame{ public void init(){ this.setBounds(100,100,200,200); this.setVisible(true); JLabel label = new JLabel("欢迎学习Java"); this.add(label); //让文本标签居中 设置水平对齐 label.setHorizontalAlignment(SwingConstants.CENTER); //获得一个容器 Container content = this.getContentPane(); content.setBackground(Color.cyan); } }

JDialog,用来被弹出,默认就有关闭窗口事件

public class DialogDemo extends JFrame{ public DialogDemo() { this.setVisible(true); this.setSize(700,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //JFrame 放东西---需要容器 Container container = this.getContentPane(); //绝对布局 container.setLayout(null); //按钮 JButton button = new JButton("点击弹出对话框"); button.setBounds(30,30,200,50); //点击这个按钮的时候,弹出一个弹窗(监听) button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //弹窗 new MyDailogDemo(); } }); container.add(button); } public static void main(String[] args) { new DialogDemo(); } //弹窗的窗口 class MyDailogDemo extends JDialog{ public MyDailogDemo() { this.setVisible(true); this.setBounds(100,100,500,500); //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 默认已有这个功能 Container container = this.getContentPane(); //设置为决定定位 container.setLayout(null); container.add(new Label("欢迎学习Java!")); } }

public class ImageIconDemo extends JFrame { public ImageIconDemo(){ //获取图片地址 JLabel label = new JLabel("ImageIcon"); URL url = ImageIconDemo.class.getResource("tx.png"); ImageIcon imageIcon = new ImageIcon(url);//命名不要重复 label.setIcon(imageIcon); label.setHorizontalAlignment(SwingConstants.CENTER); Container container = getContentPane(); container.add(label); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setBounds(100,100,200,200); } public static void main(String[] args) { new ImageIconDemo(); } }

JPanel

public class JPanelDemo extends JFrame { public JPanelDemo(){ //所有的东西放在container里边 Container container = this.getContentPane(); container.setLayout(new GridLayout(2,1,10,10));//后面参数的意思是间距 JPanel panel1 = new JPanel(new GridLayout(1,3)); JPanel panel2 = new JPanel(new GridLayout(1,2)); JPanel panel3 = new JPanel(new GridLayout(2,1)); JPanel panel4 = new JPanel(new GridLayout(3,2)); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel1.add(new JButton("1")); panel2.add(new JButton("2")); panel2.add(new JButton("2")); panel3.add(new JButton("3")); panel3.add(new JButton("3")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); panel4.add(new JButton("4")); container.add(panel1); container.add(panel2); container.add(panel3); container.add(panel4); this.setVisible(true); this.setSize(500,500); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JPanelDemo(); } }

JScrollPanel:

public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,20); textArea.setText("欢迎学习Java"); //Scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }

图片按钮

public class JButtonDemo extends JFrame { public JButtonDemo(){ Container container = this.getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo.class.getResource("tx.png"); Icon icon = new ImageIcon(resource); //把这个图片放在按钮上 JButton button = new JButton(); button.setIcon(icon); button.setToolTipText("图片按钮"); //add container.add(button); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo(); } }

单选按钮

public class JButtonDemo02 extends JFrame { public JButtonDemo02(){ Container container = this.getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo.class.getResource("tx.png"); Icon icon = new ImageIcon(resource); //单选框 JRadioButton radiobutton01 = new JRadioButton("单选框1"); JRadioButton radiobutton02 = new JRadioButton("单选框2"); JRadioButton radiobutton03 = new JRadioButton("单选框3"); //由于单选框只能选择一个,需要分组---一个组中只能选择一个 ButtonGroup ground = new ButtonGroup(); ground.add(radiobutton01); ground.add(radiobutton02); ground.add(radiobutton03); container.add(radiobutton01,BorderLayout.CENTER); container.add(radiobutton02,BorderLayout.NORTH); container.add(radiobutton03,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo02(); } }

复选按钮

public class JButtonDemo03 extends JFrame { public JButtonDemo03(){ Container container = this.getContentPane(); //将一个图片变为图标 URL resource = JButtonDemo.class.getResource("tx.png"); Icon icon = new ImageIcon(resource); //多选框 JCheckBox checkBox01 = new JCheckBox(" checkBox01"); JCheckBox checkBox02 = new JCheckBox(" checkBox02"); container.add(checkBox01,BorderLayout.NORTH); container.add(checkBox02,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JButtonDemo03(); } }

下拉框

public class TestComboboxDemo01 extends JFrame { public TestComboboxDemo01(){ Container container = this.getContentPane(); JComboBox status = new JComboBox<>(); status.addItem(null); status.addItem("正在热映"); status.addItem("已下架"); status.addItem("即将上映"); container.add(status); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo01(); } }

列表框

public class TestComboboxDemo02 extends JFrame { public TestComboboxDemo02() { Container container = this.getContentPane(); //生成列表内容 //String[] contents={"1","2","3"}; Vector contents = new Vector(); //列表中需要放入内容 JList jList = new JList(contents); contents.add("1"); contents.add("2"); contents.add("3"); container.add(jList); this.setVisible(true); this.setSize(500,300); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestComboboxDemo02(); } }

应用场景:

选择地区或者一些单个选项

列表,展示信息,一般是动态内容

文本框

public class TestTextDemo01 extends JFrame { public TestTextDemo01 (){ Container container = this.getContentPane(); //container.setLayout(null);//绝对布局 JTextField textField = new JTextField("hellow"); JTextField textField2 = new JTextField("world",20); container.add(textField,BorderLayout.NORTH); container.add(textField2,BorderLayout.SOUTH); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo01 (); } }

JAVASE——全面总结JAVA中的GUI编程

密码框

public class TestTextDemo02 extends JFrame { public TestTextDemo02 (){ Container container = this.getContentPane(); //密码框 JPasswordField passwordField = new JPasswordField();//**** passwordField.setEchoChar('*'); container.add(passwordField); this.setVisible(true); this.setSize(500,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new TestTextDemo02 (); } }

文本域

public class JScrollDemo extends JFrame { public JScrollDemo(){ Container container = this.getContentPane(); //文本域 JTextArea textArea=new JTextArea(20,20); textArea.setText("欢迎学习Java"); //Scroll面板 JScrollPane scrollPane = new JScrollPane(textArea); container.add(scrollPane); this.setVisible(true); this.setBounds(100,100,300,350); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } public static void main(String[] args) { new JScrollDemo(); } }

GUI Java 容器

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用java爬取杭电oj已ac代码
下一篇:今天,四项可信云最佳实践奖!
相关文章