所以,我有一个包含JPanels的ArrayList;所有JPanel都有一个BorderLayout,其中包含一个在NORTH上设置的JPanel(包含两个JLabel)和一个在CENTER上设置的JTextArea(当然包含文本)。我的问题是如何修改每个JTextArea的字体大小?
答案 0 :(得分:5)
以下是一些简单的代码,允许通过JTextArea
方法设置setFontSize(int index, int fontSize)
字体大小。请注意,这仅适用于panels
数组列表中的文本区域。在下面的示例中,我更改了文本区域#1和#3上的字体(有关执行此操作的调用,请参阅main
方法。)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class SimpleFrame extends JFrame {
ArrayList<JPanel> panels = new ArrayList<JPanel>();
public SimpleFrame() {
super("Simple Panel List Example");
JPanel content = (JPanel)getContentPane();
content.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
// add some panels to the array list
for(int i = 0; i < 5; i++) {
BorderLayout b = new BorderLayout();
JPanel p = new JPanel(b);
JLabel north = new JLabel("Label #"+i);
JTextArea center = new JTextArea("TextArea #"+i);
p.add("North", north);
p.add("Center", center);
panels.add(p);
content.add(p);
}
}
// change the font size of the JTextArea on panel #i
public void setFontSize(int i, int fontSize) {
JPanel p = panels.get(i);
JTextArea t = (JTextArea)((BorderLayout)p.getLayout()).getLayoutComponent("Center");
Font f = t.getFont();
Font f2 = f.deriveFont((float)fontSize);
t.setFont(f2);
}
public static void main(String[] argv) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleFrame c = new SimpleFrame();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.pack();
c.setVisible(true);
// we can change the font size using our setFontSize method
c.setFontSize(1, 8);
c.setFontSize(3, 16);
}
});
}
}