展开全部
java有专门的FontDialog和ColorDialog的
class FontSet implements ActionListener { // "字体设置"菜单事件
@Override
public void actionPerformed(ActionEvent fonte) {
new FontDialog();
}
class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{
private static final long serialVersionUID = 1L;
public JCheckBox Bold,Italic;
public List Size,Name;
public int FontName;
public int FontStyle;
public int FontSize;
public JButton OK,Cancel;
public JTextArea Text;
FontDialog() {
GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
String name[]=g.getAvailableFontFamilyNames();
Bold=new JCheckBox("加粗",false);
Italic=new JCheckBox("斜体",false);
Text=new JTextArea("字体预览用例\n9876543210\nAaBbCcXxYyZz");
OK=new JButton("确定");
Cancel=new JButton("取消");
Size=new List();
Name=new List();
int i=0;
Name.add("Default Value");
for(i=0;i<name.length;i++) Name.add(name[i]);
for(i=8;i<257;i++) Size.add(String.valueOf(i));
FontName=0;
FontStyle=0;
FontSize=8;
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setSize(480, 306);
this.setVisible(true);
this.setResizable(false);
OK.setFocusable(false);
Cancel.setFocusable(false);
Bold.setFocusable(false);
Italic.setFocusable(false);
Name.setFocusable(false);
Size.setFocusable(false);
Name.setBounds(10, 10, 212, 259);
Container c=this.getContentPane();
c.add(Name);
Bold.setBounds(314, 10, 64, 22);
c.add(Bold);
Italic.setBounds(388, 10, 64, 22);
c.add(Italic);
Size.setBounds(232, 10, 64, 259);
c.add(Size);
Text.setBounds(306, 40, 157, 157);
c.add(Text);
OK.setBounds(306, 243, 74, 26);
c.add(OK);
Cancel.setBounds(390, 243, 74, 26);
c.add(Cancel);
Name.select(FontName);
Size.select(FontSize);
Text.setFont(getFont());
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Name.addItemListener(this);
Size.addItemListener(this);
Bold.addItemListener(this);
Italic.addItemListener(this);
OK.addActionListener(this);
Cancel.addActionListener(this);
this.addWindowListener(this);
}
public void itemStateChanged(ItemEvent e) {
Text.setFont(getFont());
area.setFont(getFont());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==OK){
FontName=Name.getSelectedIndex();
FontStyle=getStyle();
FontSize=Size.getSelectedIndex();
this.setVisible(false);
}
else cancel();
}
public void windowClosing(WindowEvent e) {
cancel();
}
public Font getFont(){
if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);
else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);
}
public void cancel(){
Name.select(FontName);
Size.select(FontSize);
setStyle();
Text.setFont(getFont());
this.setVisible(false);
}
public void setStyle(){
if(FontStyle==0 || FontStyle==2) Bold.setSelected(false);
else Bold.setSelected(true);
if(FontStyle==0 || FontStyle==1) Italic.setSelected(false);
else Italic.setSelected(true);
}
public int getStyle(){
int bold=0,italic=0;
if(Bold.isSelected()) bold=1;
if(Italic.isSelected()) italic=1;
return bold+italic*2;
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
}
class backgroundEvent implements ActionListener { // "背景颜色"设置
@Override
public void actionPerformed(ActionEvent arg0) {
Color ccc = JColorChooser.showDialog(WriteText.this, "color",
new Color(240, 255, 255));
area.setSelectedTextColor(ccc);
area.setBackground(ccc);
}
}
class FontSet implements ActionListener { // "字体设置"菜单事件
@Override
public void actionPerformed(ActionEvent fonte) {
new FontDialog();
}
class FontDialog extends JDialog implements ItemListener, ActionListener, WindowListener{
private static final long serialVersionUID = 1L;
public JCheckBox Bold,Italic;
public List Size,Name;
public int FontName;
public int FontStyle;
public int FontSize;
public JButton OK,Cancel;
public JTextArea Text;
FontDialog() {
GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
String name[]=g.getAvailableFontFamilyNames();
Bold=new JCheckBox("加粗",false);
Italic=new JCheckBox("斜体",false);
Text=new JTextArea("字体预览用例\n9876543210\nAaBbCcXxYyZz");
OK=new JButton("确定");
Cancel=new JButton("取消");
Size=new List();
Name=new List();
int i=0;
Name.add("Default Value");
for(i=0;i<name.length;i++) Name.add(name[i]);
for(i=8;i<257;i++) Size.add(String.valueOf(i));
FontName=0;
FontStyle=0;
FontSize=8;
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setSize(480, 306);
this.setVisible(true);
this.setResizable(false);
OK.setFocusable(false);
Cancel.setFocusable(false);
Bold.setFocusable(false);
Italic.setFocusable(false);
Name.setFocusable(false);
Size.setFocusable(false);
Name.setBounds(10, 10, 212, 259);
Container c=this.getContentPane();
c.add(Name);
Bold.setBounds(314, 10, 64, 22);
c.add(Bold);
Italic.setBounds(388, 10, 64, 22);
c.add(Italic);
Size.setBounds(232, 10, 64, 259);
c.add(Size);
Text.setBounds(306, 40, 157, 157);
c.add(Text);
OK.setBounds(306, 243, 74, 26);
c.add(OK);
Cancel.setBounds(390, 243, 74, 26);
c.add(Cancel);
Name.select(FontName);
Size.select(FontSize);
Text.setFont(getFont());
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Name.addItemListener(this);
Size.addItemListener(this);
Bold.addItemListener(this);
Italic.addItemListener(this);
OK.addActionListener(this);
Cancel.addActionListener(this);
this.addWindowListener(this);
}
public void itemStateChanged(ItemEvent e) {
Text.setFont(getFont());
area.setFont(getFont());
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==OK){
FontName=Name.getSelectedIndex();
FontStyle=getStyle();
FontSize=Size.getSelectedIndex();
this.setVisible(false);
}
else cancel();
}
public void windowClosing(WindowEvent e) {
cancel();
}
public Font getFont(){
if(Name.getSelectedIndex()==0) return new Font("新宋体",getStyle(),Size.getSelectedIndex()+8);
else return new Font(Name.getSelectedItem(),getStyle(),Size.getSelectedIndex()+8);
}
public void cancel(){
Name.select(FontName);
Size.select(FontSize);
setStyle();
Text.setFont(getFont());
this.setVisible(false);
}
public void setStyle(){
if(FontStyle==0 || FontStyle==2) Bold.setSelected(false);
else Bold.setSelected(true);
if(FontStyle==0 || FontStyle==1) Italic.setSelected(false);
else Italic.setSelected(true);
}
public int getStyle(){
int bold=0,italic=0;
if(Bold.isSelected()) bold=1;
if(Italic.isSelected()) italic=1;
return bold+italic*2;
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
}
class backgroundEvent implements ActionListener { // "背景颜色"设置
@Override
public void actionPerformed(ActionEvent arg0) {
Color ccc = JColorChooser.showDialog(WriteText.this, "color",
new Color(240, 255, 255));
area.setSelectedTextColor(ccc);
area.setBackground(ccc);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询