Swing

3、Swing

3.1 窗口、面板
package com.wang.swing.jframe;

import javax.swing.*;
import java.awt.*;

//
public class JFrameDemo01 {

   //现在窗口是被初始化出来的,要有init方法
   public void init(){
       //顶级窗口
       JFrame jFrame = new JFrame("这是一个JFrame窗口");
       jFrame.setVisible(true);
       jFrame.setBounds(100,100,200,200);

       JLabel jLabel = new JLabel("欢迎学java");
       jFrame.add(jLabel);

       //关闭事件 已经写好的方法
       jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
   public static void main(String[] args) {
       //建立一个窗口
       new JFrameDemo01().init();

       new MyJFrame().init();
  }
}
class MyJFrame extends JFrame{
   public void init(){
       this.setBounds(10,10,200,300);
       this.setVisible(true);
       //获得容器
       Container contentPane = this.getContentPane();
       contentPane.setBackground(Color.green);

       //设置文字 JLabel
       JLabel jLabel = new JLabel("欢迎学java");
       this.add(jLabel);

       //让文本标签居中
       jLabel.setHorizontalAlignment(SwingConstants.CENTER);
  }
}
3.2 弹窗
package com.wang.swing.dialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo01 extends JFrame {
   public static void main(String[] args) {
       new DialogDemo01().init();
  }
   //JFrame要写init方法
   public void init(){
       this.setVisible(true);
       this.setSize(700,500);

       //关闭事件
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       //JFrame中放东西需要容器
       Container container = this.getContentPane();
       //绝对布局
       container.setLayout(null);

       //需要按钮
       JButton jButton = new JButton("点击弹出对话框");
       jButton.setBounds(30,30,200,50);

       //点击按钮的时候,弹出弹窗,所以需要监听事件
       jButton.addActionListener(new ActionListener() {//监听器
           @Override
           public void actionPerformed(ActionEvent e) {
               //弹窗
               new MyDialog();
          }
      });

       container.add(jButton);
  }
}

//弹窗的窗口,弹窗也是一个新的 窗口
class MyDialog extends JDialog{
   public MyDialog(){
       this.setVisible(true);
       this.setBounds(100,100,500,500);
       //默认有关闭事件,不需要写 会报错 多余

       Container container = this.getContentPane();
       //container.setLayout(null); 有这句时"一起学java"显示不出来,原因??

       container.add(new JLabel("一起学java"));
  }
}
3.3 标签

label

图标 ICON

package com.wang.swing.lable;

import javax.swing.*;
import java.awt.*;

//图标是接口,需要实现类,是一个弹窗,所以要继承JFrame
public class IconDemo extends JFrame implements Icon {

   private int width;
   private int height;
   public IconDemo(){
       //无参构造
  }
   public IconDemo(int width,int height){
       this.width=width;
       this.height=height;
  }
   public static void main(String[] args) {
       new IconDemo().init();
  }

   public void init(){
       //类初始化
       IconDemo iconDemo = new IconDemo(15, 15);

       //图标放在标签上,也可以放在按钮上
       JLabel jLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);

       //标签要用要放进容器
       Container container = getContentPane();
       container.add(jLabel);

       this.setVisible(true);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   @Override
   public void paintIcon(Component c, Graphics g, int x, int y) {
       g.fillOval(x,y,width,height);
  }

   @Override
   public int getIconWidth() {
       return this.width;
  }

   @Override
   public int getIconHeight() {
       return this.height;
  }
}

图片ImageIcon

package com.wang.swing.lable;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {

   public ImageIconDemo(){
       //获取图片地址 通过类获取当前类下资源
       JLabel jLabel = new JLabel("ImageIcon");
       URL url = ImageIconDemo.class.getResource("tx.jpg");

       ImageIcon imageIcon = new ImageIcon(url);

       jLabel.setIcon(imageIcon);
       //居中显示
       jLabel.setHorizontalAlignment(SwingConstants.CENTER);

       //获得容器
       Container container = getContentPane();
       container.add(jLabel);

       setVisible(true);
       setBounds(100,100,500,300);
       setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }
   public static void main(String[] args) {
       new ImageIconDemo();
  }
}
3.4 面板

JPanel

package com.wang.swing.jpanel;

import javax.swing.*;
import java.awt.*;

public class JPanelDemo extends JFrame {

   public JPanelDemo(){
       //所有东西要放在container中,获得container
       Container container = this.getContentPane();

       //container设置容器的样子 表格布局 后两个参数为每个面板之间的间距
       container.setLayout(new GridLayout(2,1,10,10));

       JPanel jPanel1 = new JPanel(new GridLayout(1,3));
       JPanel jPanel2 = new JPanel(new GridLayout(1,2));
       JPanel jPanel3 = new JPanel(new GridLayout(2,1));
       JPanel jPanel4 = new JPanel(new GridLayout(3,2));

       jPanel1.add(new JButton("jButton1-1"));
       jPanel1.add(new JButton("jButton1-2"));
       jPanel1.add(new JButton("jButton1-3"));
       jPanel2.add(new JButton("jButton2-1"));
       jPanel2.add(new JButton("jButton2-1"));
       jPanel3.add(new JButton("jButton3-1"));
       jPanel3.add(new JButton("jButton3-2"));
       jPanel4.add(new JButton("jButton4-1"));
       jPanel4.add(new JButton("jButton4-2"));
       jPanel4.add(new JButton("jButton4-3"));
       jPanel4.add(new JButton("jButton4-4"));
       jPanel4.add(new JButton("jButton4-5"));
       jPanel4.add(new JButton("jButton4-6"));

       container.add(jPanel1);
       container.add(jPanel2);
       container.add(jPanel3);
       container.add(jPanel4);

       this.setVisible(true);
       this.setSize(500,500);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new JPanelDemo();
  }
}

JScrollPanel 边框

package com.wang.swing.jpanel;

import javax.swing.*;
import java.awt.*;

public class JScrollPanelDemo extends JFrame {

   public JScrollPanelDemo(){

       Container container = this.getContentPane();

       //文本域
       JTextArea jTextArea = new JTextArea(20,50);//每一行写20个
       jTextArea.setText("欢迎学习java");

       //用Scroll面板
       JScrollPane jScrollPane = new JScrollPane(jTextArea);
       container.add(jScrollPane);
       
       this.setVisible(true);
       this.setBounds(100,100,500,300);
       this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  }

   public static void main(String[] args) {
       new JScrollPanelDemo();
  }
}
posted @ 2019-12-17 22:19  王迎婧  阅读(285)  评论(0编辑  收藏  举报