swing学习

1.Swing组件类的层次
  Swing组件分从显示效果上分为两种类型:JComponent类和Window类;
    JComponent组件主要包括一些不能独立显示的组件(必须依附与其他组件才能显示)。
      JPanel
      JTable
      JTree
      JTextArea
      JTextField
      JButton
    Window组件类主要包括可以独立显示的组件。
      JFrame
      JDialog

  Swing组件从功能上分为三种类型:
    顶级组件(顶级容器,可以独立显示)
      JFrame、JApplet、JDialog、JWindow
    中间组件
      中间容器类(可以充当容器,但不能独立显示)
      JPanel、JScrollPane、JSplitPane、JToolBar
      特殊中间组件类(在GUI上起特殊作用的中间层,属于中间容器类,但是能起到美化和专业化的作用)
      JInternalFrame、JLayeredPane、JRootPane等
    基本组件(实现人机交互的组件,只能依附于中间组件才能显示)
      JButton、JComboBox、JList、JMenu、JSlider、JTextField等

2.Window类
  一切图形化的东西必须包括在顶级容器内。Swing中主要有三种可以使用的顶级容器:
    JFrame(用于设计类似于Windows系统中的窗口程序)
      内容面板(ContentPane)
        基本组件
      菜单条
    JDialog(用于设计对话框)

3.JComponent类(所有轻量级组件的父类)
  JComponent的九大特性:
    Tooltips工具提示功能(鼠标停在组件上,显示提示),通过setToolTipText实现
    绘画和边框(setBorder,BorderFactory)
    可插入的观感器(定制自己的桌面,更换新的颜色方案,包括默认、Motif和Windows的L&F)
    自定义属性
    layout支持
    无障碍
    拖拽支持
    双缓冲
    键绑定

4.布局管理器  

  BorderLayout:东、南、西、北、中;(默认布局,默认被放置到中)

  

  FlowLayout:按照加入的先后顺序排列,行满换行;从左到右,居中排列;

  

  GridLayout:将空间划分为网状区域;

  

  GriBagLayout:网状划分,功能较GridLayout复杂;

    //太复杂了,给你个链接 https://www.cnblogs.com/aipan/p/6831872.html
  CardLayout:将组件当成卡片,每一只能显示一个。

    

  BoxLayout:通过允许在容器中水平或垂直的方式安排多个组件;
  SpringLayout:通过定义组件边沿的关系来实现布局;
  GroupLayout:指定一个窗体上组件彼此之间的关系。

  绝对定位,setLayout(null)把布局管理器设置为空,为每个组件调用setBounds()放啊发传递位置

5.常见组件

  JPanel  普通面板组件

  JScrollPane  带滚动的面板组件

    

  JSplitPane 带分割的面板组件

    

  JTabbedPane 选项卡组件

    

  JLabel  标签

  JTextField  文本框

  JTextArea  文本区域

  JPasswordField   密码框

  JRadioButton   ButtonGroup   单选框

  JCheckBox   复选框

  

  JMenuBar

    

6. Swing与并发

    当你使用swing编程时,就是在使用线程。在本章开头曾说过,所有的事物都应该通过SwingUtilities.invokeLater()提交swing事件分发线程。但是,不用显示的创建Thread对象这一事实意味着多线程问题可能会让你大吃一惊,你必须牢记存在着一个Swing事件分发线程,他始终在那里,通过从事件队列中拉出每个事件并依次执行它们,来处理所有的swing事件。牢记事件分发线程,将有助于确保你的应用免遭死锁和竞争条件影响。本节将讲述在使用Swing时所产生的多线程问题。

  

package com.example.demo.swing;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;

public class LongRunningTask extends JFrame{
    private JButton b1 = new JButton("Start long Running Task");
    private JButton b2 = new JButton("End long Running Task");

    public LongRunningTask() {
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                System.out.println("Task completed");
            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Thread.currentThread().interrupt();
            }
        });
        setLayout(new FlowLayout());
        add(b1);
        add(b2);

    }

    public static void main(String[] args) {
        SwingConsole.run(new LongRunningTask(), 200, 150);
    }
}

  存在的问题,点击按钮之后无响应,点击第二个按钮并不会中断等待。

package com.example.demo.swing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;

class Task implements Runnable{
    private static int counter = 0;
    private final int id = counter++;

    @Override
    public void run() {
        System.out.println(this + " started");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            System.out.println(this + " interrupted");
            return;
        }
        System.out.println(this + " comleted");
    }

    public String toString() {
        return "Task " + id;
    }

    public long id() {
        return id;
    }

}

