⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myfontchooser.java

📁 java记事本
💻 JAVA
字号:
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyFontChooser extends JDialog implements ActionListener, ListSelectionListener 
{
private JPanel top, center, bottom, top1, top2, top3;
private JLabel previewLabel;
private JTextField typeField, styleField, sizeField;
private JList typeList, styleList, sizeList;
private JScrollPane typeScroll, styleScroll, sizeScroll;
private JButton ok, cancel;
private String[] fontName, fontStyle, fontSize;
private Font font;
private String name;
private int style, size;

public MyFontChooser(JFrame owner, Font f) 
{
  super(owner, "字体选择",true );
  
  font = f;
  name = f.getFontName();
  style = f.getStyle();
  size = f.getSize();
  
  fontName = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
  fontStyle = new String[] {"普通", "粗体", "斜体", "粗斜体"};
  fontSize = new String[] { "8", "9", "10", "11", "12", "14", "16",
            "18", "20", "22", "24", "26", "28", "36", "48", "72"};
  
  typeField = new JTextField(10);
  typeField.setEditable(false);
  typeList = new JList(fontName);
  typeList.setVisibleRowCount(6);
  typeScroll = new JScrollPane(typeList);
  styleField = new JTextField(10);
  styleField.setEditable(false);
  styleList = new JList(fontStyle);
  styleList.setVisibleRowCount(6);
  styleScroll = new JScrollPane(styleList);
  sizeField = new JTextField(10);
  sizeField.setEditable(false);
  sizeList = new JList(fontSize);
  sizeList.setVisibleRowCount(6);
  sizeScroll = new JScrollPane(sizeList);
  typeList.addListSelectionListener(this);
  styleList.addListSelectionListener(this);
  sizeList.addListSelectionListener(this);
  
  top1 = new JPanel();
  top2 = new JPanel();
  top3 = new JPanel();
  top1.setLayout(new BoxLayout(top1, BoxLayout.Y_AXIS));
  top2.setLayout(new BoxLayout(top2, BoxLayout.Y_AXIS));
  top3.setLayout(new BoxLayout(top3, BoxLayout.Y_AXIS));
  
  top1.add(typeField);
  top1.add(typeScroll);
  top2.add(styleField);
  top2.add(styleScroll);
  top3.add(sizeField);
  top3.add(sizeScroll);
  
  top = new JPanel();
  top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
  top.setBorder(new TitledBorder(new EtchedBorder(), "字体/样式/大小"));
  
  top.add(top1);
  top.add(top2);
  top.add(top3);
  
  previewLabel = new JLabel("TestText 测试文本");
  previewLabel.setHorizontalAlignment(JLabel.CENTER);
  previewLabel.setBackground(Color.WHITE);
  previewLabel.setOpaque(true);
  previewLabel.setBorder(new LineBorder(Color.black));
  previewLabel.setPreferredSize(new Dimension(400, 80));
  
  center = new JPanel();
  center.setBorder(new TitledBorder(new EtchedBorder(), "预览"));
  
  center.add(previewLabel);
  
  ok = new JButton("确定");
  cancel =new JButton("取消");
  ok.addActionListener(this);
  cancel.addActionListener(this);
  
  bottom = new JPanel();
  
  bottom.add(ok);
  bottom.add(cancel);
  
  this.setLayout(new BorderLayout());
  this.add(top, BorderLayout.NORTH);
  this.add(center, BorderLayout.CENTER);
  this.add(bottom, BorderLayout.SOUTH);
  this.setResizable(false);
  this.pack();
}

public void setChoose(Font f) 
{
  font = f;
  name = f.getFontName();
  style = f.getStyle();
  size = f.getSize();
  
  typeField.setText(name);
  typeList.setSelectedValue(name, true);
  styleField.setText(fontStyle[style]);
  styleList.setSelectedIndex(style);
  sizeField.setText(String.valueOf(size));
  sizeList.setSelectedValue(String.valueOf(size), true);
}

public Font getChoose() 
{
  return font;
}

public void actionPerformed(ActionEvent ae) 
{
  if (ae.getSource()==ok) 
  {
    font = new Font(name, style, size);
    this.setVisible(false);
  } else if (ae.getSource()==cancel) 
  {
    this.setVisible(false);
  }
}

public void valueChanged(ListSelectionEvent lse) 
{
  if (lse.getSource()==typeList)
  {
    name = (String)typeList.getSelectedValue();
    typeField.setText((String)typeList.getSelectedValue());
  } else if (lse.getSource()==styleList) 
  {
    style = styleList.getSelectedIndex();
    styleField.setText(fontStyle[styleList.getSelectedIndex()]);
  } else if (lse.getSource()==sizeList) 
  {
    size = Integer.parseInt((String)sizeList.getSelectedValue());
    sizeField.setText((String)sizeList.getSelectedValue());
  }
  previewLabel.setFont(new Font(name, style, size));
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -