使用JSlider调整字体大小

时间:2017-10-26 13:35:45

标签: java eclipse jslider

希望有人可以提供帮助。我试图插入一个JSlider,所以它将调整字体大小,但我坚持编码。用户应输入成绩,然后在图表字段中显示该条目。完成此操作后,用户应该能够使用JSlider调整字体大小。

谢谢你的期待。

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

public class Grades extends JFrame implements ActionListener {
    //INSERT SLIDER
    private JTextField gradeAField = new JTextField(5);
    private JTextField gradeBField = new JTextField(5);
    private JTextField gradeCField = new JTextField(5);
    private JTextField gradeDField = new JTextField(5);
    private JTextField gradeEField = new JTextField(5);
    private JTextField spaceField = new JTextField(5);
    private int gradeAs;
    private int gradeBs;
    private int gradeCs;
    private int gradeDs;
    private int gradeEs;
    private JButton displayChartButton = new JButton("Display bar chart");
    private JButton displayClearData = new JButton("Clear Data Fields");
    private JPanel chartPanel = new JPanel();
    private Color chartPanelColor = Color.cyan;
    private final Font labelFont = new Font("Serif", Font.BOLD, 16);
    private final int chartLeftX = 30;
    private final int chartTopY = 75;
    private int textSpace = 30;
    private int textSpaceY = 39;
    int boxLeftX = 20;
    int boxTopY = 50;
    int displayWidth = 400;
    int displayHeight = 200;
    int borderPatternY = 5;
    int borderPatternX = 5;
    private JSlider slider;

    public static void main(String[] args) {

        Grades chart = new Grades();
        chart.setSize(550, 550);
        chart.createGUI();
        chart.setVisible(true);
        chart.setResizable(false);

    }

    private void createGUI() {

        // Set up main window characteristics
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());
        setTitle("Grades Calculator - 2211956");

        //Sets up the Slider for the height of text.
        slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
        window.add(slider);

        // Text field for entry of number of grades As
        JLabel gradeAFieldLabel = new JLabel("# of grade As:");
        gradeAFieldLabel.setFont(labelFont);
        window.add(gradeAFieldLabel);
        window.add(gradeAField);

        // Text field for entry of number of grades Bs
        JLabel gradeBFieldLabel = new JLabel("# of grade Bs:");
        gradeBFieldLabel.setFont(labelFont);
        window.add(gradeBFieldLabel);
        window.add(gradeBField);

        // Text field for entry of number of grades Cs
        JLabel gradeCFieldLabel = new JLabel("# of grade Cs:");
        gradeCFieldLabel.setFont(labelFont);
        window.add(gradeCFieldLabel);
        window.add(gradeCField);

        // Text field for entry of number of grades Ds
        JLabel gradeDFieldLabel = new JLabel("# of grade Ds:");
        gradeDFieldLabel.setFont(labelFont);
        window.add(gradeDFieldLabel);
        window.add(gradeDField);

        // Text field for entry of number of grades Es
        JLabel gradeEFieldLabel = new JLabel("# of grade Es:");
        gradeEFieldLabel.setFont(labelFont);
        window.add(gradeEFieldLabel);
        window.add(gradeEField);

        // Text field for entry of number of grades Es
        JLabel spaceFieldLabel = new JLabel("                     ");
        spaceFieldLabel.setFont(labelFont);
        window.add(spaceFieldLabel);

        // Set up button to request chart display
        window.add(displayChartButton);
        displayChartButton.addActionListener(this);

        // set up button to request clear data field
        window.add(displayClearData);
        displayClearData.addActionListener(this);

