zoukankan      html  css  js  c++  java
  • e778. 在JList中加入和删除项

    The default model for a list does not allow the addition and removal of items. The list must be created with a DefaultListModel.

        // Create a list that allows adds and removes
        DefaultListModel model = new DefaultListModel();
        JList list = new JList(model);
        
        // Initialize the list with items
        String[] items = {"A", "B", "C", "D"};
        for (int i=0; i<items.length; i++) {
            model.add(i, items[i]);
        }
        
        // Append an item
        int pos = list.getModel().getSize();
        model.add(pos, "E");
        
        // Insert an item at the beginning
        pos = 0;
        model.add(pos, "a");
    

    This method replaces an it

     
     
    Home > List of Packages > javax.swing.text [49 examples] > JTextArea [7 examples]

    e988. 用文本域实现一个控制台窗口

    This example creates a console window using a JTextArea which shows all output printed to System.out and System.err. System.out and System.err are replaced with piped io streams. Two reader threads are created that retrieve the output from these piped io streams and append it to the JTextArea component.
        import java.awt.*;
        import java.awt.event.*;
        import java.io.*;
        import javax.swing.*;
        import javax.swing.event.*;
        import javax.swing.text.*;
        
        public class Console extends JFrame {
            PipedInputStream piOut;
            PipedInputStream piErr;
            PipedOutputStream poOut;
            PipedOutputStream poErr;
            JTextArea textArea = new JTextArea();
        
            public Console() throws IOException {
                // Set up System.out
                piOut = new PipedInputStream();
                poOut = new PipedOutputStream(piOut);
                System.setOut(new PrintStream(poOut, true));
        
                // Set up System.err
                piErr = new PipedInputStream();
                poErr = new PipedOutputStream(piErr);
                System.setErr(new PrintStream(poErr, true));
        
                // Add a scrolling text area
                textArea.setEditable(false);
                textArea.setRows(20);
                textArea.setColumns(50);
                getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER);
                pack();
                setVisible(true);
        
                // Create reader threads
                new ReaderThread(piOut).start();
                new ReaderThread(piErr).start();
            }
        
            class ReaderThread extends Thread {
                PipedInputStream pi;
        
                ReaderThread(PipedInputStream pi) {
                    this.pi = pi;
                }
        
                public void run() {
                    final byte[] buf = new byte[1024];
                    try {
                        while (true) {
                            final int len = pi.read(buf);
                            if (len == -1) {
                                break;
                            }
                            SwingUtilities.invokeLater(new Runnable() {
                                public void run() {
                                    textArea.append(new String(buf, 0, len));
        
                                    // Make sure the last line is always visible
                                    textArea.setCaretPosition(textArea.getDocument().getLength());
        
                                    // Keep the text area down to a certain character size
                                    int idealSize = 1000;
                                    int maxExcess = 500;
                                    int excess = textArea.getDocument().getLength() - idealSize;
                                    if (excess >= maxExcess) {
                                        textArea.replaceRange("", 0, excess);
                                    }
                                }
                            });
                        }
                    } catch (IOException e) {
                    }
                }
            }
        }
    
    The console window is created using
        try {
            new Console();
        } catch (IOException e) {
        }
    
    Related Example
  • 相关阅读:
    小白自动化测试指南
    分布式性能测试框架用例方案设想(二)
    高QPS下的固定QPS模型
    测试自动化最佳实践【译】
    moco框架接口命中率统计实践
    基于docker的分布式性能测试框架功能验证(一)
    编写高质量代码:Web前端开发修炼之道(一)
    JavaScript中点操作符和中括号操作符区别
    Vue脚手架生成及配置
    Npm设置淘宝镜像
  • 原文地址:https://www.cnblogs.com/borter/p/9596133.html
Copyright © 2011-2022 走看看