import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Random;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;...
请问JAVA怎么实现JTextArea的刷新,要能刷新字体的
import java.awt.Container;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Random;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;public class JtextAreaDemo extends JFrame implements ActionListener { private JScrollPane pane = new JScrollPane(); private JTextArea area = new JTextArea(); private JButton button = new JButton("Click Me"); private ArrayList<String> list = new ArrayList<String>(); public JtextAreaDemo() { super("Change JTextArea Font"); try { init(); } catch (Exception e) { e.printStackTrace(); } } private void init() { pane.getViewport().add(area); pane.setAutoscrolls(true); pane.setWheelScrollingEnabled(true); pane.setBounds(10, 10, 580, 280); button.setBounds(240, 300, 100, 25); button.addActionListener(this); Container c = this.getContentPane(); c.setLayout(null); c.add(pane); c.add(button); String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (String fontName : fontNames) { list.add(fontName); } this.setSize(600, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { new JtextAreaDemo(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { Random random = new Random(); int size = random.nextInt(150) + 5; String name = list.get(random.nextInt(list.size())); area.setFont(new Font(name, 0, size)); } }}
2013-06-02
设置JTextArea的字体直接用setFont方法就可以2013-06-02