GUI编程二

3. Swing

AWT是底层,Swing是封装了,画出的窗口会更好看一点

3.1 窗口,面板

JFrame

public class JFrameDemo {
      public static void main(String[] args){
            //建立一个窗口
            new MyJframe2().init();
      }
}

class MyJframe2 extends JFrame{
      public void init(){
            this.setBounds(10,10,200,300)
            this.setVisible(true);
      
            //设置文字 JLabel
            JLable label = new JLabel("欢迎******");
            jf.add(label)
            //设置居中
            label.setHorizontalAlignment(SwingConstants.CENTER);

            //获得一个容器,需要设置容器的颜色才能更换背景色,直接设置frame是没办法的。
            Container container = this.getContentPane();
            container.setBackground(Color.BLUE);

            //关闭事件直接调用现成的不需要自己写监听,关闭操作有四个常量
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

}

3.2 弹窗

JDialog 默认有关闭事件

      public class DialogDemo extends JFrame{
      public DialogDemo(){
            this.setVisible(true);
            this.setSize(700,500);
            this.setDefaultCloseOperation(WindwoConstants.EXIT_ON_CLOSE);

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

            JButton button = new JButton("点击一个对话框");
            button.setBounds(30,30,200,50);

            //点击这个按钮的时候,弹出一个弹窗
            button.addActionListner(new ActionListner(){
                  @Override
                  public void actionPerformed(ActionEvent e){
                        //弹窗
                        new MyDialogDemo();            

                  }

            }
            )
            container.add(button);
            
      }

      public static void main(String[] args){
            new DialogDemo();
      }
         
      }
//弹窗
class MyDialogDemo extends JDialog{
      public MyDialogDemo (){
            this.setVisible(true);
            this.setBounds(100,100,500,500);
            //弹窗JDialog不需要自己写关闭,已经有了
            //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            Container container = this.getContentPane();
            container.setLayout(null);

            container.add(new Label("学java"));
      }
}

3.3 标签

new JLabel("***")
图标ICON
例1 画出来的图标

//实现图标接口,继承frame类
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);
            //图标可以放在标签,按钮上,创建时第二个参数是icon
            new JLabel("icontest",iconDemo,SwingConstants.CENTER);
            
            Container container = getContentPane();
            container.add(label);

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

      @Overide
      public void paintIcon(Component c, Graphics g, int x, int y){
            g.fillOval(x,y,width,height)
      }
      @Overide
      public int getInconWidth(){
            return this.width;
      }
      @Overide
      public  int getIconHeight(){
            return this.height;
      }
}

例2 图片图标

public class ImageIconDemo extend JFrame{
      public ImageIconDemo(){
            //获取图片地址,获取当前类同级资源
            JLabel label = new JLabel("ImageIcon");
            URL url=ImageIconDemo.class.getResource(tx.jpg)

            ImageIcon imageIcon = new ImageIcon(url);
            label.setIcon(imageIcon);
            label.setHorizontalAlignment(SwingContants.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();
      }
}

3.4 面板

JPanel,JScroll面板

public class JPanelDemo extends JFrame {
      Container container = this.getContentPane();
      container.setLayout(new GridLayout(2,1,10,10));//后面参数间距
      JPanel panel1 = new JPanel(new GridLayout(1,3));
      panel1.add(new JButton(1));
      panel1.add(new JButton(1));
      panel1.add(new JButton(1));
      
      container.add(panel1);

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

JScrollPanel

public class JScrollDemo extends JFrame{
      public JScrollDemo(){
            Container container =  this.getContentPane();

            //文本域
            JTextArea textArea = new JTextArea(20,150);
            textArea.setText("欢迎学习");//默认值

            JScrollPane scrollPane = new JScrollPane(textArea);
           // container.add(textArea); 不能直接add TextArea,要使用ScrollPane面板
            container.add(scrollPane);
            this.setVisible(true);
            this.setBounds(100,100,300,350);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      }

      public void static main(){
            new JScrollDemo();
      }

}

3.5 按钮

  • 图标按钮setIcon, setToolTipText
public class JButtonDemo01 extends JFrame{
      public JButtonDemo01() throws HeadlessException{
      Container container = this.getContentPane();
      //将一个图片变为图标
      URL resource = JButtonDemo01.class.getResource("tx.png");
      Icon icon = new ImageIcon(resource);

      //把这个图标放在按钮上
      JButton button = new JButton();
      button.setIcon(icon);
      button.setToolTipText("图片按钮");
      
      container.add(button);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setSize(500, 300);
      this.setVisible(true);
}
      public static void main(String[] args){
    	  System.out.print("hello");
    	  new JButtonDemo01();
      }
}      
  • 单选按钮 JRadioButton & ButtonGroup
    注意要分在一个组,否则不能实现多个按钮单选
      //radio button
      JRadioButton radioButton1 = new JRadioButton("JRadioButton1");
      JRadioButton radioButton2 = new JRadioButton("JRadioButton2");
      JRadioButton radioButton3 = new JRadioButton("JRadioButton3");
      
      //need ButtonGroup , only can choose one in a group
      ButtonGroup group = new ButtonGroup();
      group.add(radioButton1);
      group.add(radioButton2);
      group.add(radioButton3);
      
      container.add(radioButton3, BorderLayout.CENTER);
      container.add(radioButton2, BorderLayout.NORTH);
      container.add(radioButton1, BorderLayout.SOUTH);
  • 复选按钮 JCheckBox
      //checkBox
      JCheckBox checkBox01 = new JCheckBox("checkBox01");
      JCheckBox checkBox02 = new JCheckBox("checkBox02");
      
      container.add(checkBox01,BorderLayout.NORTH);
      container.add(checkBox02,BorderLayout.SOUTH);

3.6 列表

  • 下拉框 JComboBox
		JComboBox status = new JComboBox(); 
		status.addItem(null);
		status.addItem("ongoing");
		status.addItem("done");
		status.addItem("pending");
		status.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JComboBox status = (JComboBox)e.getSource();
				System.out.print(status.getSelectedIndex());
			}
		});
		container.add(status);
  • 列表框 JList
    不是下拉的,直接展示出来的item
		//construct content in the list,different data type for JList
		//String[] contents = {"1","2","3"};
		
                //verctor集合可以动态增减,应用举例如QQ好友列表
		Vector contents = new Vector();
		JList jlist = new JList(contents);

		container.add(jlist);
		contents.add("zhangsan");
		contents.add("lisi");
		contents.add("wangwu");
  • 应用场景
    下拉框:选择地区,或者一些单个选项,只有两个的时候采用单选项
    列表: 展示信息,一般是动态扩容的

3.7 文本框

  • 文本框 JTextField
		JTextField textField = new JTextField("hello");
		JTextField textField2 = new JTextField("world",20);
		
		container.add(textField,BorderLayout.NORTH);
		container.add(textField2,BorderLayout.SOUTH);
  • 密码框 JPasswordField, setEchoChar
    尽量使用panel做,不要直接使用container(默认东西南北中布局)里写
		Container container = this.getContentPane();

		JPasswordField passwordField = new JPasswordField();
		passwordField.setEchoChar('*');
		container.add(passwordField);
  • 文本域 JTextArea
    配合面板JScrollPanel,例参上JScrollPanel
    扩展
  1. 优秀的前端页面素材网站 layui
  2. C/S 架构,对于BS 结构 HTML(组件画图)+Servlet(监听器)
posted @ 2021-02-02 17:30  晒网达人  阅读(104)  评论(0)    收藏  举报