我尝试了很多东西,我只能将字体放在列表中,但是如何在选择它们时使它们有效?
这是我的代码
package textModifier;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Handler;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.*;
public class javaTextEditor extends JComboBox implements ActionListener, ItemListener {
JFrame frame = new JFrame("A text modifier frame - Assignment 1");
Border layout = BorderFactory.createLineBorder(Color.RED, 1);
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JLabel label1 = new JLabel("Enter Message:");
JLabel label2 = new JLabel("Select Font:");
JLabel label3 = new JLabel();
JTextField text = new JTextField("UIPG - Assignment 1");
JComboBox fonts;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[] = ge.getAvailableFontFamilyNames();
Vector vector = new Vector();
ButtonGroup radio = new ButtonGroup();
JRadioButton red = new JRadioButton("Red");
JRadioButton green = new JRadioButton("Green");
JRadioButton blue = new JRadioButton("Blue");
JCheckBox Capitalized, Bold, Italic;
JButton Left, Center, Right;
public void ui(){
for (int i = 1; i < fontName.length; i++)
vector.addElement(fontName[i]);
fonts = new JComboBox(vector); // wwwwqput the font names on the ComboBox
Capitalized = new JCheckBox("Capitalized");
Bold = new JCheckBox("Bold");
Italic = new JCheckBox("Italic");
Left = new JButton("Left");
Center = new JButton("Center");
Right = new JButton("Right");
radio.add(red);
radio.add(green);
radio.add(blue);
panel.add(label1);
panel.add(text);
panel.add(label2);
panel.add(fonts);
panel3.add(red);
panel3.add(green);
panel3.add(blue);
panel2.add(Capitalized);
panel2.add(Bold);
panel2.add(Italic);
panel4.add(Left);
panel4.add(Center);
panel4.add(Right);
panel5.setBorder(layout);
panel5.setBackground(Color.YELLOW);
panel5.add(label3);
Left.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Left)
label3.setLocation(10, 8);
}
});
Center.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Center)
label3.setLocation(245, 8);
}
});
Right.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == Right)
label3.setLocation(480, 8);
}
});
red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(red.isSelected())
label3.setForeground(Color.red);
}
});
green.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(green.isSelected())
label3.setForeground(Color.green);
}
});
blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(blue.isSelected())
label3.setForeground(Color.blue);
}
});
text.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String str = text.getText();
label3.setText(str);
}
});
panel5.add(label3);
frame.setVisible(true);
frame.setSize(800, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel3.setLayout(new BoxLayout(panel3, BoxLayout.Y_AXIS));
panel4.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.getContentPane().add(BorderLayout.NORTH, panel);
frame.getContentPane().add(BorderLayout.EAST, panel2);
frame.getContentPane().add(BorderLayout.WEST, panel3);
frame.getContentPane().add(BorderLayout.SOUTH, panel4);
frame.getContentPane().add(BorderLayout.CENTER, panel5);
Bold.addItemListener(this);
Italic.addItemListener(this);
Capitalized.addItemListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void itemStateChanged(ItemEvent e) {
Font f = null;
if(Bold.isSelected() && Italic.isSelected() && Capitalized.isSelected()){
f = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
label3.setText(label3.getText().toUpperCase());
}
else if(Bold.isSelected())
f = new Font("Serif", Font.BOLD, 14);
else if(Italic.isSelected())
f = new Font("Serif", Font.ITALIC, 14);
else if(Capitalized.isSelected())
label3.setText(label3.getText().toUpperCase());
else{
f = new Font("Serif", Font.PLAIN, 14);
}
label3.setFont(f);
}
}
我应该在ActionListener中写什么才能使字体有效?
答案 0 :(得分:3)
由于您没有发布此尝试(只是一个空方法),我会尝试指出正确的方向。
首先,您应该看一下Font API。在那里你会看到像
这样的方法因此,在您的监听器中,首先获取您想要更改字体的字体。
Font font = label.getFont();
然后使用上面的getter获取值并根据从组合框中收到的新值创建/设置新字体。像
这样的东西String family = (String)combobox.getSelectedItem();
Font font = label.getFont();
int size = font.getSize();
int style = font.getStyle();
label.setFont(new Font(family, size, style));
你也可以为你的复选框使用相同的技术,因为我发现你很难对这个家庭进行编码。
旁注:
使用java命名约定 - 变量以小写字母开头Bold
→bold
此外,我完全不知道为什么你的类正在扩展JComboBox。看起来像一个完全错误的设计