public class InterruptableLongRunningTask extends JFrame{
    private JButton b1 = new JButton("Start long Running Task");
    private JButton b2 = new JButton("End long Running Task");

    ExecutorService executorService = Executors.newSingleThreadExecutor();

    public InterruptableLongRunningTask() {
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Task task = new Task();
                executorService.execute(task);
                System.out.println(task + " added to the queue");
            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                executorService.shutdownNow();
            }
        });
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
    }

    public static void main(String[] args) {
        SwingConsole.run(new InterruptableLongRunningTask(), 200, 150);
    }
}

  仍然存在问题shutdownNow()之后不能再执行任务了,所以需要Callable/Future.

public class TaskItem<R,C extends Callable<R>> {
    public final Future<R> future;
    public final C task;

    public TaskItem(Future<R> future,C task) {
        this.future = future;
        this.task = task;
    }
}

public class TaskManager<R,C extends Callable<R>> extends ArrayList<TaskItem<R,C>>{
    private ExecutorService exec = Executors.newSingleThreadExecutor();

    public void add(C task) {
        add(new TaskItem<R, C>(exec.submit(task), task));
    }

    public List<R> getResults(){
        List<R> result = new ArrayList<>();
        Iterator<TaskItem<R, C>> items = iterator();
        while(items.hasNext()) {
            TaskItem<R, C> item = items.next();
            if(item.future.isDone()) {
                try {
                    result.add(item.future.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
                items.remove();
            }
        }
        return result;
    }

    public List<String> purge(){
        Iterator<TaskItem<R, C>> items = iterator();
        List<String> results = new ArrayList<>();
        while(items.hasNext()) {
            TaskItem<R, C> item = items.next();
            if(!item.future.isDone()) {
                results.add("Cancelling "+item.task);
                item.future.cancel(true);
                items.remove();
            }
        }
        return results;
    }
}

============================================================================================================================

class Task implements Runnable{
    private static int counter = 0;
    private final int id = counter++;

    @Override
    public void run() {
        System.out.println(this + " started");
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            System.out.println(this + " interrupted");
            return;
        }
        System.out.println(this + " comleted");
    }

    public String toString() {
        return "Task " + id;
    }

    public long id() {
        return id;
    }

}

class CallableTask extends Task implements Callable<String>{

    @Override
    public String call() throws Exception {
        run();
        return "Return value of " + this;
    }

}

public class InterruptableLongRunningCallable extends JFrame{
    private JButton b1 = new JButton("Start Long Running Task");
    private JButton b2 = new JButton("End Long Running Task");
    private JButton b3 = new JButton("Get results");

    private TaskManager<String, CallableTask> manager = new TaskManager<>();

    public InterruptableLongRunningCallable() {
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                CallableTask task = new CallableTask();
                manager.add(task);
                System.out.println(task + " added to the queue");
            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(String result:manager.purge()) {
                    System.out.println(result);
                }
            }
        });
        b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(TaskItem<String, CallableTask> tt:manager) {
                    tt.task.id();
                }
                for(String result:manager.getResults()) {
                    System.out.println(result);
                }
            }
        });
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
        add(b3);
    }
    public static void main(String[] args) {
        SwingConsole.run(new InterruptableLongRunningCallable(), 200, 150);
    }

}

  通常你还需要一个带进度条的友好提示界面

package com.example.demo.swing;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;

class MonitoredCallable implements Callable<String>{
    private static int counter = 0;
    private final int id = counter++;

    private final ProgressMonitor monitor;
    private static final int MAX = 8;

    public MonitoredCallable(ProgressMonitor monitor) {
        this.monitor = monitor;
        monitor.setNote(toString());
        monitor.setMaximum(MAX-1);
        monitor.setMillisToPopup(500);
    }

    @Override
    public String call() throws Exception {
        System.out.println(this + " stared");
        try {
            for(int i=0;i<MAX;i++) {
                TimeUnit.MILLISECONDS.sleep(500);
                if(monitor.isCanceled()) {
                    Thread.currentThread().interrupt();
                }
                final int progess = i;
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        monitor.setProgress(progess);
                    }
                });
            }
        }catch (Exception e) {
            monitor.close();
            System.out.println(this + " interrupted");
            return "Result: " + this + " interrupted";
        }
        System.out.println(this + " completed");
        return "Result: " + this + " completed";
    }

    @Override
    public String toString() {
        return "Task " + id;
    }
}

public class MonitoredLongRunningCallable extends JFrame{
    private JButton b1 = new JButton("Start Long Running Task");
    private JButton b2 = new JButton("End Long Running Task");
    private JButton b3 = new JButton("Get results");

