关闭

利用下拉框控制字体格式

195人阅读 评论(0) 收藏 举报

package swing;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ComboBoxTest {
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   @Override
   public void run() {
    ComboBoxFrame frame = new ComboBoxFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
   }
  });
 }
}

class ComboBoxFrame extends JFrame {
 public ComboBoxFrame() {
  this.setTitle("ComboBox Test");
  this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  
  label = new JLabel("The quick brown fox jumps over the lazy dog.");
  label.setFont(new Font("Serif", Font.PLAIN, DEFAULT_SIZE));
  this.add(label, BorderLayout.CENTER);
  
  faceCombo = new JComboBox();
  faceCombo.setEditable(true);
  faceCombo.addItem("Serif");
  faceCombo.addItem("SansSerif");
  faceCombo.addItem("Monospaced");
  faceCombo.addItem("Dialog");
  faceCombo.addItem("DialogInput");
  
  faceCombo.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent event) {
    label.setFont(new Font((String) faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE));
   }
  });
  
  JPanel comboPanel = new JPanel();
  comboPanel.add(faceCombo);
  this.add(comboPanel, BorderLayout.SOUTH);
 }
 
 public static final int DEFAULT_WIDTH = 300;
 public static final int DEFAULT_HEIGHT = 200;
 
 private JComboBox faceCombo;
 private JLabel label;
 private static final int DEFAULT_SIZE = 12;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:100948次
    • 积分:1751
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:0篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论