package com.swing.demo; import java.awt.EventQueue; import java.awt.Font; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; /** * GridBagLayou 布局 * * @author Administrator * */ public class GridBagLayoutTest { public static void main(String[] args) { EventQueue.invokeLater(new Runnable(){ public void run() { FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(3); frame.setVisible(true); } }); } } class FontFrame extends JFrame{ public FontFrame(){ setTitle("GridBagLayoutTest"); setSize(500,300); //布局管理器 GridBagLayout layout = new GridBagLayout(); setLayout(layout); ActionListener listener = new FontAction(); //construct components JLabel faceLabel = new JLabel("Face:"); face = new JComboBox(new String[]{"Serif","SansSerif","Monospced","Dialog","DialogInput"}); face.addActionListener(listener); JLabel sizeLabel = new JLabel("Size:"); size = new JComboBox(new String[]{"8","10","12","15","18","24","36","48",}); size.addActionListener(listener); bold = new JCheckBox("Blod"); bold.addActionListener(listener); italic = new JCheckBox("Italic"); italic.addActionListener(listener); sample = new JTextArea(); sample.setText("The quick brown fox jumps over the lazy dog"); sample.setEditable(false); sample.setLineWrap(true); sample.setBorder(BorderFactory.createEtchedBorder()); add(faceLabel,new GBC(0,0).setAnchor(GBC.EAST)); add(face,new GBC(1,0).setFill(GBC.HORIZONTAL).setweight(100,0).setInsets(1)); add(sizeLabel,new GBC(0,1).setAnchor(GBC.EAST)); add(size, new GBC(1,1).setFill(GBC.HORIZONTAL).setweight(100,0).setInsets(1)); add(bold,new GBC(0,2,2,1).setAnchor(GBC.CENTER).setweight(100,100)); add(italic,new GBC(0,3,2,1).setAnchor(GBC.CENTER).setweight(100,100)); add(sample,new GBC(2,0,1,4).setFill(GBC.BOTH).setweight(100,100)); } private JComboBox face; private JComboBox size; private JCheckBox bold; private JCheckBox italic; private JTextArea sample; class FontAction implements ActionListener{ public FontAction(){} public void actionPerformed(ActionEvent e) { String fontFace = (String) face.getSelectedItem(); int fontStyle = (bold.isSelected() ? Font.BOLD:0) + (italic.isSelected() ? Font.ITALIC:0); int fontSize = Integer.parseInt((String)size.getSelectedItem()); Font font = new Font(fontFace,fontStyle,fontSize); sample.setFont(font); sample.repaint(); } } }