这是我第一次使用JComboBox进行Java GUI分配,我有多个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 if的对象,但如果我要在该JComboBox中选择一个大小,那将覆盖标签的字体类型。我尝试使用" resultLabel.setText()"更改输出标签。方法,但它也没有工作。任何帮助或想法将不胜感激
答案 0 :(得分:1)
这就是我要做的事。
而不是使用字符串大小的字符串使用整数。
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
}