hockeygoalie5
In Runtime
- Messages
- 299
- Location
- United States
Okay, so I made this. It is supposed to detect that if the font is bold or italic while the other font change button is pressed and make the font both bold and italic. But this is ignored and when the button is pressed, it is only one font.
Code:
package org.gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GUI1 extends JFrame {
private static final long serialVersionUID = -8003666480923795663L;
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setVisible(true);
frame.setSize(500, 100);
final Font boldFont = new Font("Bold", Font.BOLD, 12);
final Font italic = new Font("Italic", Font.ITALIC, 12);
final Font boldItalic = new Font("Bold Italic", Font.BOLD+Font.ITALIC, 12);
final JLabel label = new JLabel("JLable");
JButton button = new JButton("Change color");
JButton bold = new JButton("Toggle bold");
JButton italicButton = new JButton("Toggle italic");
label.setFont(null);
label.setForeground(Color.BLUE);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if(label.getForeground() == Color.BLUE) {
label.setForeground(Color.RED);
}
else if(label.getForeground() == Color.RED) {
label.setForeground(Color.BLUE);
}
}
});
bold.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if(label.getFont() != boldFont) {
label.setFont(boldFont);
}
else if(label.getFont() == boldFont) {
label.setFont(null);
}
else if(label.getFont() == italic) {
label.setFont(boldItalic);
}
else if(label.getFont() == boldItalic) {
label.setFont(italic);
}
}
});
italicButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
if(label.getFont() != italic) {
label.setFont(italic);
}
else if(label.getFont() == italic) {
label.setFont(null);
}
else if(label.getFont() == boldFont) {
label.setFont(boldItalic);
}
else if(label.getFont() == boldItalic) {
label.setFont(boldFont);
}
}
});
frame.add(label);
frame.add(button);
frame.add(bold);
frame.add(italicButton);
}
}