使用jcombobox设置jlabel的大小

6qftjkof  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(621)

这是我第一次将jcombobox用于javagui分配,我有多个jcombobox来修改用户的字符串输入(如字体、颜色和大小),并在gui的jlabel上显示输出。我已经成功地使用字体和颜色组合框修改了输出标签,但是我目前不知道如何设置标签的大小,而不干扰字体和颜色组合框。这是我的密码:

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

public class JavaAssignment implements ActionListener {
    JPanel panel;
    JLabel textLabel, fontTypeLabel, fontSizeLabel, fontColorLabel, resultLabel, newLineLabel;
    JTextField textField;
    JComboBox fontTypeComboBox, fontSizeComboBox, fontColorComboBox;
    JButton okButton, cancelButton;

    GridLayout glay;
    //Declaration of all objects needed on the interface

    public JPanel createPaneContent() {
     panel = new JPanel();

     glay = new GridLayout (6,2,50,50); //setting layout grid arrangement and 50,50 for spaces in between

     panel.setLayout(glay);

     textLabel = new JLabel("Enter Text:");
     textField = new JTextField(10);
     fontTypeLabel = new JLabel("Select font type: ");

     String fontTypes[] = {"Consolas", "Calibri", "Arial", "Times New Roman"};
     fontTypeComboBox = new JComboBox(fontTypes);
     fontTypeComboBox.setSelectedIndex(0);
     fontTypeComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifes font styles, and adding action listener.

     fontSizeLabel = new JLabel("Select font size: ");

     String fontSizes[] = {"Small", "Medium", "Big"};
     fontSizeComboBox = new JComboBox(fontSizes);
     fontSizeComboBox.setSelectedIndex(0);
     fontSizeComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifies font sizes, and adding action listener.

     fontColorLabel = new JLabel("Select font color");

     String fontColors[] = {"Default", "Red", "Green", "Blue"};
     fontColorComboBox = new JComboBox(fontColors);
     fontColorComboBox.setSelectedIndex(0);
     fontColorComboBox.addActionListener(this);
     //Declaring arrays for the JComboBox that modifies the color, and adding action listener.

     resultLabel = new JLabel("Your text will appear here");
     newLineLabel = new JLabel("");
     //newLineLabel is used to occupy space on the right side of resultLabel, to force the OK and Cancel button below. 
     okButton = new JButton("OK");
     okButton.addActionListener(this);
     cancelButton = new JButton("Cancel");
     cancelButton.addActionListener(this);

     panel.add(textLabel);
     panel.add(textField);
     panel.add(fontTypeLabel);
     panel.add(fontTypeComboBox);
     panel.add(fontSizeLabel);
     panel.add(fontSizeComboBox);
     panel.add(fontColorLabel);
     panel.add(fontColorComboBox);
     panel.add(resultLabel);
     panel.add(newLineLabel);
     panel.add(okButton);
     panel.add(cancelButton);
     //adding objects to the GUI all in order 

     return panel;

     //referenced from http://www.macs.hw.ac.uk/guidebook/?name=JComboBox&page=1 
    }

    public void actionPerformed(ActionEvent aE) {

        if (aE.getSource() == okButton) {
            String text; //variable to hold text input from result label
            int temp1 = fontTypeComboBox.getSelectedIndex();
            int temp2 = fontSizeComboBox.getSelectedIndex();
            int temp3 = fontColorComboBox.getSelectedIndex();
            //temporary variables initialized to get selected value from JComboBoxes. 
            text = (textField.getText()); //variable to hold string input from textField

            resultLabel.setText("" + text); //setting the modified text to the resultLabel to display in textField on the GUI

            if (temp1 == 0) {
                Font consolasFont = new Font("Consolas", Font.PLAIN, 15);
                resultLabel.setFont(consolasFont);
            } else if (temp1 == 1) {
                Font calibriFont = new Font("Calibri", Font.PLAIN, 15);
                resultLabel.setFont(calibriFont);
            } else if (temp1 == 2) {
                Font arialFont = new Font("Arial", Font.PLAIN, 15);
                resultLabel.setFont(arialFont);
            } else if (temp1 == 3) {
                Font timesNewRomanFont = new Font("Times New Roman", Font.PLAIN, 15);
                resultLabel.setFont(timesNewRomanFont);
            } //end font type if statement

            if (temp2 ==0) {
                resultLabel.setFont(temp1, Font.PLAIN, 10);
            } //this if statement definitely will not work. 

            if (temp3 == 0) {
                resultLabel.setForeground(Color.BLACK);
            } else if (temp3 == 1) {
                resultLabel.setForeground(Color.RED);
            } else if (temp3 == 2) {
                resultLabel.setForeground(Color.GREEN);
            } else if (temp3 == 3) {
                resultLabel.setForeground(Color.BLUE);
            } //end font color statement

        } else if (aE.getSource() == cancelButton) {
            System.exit(0);
        } //end fontTypeComboBox if statement
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Font Modifier");
        frame.setSize(600,500);

        JavaAssignment fontModifier = new JavaAssignment();

        frame.setContentPane(fontModifier.createPaneContent());
        frame.setVisible(true);
    }
}

我尝试创建一个类似于我的字体类型if语句的对象,但是如果我在jcombobox中选择一个大小,它将覆盖标签的字体类型。我试图使用“resultlabel.settext()”方法更改输出标签,但也不起作用。任何帮助或想法将不胜感激

fcipmucu

fcipmucu1#

我会这么做。
字体大小不使用字符串,而是使用整数。

Integer fontSizes[] = {10, 12, 14, 16, 18, 20, 22};

而不是 getSelectedIndex() 使用 getSelectedItem() . 不过,您需要强制转换返回值。所以你可以这样做

fontString = (String)fontTypeComboBox.getSelectedItem();
fontSize = (int)fontSizeComboBox.getSelectedItem();
...
resultLabel.setFont(new Font(fontString, Font.PLAIN, fontSize));

以下是我所做的更改,它似乎工作正常

Integer fontSizes[] = {10, 12, 14, 16, 18, 20, 22};
fontSizeComboBox = new JComboBox(fontSizes);
....
public void actionPerformed(ActionEvent aE) {

    if (aE.getSource() == okButton) {
        String text; //variable to hold text input from result label
        String fontString = (String)fontTypeComboBox.getSelectedItem();
        int fontSize = (int)fontSizeComboBox.getSelectedItem();
        int temp3 = fontColorComboBox.getSelectedIndex();
        //temporary variables initialized to get selected value from JComboBoxes. 
        text = (textField.getText()); //variable to hold string input from textField

        resultLabel.setText("" + text); //setting the modified text to the resultLabel to display in textField on the GUI

        resultLabel.setFont(new Font(fontString, Font.PLAIN, fontSize));

        if (temp3 == 0) {
            resultLabel.setForeground(Color.BLACK);
        } else if (temp3 == 1) {
            resultLabel.setForeground(Color.RED);
        } else if (temp3 == 2) {
            resultLabel.setForeground(Color.GREEN);
        } else if (temp3 == 3) {
            resultLabel.setForeground(Color.BLUE);
        } //end font color statement

    } else if (aE.getSource() == cancelButton) {
        System.exit(0);
    } //end fontTypeComboBox if statement
}

相关问题