package java实践;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Window_Font extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1117105762639379921L;
private final JPanel contentPanel = new JPanel();
/**
* Launch the application.
*/
// public static void main(String[] args) {
// try {
// Ziti dialog = new Ziti();
// dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// dialog.setVisible(true);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
/**
* Create the dialog.
*/
@SuppressWarnings("unchecked")
public Window_Font(JTextArea textArea) {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel label = new JLabel("字体:");
label.setBounds(14, 13, 51, 18);
contentPanel.add(label);
}
JComboBox comboBox = new JComboBox();
comboBox.setBounds(64, 10, 87, 24);
contentPanel.add(comboBox);
String name[]=getFontName();
for(int i=0;i<name.length;i++){
comboBox.addItem(name[i]);
}
JLabel label = new JLabel("字形:");
label.setBounds(165, 13, 45, 18);
contentPanel.add(label);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBounds(213, 10, 60, 24);
contentPanel.add(comboBox_1);
comboBox_1.addItem("平体"); //Font.PLAIN
comboBox_1.addItem("加粗"); //Font.BOLD
comboBox_1.addItem("斜体"); //Font.ITALIC
JLabel label_1 = new JLabel("大小:");
label_1.setBounds(287, 13, 45, 18);
contentPanel.add(label_1);
JComboBox comboBox_2 = new JComboBox();
comboBox_2.setBounds(334, 10, 58, 24);
contentPanel.add(comboBox_2);
JPanel panel = new JPanel();
panel.setBounds(175, 44, 219, 142);
contentPanel.add(panel);
JLabel lblNewLabel = new JLabel("样式");
panel.add(lblNewLabel);
JButton button = new JButton("颜色");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WindowColor windowColor=new WindowColor(button,lblNewLabel,textArea);
}
});
button.setBounds(38, 99, 113, 27);
contentPanel.add(button);
for (int i = 1; i < 80; i++) {
comboBox_2.addItem(i);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int style = 0;
if (comboBox_1.getSelectedItem()=="平体") {
style = Font.PLAIN;
}
if (comboBox_1.getSelectedItem()=="加粗") {
style = Font.BOLD;
}
if (comboBox_1.getSelectedItem()=="斜体") {
style = Font.ITALIC;
}
lblNewLabel.setFont(new Font((String) comboBox.getSelectedItem(), style,comboBox_2.getSelectedIndex()));
textArea.setFont(new Font((String) comboBox.getSelectedItem(), style,comboBox_2.getSelectedIndex()));
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
public String [] getFontName(){
String all[];
GraphicsEnvironment graphicsEnvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
all=graphicsEnvironment.getAvailableFontFamilyNames();
return all;
}
}