上一个
下一个

Java:图形用户界面案例:统计字符数并显示直方图

0.前言

摆了两天没怎么干活,不过上一篇说的监听器已经更新了——Java这玩意还蛮好用的。这薄薄的一本厚书也要被我看得差不多了,还剩两章看完差不多就能说自己是Java这门语言上的一个小学生了,可喜可贺可喜可贺。之后是继续学习Java,还是unity入个门就要看心情了。不过深度学习这玩意是不会学的了。

今天分享图形化界面的一个小案例,在这个案例中可以看到监听器的使用,图形绘制,还有一些GUI组件的使用。还是蛮不错的。注释我是写了的,但是懒得切换输入法所以写的是英文注释,不过应该没关系。先上效果图:

1.Histogram类

				
					import javax.swing.*;
import java.awt.*;

public class Histogram extends JPanel {
    private int[] count;
    public void showHistogram(int[] count)
    {
        this.count=count;
        repaint();
    }

    protected void paintComponent(Graphics g)
    {
        if(count==null) return;

        super.paintComponent(g);

        int width = getWidth();
        int height = getHeight();
        int interval = (width-40)/count.length;
        int individualWidth = (int)(((width-40)/24)*0.60);

        //Find max count to decide the highest bar
        int maxcount=0;
        for(int i:count)
        {
            if(maxcount<i)
            {
                maxcount=i;
            }
        }

        //x is the start position for the first bar in the histogram
        int x = 30;

        //draw
        g.drawLine(10,height-45,width-10,height-45);
        for(int i=0;i<count.length;i++)
        {
            int barheight = (int)(((double)count[i]/(double) maxcount)*(height-55));

            g.drawRect(x,height-45-barheight,individualWidth,barheight);
            g.drawString((char)(65+i)+"",x,height-30);

            x+=interval;
        }

    }
    public Dimension getPreferredSize()
    {
        return new Dimension(300,300);
    }
}

				
			

2.MultipleWindowDemo类(主函数入口)

				
					import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MultipleWindowDemo extends JFrame {
    private JTextArea jta;
    private JButton jbtShowHisToGram = new JButton( "show histogram");
    private Histogram histogram = new Histogram();

    //new frame window to show
    private JFrame histogramFrame = new JFrame();

    public MultipleWindowDemo(){
        //store test area in a scroll area
        JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
        scrollPane.setPreferredSize(new Dimension(300,300));
        jta.setWrapStyleWord(true);
        jta.setLineWrap(true);

        //Place scroll and button
        add(scrollPane,BorderLayout.CENTER);
        add(jbtShowHisToGram,BorderLayout.SOUTH);

        //listener
        jbtShowHisToGram.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int[] count = countLetters();

                histogram.showHistogram(count);
                histogramFrame.setVisible(true);
            }
        });

        histogramFrame.add(histogram);
        histogramFrame.pack();
        histogramFrame.setTitle("Histogram");
    }

    private int[] countLetters(){
        int[] count = new int[26];
        String text = jta.getText();

        for(int i=0;i<text.length();i++)
        {
            char character = text.charAt(i);

            if((character>='A')&&(character<='Z'))
                count[character-'A']++;
            if((character>='a')&&(character<='z'))
                count[character-'a']++;
        }
        return count;
    }

    public static void main(String[] args) {
        MultipleWindowDemo frame = new MultipleWindowDemo();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("CountCharNumberAndShow");
        frame.pack();
        frame.setVisible(true);
    }
}
				
			

3.小结与碎碎念

Java提供了一套十分便利的GUI组件,可以迅速搭建一套看得过去的图形用户界面。

PS:我还没阳,不知道能苟到什么时候。

订阅评论
提醒
0 评论
内联反馈
查看所有评论

《Java:图形用户界面案例:统计字符数并显示直方图》

0
希望看到您的想法,请您发表评论x