    private TaskManager<String, MonitoredCallable> manager = new TaskManager<>();

    public MonitoredLongRunningCallable() {
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                MonitoredCallable task = new MonitoredCallable(new ProgressMonitor(MonitoredLongRunningCallable.this, "Long-Running Task", "", 0, 0));
                manager.add(task);
                System.out.println(task + " added to the queue");
            }
        });

        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(String resutl:manager.purge()) {
                    System.out.println(resutl);
                }
            }
        });

        b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(String result:manager.getResults()) {
                    System.out.println(result);
                }
            }
        });
        setLayout(new FlowLayout());
        add(b1);
        add(b2);
        add(b3);
    }

    public static void main(String[] args) {
        SwingConsole.run(new MonitoredLongRunningCallable(), 200, 500);
    }
}

22.10.2 可视化线程机制

package com.example.demo.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.sql.Time;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.swing.JFrame;
import javax.swing.JPanel;

class CBox extends JPanel implements Runnable{
    private int pause;
    private static Random rand = new Random();
    private Color color = new Color(0);

    public CBox(int pause) {
        this.pause = pause;
    }

    @Override
    protected void paintComponent(Graphics g) {
        g.setColor(color);
        Dimension s = getSize();
        g.fillRect(0, 0, s.width, s.height);
    }