        // Set up the panel for drawing on
        chartPanel.setPreferredSize(new Dimension(450, 300));
        chartPanel.setBackground(chartPanelColor);
        window.add(chartPanel);

    }

    public static void changeFont(Component component, int fontSize) {
        Font f = component.getFont();
        component.setFont(new Font(f.getName(), f.getStyle(), f.getSize() + fontSize));
        if (component instanceof Container) {
            for (Component child : ((Container) component).getComponents()) {
                changeFont(child, fontSize);
            }
        }
    }

    public void actionPerformed(ActionEvent e) {

        {
            Graphics g = chartPanel.getGraphics();

            g.drawRect(boxLeftX, boxTopY, displayWidth, displayHeight);

            for (int count = 0; borderPatternX < displayHeight; count++) {
                //draws horizontal lines
                g.drawLine(boxLeftX, borderPatternX + boxTopY, boxLeftX + displayWidth, borderPatternX + boxTopY);
                borderPatternX = borderPatternX + 5;
            }

            for (int horizontal = 0; borderPatternY < displayWidth; horizontal++) {
                // draws vertical lines
                g.drawLine(borderPatternY + boxLeftX, boxTopY, borderPatternY + boxLeftX, boxTopY + displayHeight);
                borderPatternY = borderPatternY + 5;
            }

            g.setColor(Color.white);
            g.fillRect(boxLeftX + 5, boxTopY + 5, displayWidth - 10, displayHeight - 10);

            g.setColor(Color.black);
            g.drawLine(boxLeftX + 5, (boxTopY + 5) + 38, (boxLeftX + displayWidth - 5), (boxTopY + 5) + 38);
            g.drawLine(boxLeftX + 5, (boxTopY + 5) + 76, (boxLeftX + displayWidth - 5), (boxTopY + 5) + 76);
            g.drawLine(boxLeftX + 5, (boxTopY + 5) + 114, (boxLeftX + displayWidth - 5), (boxTopY + 5) + 114);
            g.drawLine(boxLeftX + 5, (boxTopY + 5) + 152, (boxLeftX + displayWidth - 5), (boxTopY + 5) + 152);
        }


        if (e.getSource() == displayChartButton) {
            checkAndRecordData();
            Graphics g = chartPanel.getGraphics();
            g.setColor(Color.black);

            for (int i = 0; i < gradeAs; i++) {
                g.drawString("A", chartLeftX + textSpace * i, chartTopY);
            }
        }

        if (e.getSource() == displayChartButton) {
            checkAndRecordData();
            Graphics g = chartPanel.getGraphics();
            g.setColor(Color.black);

            for (int i = 0; i < gradeBs; i++) {
                g.drawString("B", chartLeftX + textSpace * i, chartTopY + textSpaceY);
            }
        }

        if (e.getSource() == displayChartButton) {
            checkAndRecordData();
            Graphics g = chartPanel.getGraphics();
            g.setColor(Color.black);

            for (int i = 0; i < gradeCs; i++) {
                g.drawString("C", chartLeftX + textSpace * i, chartTopY + textSpaceY * 2);
            }
        }

        if (e.getSource() == displayChartButton) {
            checkAndRecordData();
            Graphics g = chartPanel.getGraphics();
            g.setColor(Color.black);

            for (int i = 0; i < gradeDs; i++) {
                g.drawString("D", chartLeftX + textSpace * i, chartTopY + textSpaceY * 3);
            }
        }

        if (e.getSource() == displayChartButton) {
            checkAndRecordData();
            Graphics g = chartPanel.getGraphics();
            g.setColor(Color.black);

            for (int i = 0; i < gradeEs; i++) {
                g.drawString("E", chartLeftX + textSpace * i, chartTopY + textSpaceY * 4);
            }
        }

        if (e.getSource() == displayClearData) {
            checkAndRecordData();
            gradeAField.setText("");
            gradeBField.setText("");
            gradeCField.setText("");
            gradeDField.setText("");
            gradeEField.setText("");
            chartPanel.repaint();
        }
    }

    private void checkAndRecordData() {

        int tempAs = 0;
        tempAs = Integer.parseInt(gradeAField.getText());
        gradeAs = tempAs;

        int tempBs = 0;
        tempBs = Integer.parseInt(gradeBField.getText());
        gradeBs = tempBs;

        int tempCs = 0;
        tempCs = Integer.parseInt(gradeCField.getText());
        gradeCs = tempCs;

        int tempDs = 0;
        tempDs = Integer.parseInt(gradeDField.getText());
        gradeDs = tempDs;

        int tempEs = 0;
        tempEs = Integer.parseInt(gradeEField.getText());
        gradeEs = tempEs;
    }

    public void stateCahnged(ChangeEvent e) {

        int fontSize = sliderFontSet.getValue();

        if (!((JSlider) e.getSource()).getValueIsAdjusting()) {
            changeFont(this, sliderFontSet.getValue() - slider);
            slider = sliderFontSet.getValue();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你的上一个功能

public void stateCahnged(ChangeEvent e) {

        int fontSize = sliderFontSet.getValue();

        if (!((JSlider) e.getSource()).getValueIsAdjusting()) {
            changeFont(this, sliderFontSet.getValue() - slider);
            slider = sliderFontSet.getValue();
        }
    }
如果不是stateChanged(ChangeEvent e),

拼写错误?也许是因为姓名不正确而被调用。