下面是FontSet类的代码,当点击“字体”菜单项时,会弹出这样的窗口(这时鼠标还没有移进这个窗口)当把鼠标移进这个窗口时,窗口会变成这样
package notebook1;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
/**
*
*
* @author 逄淑越
* @version 1.0.0 2015/04/18
*/
public class FontSet extends JDialog implements ItemListener,ActionListener
{
private TextAreaUesdInAllClass textareausedinallclassinfontset;
private String selectedfontname="宋体";
private int selectedfontstyle=Font.PLAIN;
private int selectedfontsize=12;
private List fontnamelist;
private List fontstylelist;
private List fontsizelist;
private String fontnames[];
private String fontstyles[]={"常规","粗体","斜体","斜粗体"};
private String fontsizes[]={"8","9","10","11","12","13","14","15","16"};
JButton okbutton;
JButton nobutton;
private String selectedfontstyleindex;
private String selectedfontsizeindex;
public FontSet()
{
fontNameArea();
fontStyleArea();
fontSizeArea();
buttonArea();
setVisible(false);
setBounds(100,100,500,400);
}
private void fontNameArea()
{
SetFontNameLabel();
setFontNameTextField();
setFontNameList();
}
private void SetFontNameLabel()
{
JLabel fontnamelabel=new JLabel("字体");
fontnamelabel.setBounds(20,10,60,20);
add(fontnamelabel);
}
private void setFontNameTextField()
{
JTextField fontnametextfield=new JTextField("宋体");
fontnametextfield.setText(selectedfontname);
fontnametextfield.setBounds(20,35,150,20);
add(fontnametextfield);
}
private void setFontNameList()
{
fontnamelist=new List(7,false);
GraphicsEnvironment graphicsenvironment=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontnames[]=graphicsenvironment.getAvailableFontFamilyNames();
for(String fontname:fontnames)
{
fontnamelist.add(fontname);
}
fontnamelist.setBounds(20,60,150,180);
fontnamelist.addItemListener(this);
add(fontnamelist);
}
private void fontStyleArea()
{
setFontStyleLabel();
setFontStyleTextField();
setFontStyleList();
}
private void setFontStyleLabel()
{
JLabel fontstylelabel=new JLabel("字形");
fontstylelabel.setBounds(180,10,60,20);
add(fontstylelabel);
}
private void setFontStyleTextField()
{
JTextField fontstyletextfield=new JTextField("常规");
fontstyletextfield.setText(selectedfontstyleindex);
fontstyletextfield.setBounds(180,35,110,20);
add(fontstyletextfield);
}
private void setFontStyleList()
{
fontstylelist=new List(7,false);
for(String fontstyle:fontstyles)
{
fontstylelist.add(fontstyle);
}
fontstylelist.setBounds(180,60,110,180);
fontstylelist.addItemListener(this);
add(fontstylelist);
}
private void fontSizeArea()
{
setFontSizeLabel();
setFontSizeTextField();
setFontSizeList();
}
private void setFontSizeLabel()
{
JLabel fontsizelabel=new JLabel("大小");
fontsizelabel.setBounds(300,10,60,20);
add(fontsizelabel);
}
private void setFontSizeTextField()
{
JTextField fontsizetextfield=new JTextField("12");
fontsizetextfield.setText(selectedfontsizeindex);
fontsizetextfield.setBounds(300,35,80,20);
add(fontsizetextfield);
}
private void setFontSizeList()
{
fontsizelist=new List(10,false);
for(String fontsize:fontsizes)
{
fontsizelist.add(fontsize);
}
fontsizelist.setBounds(300,60,80,180);
fontsizelist.addItemListener(this);
add(fontsizelist);
}
private void fontShowArea()
{
setFontShowLabel();
setFontShowField();
}
private void setFontShowLabel()
{
JLabel fontshowlabel=new JLabel("示例");
fontshowlabel.setBounds(180,250,60,20);
add(fontshowlabel);
}
private void setFontShowField()
{
JTextField fontshowtextfield=new JTextField("AaBbCc");
fontshowtextfield.setEditable(false);
fontshowtextfield.setBounds(180,275,200,60);
Font fontofshowtextfield=new Font(selectedfontname,selectedfontstyle,selectedfontsize);
fontshowtextfield.setFont(fontofshowtextfield);
add(fontshowtextfield);
}
private void buttonArea()
{
addButton();
}
private void addButton()
{
okbutton=new JButton("确定");
nobutton=new JButton("取消");
okbutton.setBounds(400,35,70,20);
nobutton.setBounds(400,65,70,20);
okbutton.addActionListener(this);
nobutton.addActionListener(this);
add(okbutton);
add(nobutton);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==fontnamelist)
{
int fontnameindex=1;
fontnameindex=fontnamelist.getSelectedIndex();
selectedfontname=fontnames[fontnameindex];
}
if(e.getSource()==fontstylelist)
{
int fontstyleindex=1;
fontstyleindex=fontstylelist.getSelectedIndex();
selectedfontstyleindex=fontstyles[fontstyleindex];
switch(selectedfontstyleindex)
{
case "常规":
selectedfontstyle=Font.PLAIN;
break;
case "粗体":
selectedfontstyle=Font.BOLD;
break;
case "斜体":
selectedfontstyle=Font.ITALIC;
break;
case "粗斜体":
selectedfontstyle=Font.BOLD+Font.ITALIC;
break;
}
}
if(e.getSource()==fontsizelist)
{
selectedfontsizeindex=fontsizelist.getSelectedItem();
selectedfontsize=Integer.parseInt(selectedfontsizeindex);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==okbutton)
{
Font font=new Font(selectedfontname,selectedfontstyle,selectedfontsize);
textareausedinallclassinfontset.textarea.setFont(font);
}
else
{
}
}
}
------解决思路----------------------
在构造函数的开头增加一句
public FontSet()
{
this.setLayout(null);//就是这句