    @Override
    public void run() {
        while(!Thread.interrupted()) {
            color = new Color(rand.nextInt(0xFFFFFF));
            repaint();
            try {
                TimeUnit.MILLISECONDS.sleep(pause);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

public class ColorBoxes extends JFrame{
    private int grid = 12;
    private int pause = 50;
    private static ExecutorService exec = Executors.newCachedThreadPool();

    public void setUp() {
        setLayout(new GridLayout(grid,grid));
        for(int i=0;i<grid*grid;i++) {
            CBox cBox = new CBox(pause);
            add(cBox);
            exec.execute(cBox);
        }
    }

    public static void main(String[] args) {
        ColorBoxes boxes = new ColorBoxes();
        if(args.length>0) {
            boxes.grid = new Integer(args[0]);
        }
        if(args.length>1) {
            boxes.pause = new Integer(args[1]);
        }
        boxes.setUp();
        SwingConsole.run(boxes, 500, 400);
    }
}

22.11 可视化编程与JavaBean

  Introspector(内省器)

package com.example.demo.swing;

import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.BeanInfo;
import java.beans.EventSetDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.WildcardType;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class BeanDumper extends JFrame{
    private JTextField query = new JTextField(20);
    private JTextArea results = new JTextArea();

    public void print(String s) {
        results.append(s+"\n");
    }

    public void dump(Class<?> bean) {
        results.setText("");
        BeanInfo bInfo = null;
        try {
            bInfo = Introspector.getBeanInfo(bean,Object.class);
        } catch (IntrospectionException e) {
            System.out.println("couldn‘t introspect " + bean.getName());
            e.printStackTrace();
            return;
        }
        for(PropertyDescriptor d:bInfo.getPropertyDescriptors()) {
            Class<?> p = d.getPropertyType();
            if(p==null) continue;
            System.out.println("property type: " + p.getName() + "property name: "+d.getName());
            Method readMethod = d.getReadMethod();
            if(readMethod!=null) {
                System.out.println("read method: "+readMethod);
            }
            Method writeMethod = d.getWriteMethod();
            if(writeMethod!=null) {
                System.out.println("write metho: "+writeMethod);
            }
            System.out.println("==============================");
        }
        System.out.println("public methods:");
        for(MethodDescriptor m:bInfo.getMethodDescriptors()) {
            System.out.println(m.getMethod().toString());
        }
        System.out.println("==============================");
        System.out.println("event support:");
        for(EventSetDescriptor e:bInfo.getEventSetDescriptors()) {
            System.out.println("Listener type: "+e.getListenerType().getName());
            for(Method lm:e.getListenerMethods()) {
                System.out.println("Listener method: "+lm.getName());
            }
            for(MethodDescriptor lm:e.getListenerMethodDescriptors()) {
                System.out.println("method descriptor: "+ lm.getMethod());
            }
            Method addListener = e.getAddListenerMethod();
            System.out.println("Add listener Method "+ addListener);
            Method removeListener = e.getRemoveListenerMethod();
            System.out.println("remove listener Method"+removeListener);
            System.out.println("==========================");
        }
    }

    class Dumper implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            String name = query.getText();
            Class<?> c = null;
            try {
                c = Class.forName(name);
            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();
                results.setText("couldn‘t find "+name);
                return;
            }
            dump(c);
        }

    }

    public BeanDumper() {
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(new JLabel("Qualified bean name"));
        panel.add(query);
        add(BorderLayout.NORTH,panel);
        add(new JScrollPane(results));
        Dumper dumper = new Dumper();
        query.addActionListener(dumper);
        query.setText("com.example.demo.swing.Frog");
        dumper.actionPerformed(new ActionEvent(dumper, 0, ""));
    }

    public static void main(String[] args) {
        SwingConsole.run(new BeanDumper(), 600, 500);
    }
}

原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/11760477.html

时间: 2024-10-14 07:35:52

swing学习的相关文章

JAVA Swing学习之窗体文本显示

JAVA中提供了一种类Font,专门用来定义字体,关于这个字体的定位需要注意几点: 1.利用Font类的getStringBounds方法来获得包围字符串的矩形: 2.如果要的得到屏幕设备对于字体属性的描述,需要调用Graphics的getFontRenderContext方法: 3.注意几个概念:上坡度,下坡度,行间距,基线,行高,这些大小会用来定义对一个字符串的描述,其中如果得到包围字符串矩阵的边界,对其进行getWidth(),则可得到上坡度+下坡度+行间距: 4.如果想得到下坡度和行间距

Swing学习篇 API [一]Swing常用组件

1.按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且还可以将多个图像指定给一个按钮. 在JButton中有如下几个比较常用的构造方法. JButton(Icon icon): 按钮上显示图标. JButton(String text): 按钮上显示字符. JButton(String text,Icon icon): 按钮上既显示图标又先施字符. J

Swing学习笔记1-----Swing组件类的层次

1.  从结构上划分 Swing 组件类分为两种,一种是JComponent类,一种是Windows类.其中windows类包含的是一些可以独立显示的组件,而JComponent类包含的是不可以独立显示的组件. 什么是可独立显示的组件和不可独立显示的组件? 可独立显示的组件:当运行一个程序时,可独立显示的组件无需在其他组件上即可显示,即它可以直接显示出来,例如JFrame类. 不可独立显示的组件:运行时,必须依靠可独立显示的组件才能将其显示出来,如JLabel类,JButton类,得托付在类似于

Swing 学习1

AWT和swing中框架和组件类的继承 Object Component Container JComponent        Window JPanel                 Frame JFrame

Java Swing学习小结

1.使窗体位于屏幕中心 Dimension welcomeScreen=Toolkit.getDefaultToolkit().getScreenSize(); int x=(welcomeScreen.width-this.getSize().width)/2; int y=(welcomeScreen.height-this.getSize().height)/2; this.setBounds(x, y, 500, 400); 2.设置文件过滤器 private void jButton1

Java Swing界面编程(1)

写多了jsp,对于页面式的系统已经写烦了,本人也開始着手于java swing的学习,作为菜鸟,仅想用博客记录下我的swing学习的历程.话不多说,首先開始我的第一个窗体化程序. 下面给出源码: package com.beyole.util; import java.awt.Color; import javax.swing.JFrame; public class test1 { public static void main(String[] args) { JFrame frame=ne

swing入门教程

     UI 组件简介 在开始学习 Swing 之前,必须回答针对真正初学者的一个问题:什么是 UI?初学者的答案是"用户界面".但是因为本教程的目标是要保证您不再只是个初学者,所以我们需要比这个定义更高级的定义. 所以,我再次提出这个问题:什么是 UI?您可能把它定义成您按下的按钮.打字的地址栏 .打开和关闭的窗口,等等,这些都是 UI 的元素,但是除了在屏幕上看到的这些之外,还有更多都是 UI 元素.比如鼠标.键盘.音量.屏幕颜色.使用的字体,以及一个对象相对于另一个对象的位

swing Jlable中存放变量显示问题

java swing 学习 在做一个ATM机系统小案例中,碰到JLable中存放变量,变量发生改变,而JLable中还是显示原来的值,网上寻找答案,用updateUI()和revalidate();方法都没有用,原来解决这个问题,需要把存放变量的JLable设为全局变量. atm机代码下载地址 版权声明:本文为博主原创文章,未经博主允许不得转载.

Swing:LookAndFeel 教程第一篇——手把手教你写出自己的 LookAndFeel

本文是 LookAndFeel 系列教程的第一篇. 是我在对 Swing 学习摸索中的一些微薄经验. 我相信,细致看全然系列之后.你就能写出自己的 LookAndFeel. 你会发现 Swing 原来能够这样美. -------------------------------------------------------------------------------- 引言: 我第一次接触 Java 要追溯到非常多年前做毕业设计的时候. 那天我和同学来到了一个微型软件公司(三程序猿.一会计.