**出现以下错误:
1、可以改变对话框中字体大小,无法改变字体种类。
2、且无法将改变的效果传递到主窗口。
3、对话框setModel的值为true,还是能点到主窗口
4、可以通过下拉框可改变标签字体大小,但是为什么我改变了字体大小之后,我点那个字体种类的下拉框没有出现下拉列表呢?是不是两个下拉列表有冲突呢?
**
以下代码分别在4个java文件中
java程序,编写一个JDialog的子类FontDialog,该类为FontFamily对象维护的数据视图,要求FontDialog对象使用下拉列表显示FontFamily对象维护的全部字体的名称,当选择下拉列表中某个字体名称后,FongDialog对象使用标签显示字体的效果。要求对话框提供返回下拉列表所选择的字体名称的方法
编写一个窗口,该窗口有“设置字体”按钮和一个文本区对象,当单击按钮时,弹出一个FontDialog对话框,然后根据用户在下拉列表中选择的字体来显示文本区中的文本。
package test;
public class E9_2 {
public static void main(String args[]){
window1 window = new window1();
}
}
package test;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;
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.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fontdialog extends JDialog implements ActionListener,ItemListener{
JComboBox name,size;
JPanel north,center,south;
JButton confirm,cancel;
Label label;
String allnames[];
int sizes[];
String font_name = "Adobe Arabic";
int font_size = 24;
Font f,font;
int tag;
Fontdialog(){
setModal(true);
setBounds(400,400,700,400);
setLayout(new BorderLayout());
setTitle("字体对话框");
f = new Font(font_name,1,font_size);
name = new JComboBox();
size = new JComboBox();
name.setMaximumRowCount(5);
size.setMaximumRowCount(5);
createitem();
north = new JPanel();
south = new JPanel();
center = new JPanel();
confirm = new JButton("Yes");
cancel = new JButton("Cancel");
label = new Label("hello,Java程序");
label.setFont(f);
north.setLayout(new FlowLayout());
center.setLayout(new BorderLayout());
south.setLayout(new FlowLayout());
north.add(name);
north.add(size);
center.add(label,BorderLayout.CENTER);
south.add(confirm);
south.add(cancel);
confirm.addActionListener(this);
cancel.addActionListener(this);
name.addItemListener(this);/*new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
font_name = name.getSelectedItem().toString();
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
validate();
}
}
});*/
size.addItemListener(this);/*new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange() == ItemEvent.SELECTED){
font_size = Integer.parseInt(size.getSelectedItem().toString());
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
validate();
}
}
});*/
name.addActionListener(this);
size.addActionListener(this);
add(north,BorderLayout.NORTH);
add(south,BorderLayout.SOUTH);
add(center,BorderLayout.CENTER);
}
void createitem(){
allnames = new FontFamilyNames().getFontName();
for(int i=0;i<allnames.length;i++){
name.addItem(allnames[i]);
}
sizes = new int[80];
for(int i=0;i<sizes.length;i++){
sizes[i]=i+1;
}
for(int i:sizes){
size.addItem(String.valueOf(i));
}
}
public String get_fontname(){
return font_name;
}
public int get_fontsize(){
return font_size;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cancel){
this.dispose();
}
if(e.getSource()== confirm){
tag = 1;
this.setVisible(false);
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==name){
font_name = (String)name.getSelectedItem();
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
}
if(e.getSource()==size){
font_size = Integer.parseInt(size.getSelectedItem().toString());
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
}
}
}
package test;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class window1 extends JFrame implements ActionListener {
JTextArea text;
JButton button;
String fontname;
int fontsize;
Font font;
window1(){
text = new JTextArea();
button.addActionListener(this);
init();
setVisible(true);
setBounds(200,200,800,400);
validate();
}
void init(){
Font font = text.getFont();
text.setText("Java 2实用教程(第四版)");
text.setFont(new Font(font.getName(), font.getStyle(), 60));
button = new JButton("设置字体");
setLayout(new BorderLayout());
add(button,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== button){
Fontdialog fontdialog = new Fontdialog();
fontdialog.setVisible(true);
if(fontdialog.tag == 1){
fontsize = fontdialog.get_fontsize();
fontname = fontdialog.get_fontname();
font = new Font(fontname,1,fontsize);
text.setFont(font);
text.repaint();
validate();
}
}
}
}
package test;
import java.awt.GraphicsEnvironment;
public class FontFamilyNames {
String allFontNames[];
public String [] getFontName() {
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
allFontNames=ge.getAvailableFontFamilyNames();
return allFontNames;
}
}