cgamaz1ng 2015-12-07 16:04 采纳率: 25%
浏览 1645
已采纳

初学Java,一些事件处理问题,help!!!!!!

**出现以下错误:

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;
    }
}
  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2015-12-08 05:51
    关注

    你的程序抛出异常了,在window的构造函数中,button没有new操作,就添加事件监听了。
    修正,把所有成员变量的初始化放在init函数中完成,修正代码如下:

        public window1() {
            init();
            setVisible(true);
            setBounds(200, 200, 800, 400);
            validate();
        }
    
        void init() {
            text = new JTextArea();
            Font font = text.getFont();
            text.setText("Java 2实用教程(第四版)");
            text.setFont(new Font(font.getName(), font.getStyle(), 60));
    
            button = new JButton("设置字体");
            button.addActionListener(this);
            setLayout(new BorderLayout());
            add(button, BorderLayout.NORTH);
            add(text, BorderLayout.CENTER);
            System.out.println("init ok.");
        }
    

    测试了下,你的代码基本上完成了上述功能了。从java编码规范上来说,建议类名称定义为首字母大写,而且类名称要有实际意义。
    即你的window1的类名不合规范,可以修正为有意义的名称,例如:MainWindow。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退