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

📄 fontdialog.java

📁 用Java实现的网络画图程序
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;

public class FontDialog extends JDialog implements Constants,ActionListener,ListSelectionListener{
	public FontDialog(SketchFrame window){
		super(window,"字体选择",true);
		this.window=window;	
		font=window.getFont();
		fontSize=font.getSize();
		fontStyle=font.getStyle();

		JPanel buttonPane=new JPanel();
		buttonPane.add(ok= createButton("确定"));
		buttonPane.add(cancel= createButton("取消"));
		
		getContentPane().add(buttonPane,BorderLayout.SOUTH);
		
		JPanel dataPane=new JPanel();
		dataPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(5,5,5,5)));
		GridBagLayout gbLayout=new GridBagLayout();
		dataPane.setLayout(gbLayout);
		GridBagConstraints constraints =new GridBagConstraints();
		
		JLabel label=new JLabel("选择字体");
		constraints.fill=GridBagConstraints.HORIZONTAL;
		constraints.gridwidth=GridBagConstraints.REMAINDER;
		gbLayout.setConstraints(label,constraints);
		dataPane.add(label);

		GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
		String fontNames[]=ge.getAvailableFontFamilyNames();
		fontList= new JList(fontNames);
		fontList.setValueIsAdjusting(true);
		fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		fontList.addListSelectionListener(this);
		JScrollPane chooseFont=new JScrollPane(fontList);
		chooseFont.setMinimumSize(new Dimension(300,100));

		JPanel display=new JPanel();
		fontDisplay=new JLabel("字体示例: x X y Y z Z");
		fontDisplay.setPreferredSize(new Dimension(300,100));
		display.add(fontDisplay);


		JSplitPane splitPane =new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,chooseFont,display);
		gbLayout.setConstraints(splitPane,constraints);
		
		dataPane.add(splitPane);


		JPanel sizePane=new JPanel();
		label =new JLabel("选择字体大少");
		sizePane.add(label);
		String[] sizeList={"8","10","12","14","16","18","20","22","24"};
		chooseSize=new JComboBox(sizeList);
		chooseSize.setSelectedItem(Integer.toString(fontSize));
		chooseSize.addActionListener(this);
		sizePane.add(chooseSize);
		gbLayout.setConstraints(sizePane,constraints);
		
		dataPane.add(sizePane);

		JRadioButton bold=new JRadioButton("粗体",(fontStyle&Font.BOLD)>0);
		JRadioButton italic=new JRadioButton("斜体",(fontStyle&Font.ITALIC)>0);
		bold.addItemListener(new StyleListener(Font.BOLD));
		italic.addItemListener(new StyleListener(Font.ITALIC));
		
		JPanel stylePane=new JPanel();
		stylePane.add(bold);
		stylePane.add(italic);
		gbLayout.setConstraints(stylePane,constraints);
		dataPane.add(stylePane);
		getContentPane().add(dataPane,BorderLayout.CENTER);
		pack();
		setVisible(false);

		
		
		

		
		
	
	
	}


	private JButton createButton(String label){
		JButton button=new JButton(label);
		button.setPreferredSize(new Dimension(80,20));
		button.addActionListener(this);
		return button;
	}

	
	public void actionPerformed(ActionEvent e){
		Object source=e.getSource();
		if(source==ok){
			window.setFont(font);
			setVisible(false);
		}
		else if(source==cancel)
			setVisible(false);
		
		else if(source==chooseSize){
			fontSize=Integer.parseInt((String)chooseSize.getSelectedItem());
			font=font.deriveFont((float)fontSize);
			fontDisplay.setFont(font);
			fontDisplay.repaint();
		}
	}

	public void valueChanged(ListSelectionEvent e){
		if(!e.getValueIsAdjusting()){
			font=new Font((String)fontList.getSelectedValue(),fontStyle,fontSize);
			fontDisplay.setFont(font);
			fontDisplay.repaint();
		}
	}

		
	class StyleListener implements ItemListener{
		public StyleListener(int style){
			this.style=style;
		}

		public void itemStateChanged(ItemEvent e){
			if(e.getStateChange()==ItemEvent.SELECTED)
				fontStyle|=style;
			else
				fontStyle &=~style;
			font=font.deriveFont(fontStyle);
			fontDisplay.setFont(font);
			fontDisplay.repaint();
		}
		
		private int style;
	}
				

	private SketchFrame window;
	private Font font;
	private int fontStyle;
	private int fontSize;
	private JButton ok;
	private JButton cancel;
	private JList fontList;
	private JComboBox chooseSize;
	private JLabel fontDisplay;
	

}

⌨️ 快捷键说明

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