java swing中对于JList的使用(三)
来源:互联网 发布:手机号码归属地数据库 编辑:程序博客网 时间:2024/05/16 15:44
package com.robert;import javax.swing.*;import java.awt.*;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午9:57 * To change this template use File | Settings | File Templates. */public class ListRenderingTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame frame = new ListRenderingFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); }}
package com.robert;import javax.swing.*;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.*;import java.util.ArrayList;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午9:59 * To change this template use File | Settings | File Templates. */public class ListRenderingFrame extends JFrame { private static final int DEFAULT_WIDHT = 400; private static final int DEFAULT_HEIGHT = 300; private JTextArea text; private JList fontList; public ListRenderingFrame() { setTitle("ListRenderingTest"); setSize(DEFAULT_WIDHT,DEFAULT_HEIGHT); ArrayList<Font> fonts = new ArrayList<Font>(); final int SIZE = 24; fonts.add(new Font("Serif", Font.PLAIN, SIZE)); fonts.add(new Font("SansSerif", Font.PLAIN, SIZE)); fonts.add(new Font("Monospaced", Font.PLAIN, SIZE)); fonts.add(new Font("Dialog", Font.PLAIN, SIZE)); fonts.add(new Font("DialogInput", Font.PLAIN, SIZE)); fontList = new JList(fonts.toArray()); fontList.setVisibleRowCount(4); fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fontList.setCellRenderer(new FontCellRenderer()); JScrollPane scrollPane = new JScrollPane(fontList); JPanel p = new JPanel(); p.add(scrollPane); fontList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { Font font = (Font)fontList.getSelectedValue(); text.setFont(font); } }); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.SOUTH); text = new JTextArea("The quick brown fox jumps over the lazy dog"); text.setFont((Font)fonts.get(0)); text.setLineWrap(true); text.setWrapStyleWord(true); contentPane.add(text, BorderLayout.CENTER); }}
package com.robert;import com.sun.corba.se.impl.orbutil.graph.Graph;import sun.java2d.loops.DrawGlyphListAA;import javax.swing.*;import java.awt.*;/** * Created by IntelliJ IDEA. * User: Administrator * Date: 11-11-12 * Time: 下午10:22 * To change this template use File | Settings | File Templates. */ class FontCellRenderer extends JComponent implements ListCellRenderer { private Font font; private Color background; private Color foreground; public Component getListCellRendererComponent(JList list,Object value,int index, boolean isSelected, boolean cellHasFocus) { font = (Font)value; background = isSelected ? list.getSelectionBackground() : list.getBackground(); foreground = isSelected ? list.getSelectionForeground() : list.getForeground(); return this; } public void paintComponent(Graphics g) { String text = font.getFamily(); FontMetrics fm = g.getFontMetrics(font); g.setColor(background); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(foreground); g.setFont(font); g.drawString(text, 0, fm.getAscent()); } public Dimension getPreferredSize() { String text = font.getFamily(); Graphics g = getGraphics(); FontMetrics fm = g.getFontMetrics(font); return new Dimension(fm.stringWidth(text), fm.getHeight()); } }