为什么大胆+斜体不起作用?

时间:2015-09-02 05:21:46

标签: java swing jtextfield

我试图同时以斜体和粗体形式显示输入的文本,但由于某种原因,条件不起作用。每次检查两个框时,文本都会回到第一个条件。书中的代码与我的相同,各种网站上的代码似乎也是一样的。我找不到问题。
我尝试了^和+而不是|两种字体之间,仍然无法正常工作。 这是在ItemListener子类中:

Font f=null; // bold, italic - name of the checkboxes
  public void itemStateChanged(ItemEvent e) {
    if (italic.isSelected() )//1st condition
    f = new Font("Serif", Font.ITALIC, 30);
  else if (bold.isSelected() )
    f = new Font("Serif", Font.BOLD, 30);
  else if ( bold.isSelected() && italic.isSelected() )
    f = new Font("Serif", Font.BOLD | Font.ITALIC, 30);
  else 
    f = new Font("Serif", Font.PLAIN, 30);

  tf.setFont(f);// tf = object of JTextField
  }

2 个答案:

答案 0 :(得分:5)

看看你的逻辑

if (italic.isSelected() )//1st condition
    f = new Font("Serif", Font.ITALIC, 30);
else if (bold.isSelected() )
    f = new Font("Serif", Font.BOLD, 30);
else if ( bold.isSelected() && italic.isSelected() )
    f = new Font("Serif", Font.BOLD | Font.ITALIC, 30);
else 
    f = new Font("Serif", Font.PLAIN, 30);

如果italic.isSelected()然后将字体设为斜体,否则如果bold.isSelected()使其变为粗体,则else if ( bold.isSelected() && italic.isSelected() )无法进行评估

Font Change

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FontTest {

    public static void main(String[] args) {
        new FontTest();
    }

    public FontTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            JTextField field = new JTextField("Bunch a munchy carrots");
            add(field, gbc);

            JToggleButton bold = new JToggleButton("Bold");
            JToggleButton italic = new JToggleButton("Italic");

            ActionListener listener = new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    Font font = field.getFont();
                    if (bold.isSelected() && italic.isSelected()) {
                        font = font.deriveFont(Font.BOLD | Font.ITALIC);
                    } else if (bold.isSelected()) {
                        font = font.deriveFont(Font.BOLD);
                    } else if (italic.isSelected()) {
                        font = font.deriveFont(Font.ITALIC);
                    } else {
                        font = font.deriveFont(Font.PLAIN);
                    }
                    field.setFont(font);
                }
            };

            bold.addActionListener(listener);
            italic.addActionListener(listener);

            add(bold);
            add(italic);
        }

    }

}

答案 1 :(得分:3)

重复:Can we combine 2 font styles together in Java?

将您的代码更改为:

Font f = null; // bold, italic - name of the checkboxes
public void itemStateChanged(ItemEvent e) {
  if (italic.isSelected() )
    f = new Font("Serif", Font.ITALIC, 30);
  else if (bold.isSelected() )
    f = new Font("Serif", Font.BOLD, 30);
  else if ( bold.isSelected() && italic.isSelected() )
    f = new Font("Serif", Font.BOLD + Font.ITALIC, 30);
  else 
    f = new Font("Serif", Font.PLAIN, 30);

  tf.setFont(f);// tf = object of JTextField
}

修改

正如@MadProgrammer指出的那样,这种逻辑也是错误的。如果选择了斜体和粗体,它将在它到达粗体和斜体语句之前进入斜体if语句。

这可以解决您的问题:

Font f = null; // bold, italic - name of the checkboxes
public void itemStateChanged(ItemEvent e) {
  if (italic.isSelected() && !bold.isSelected())
    f = new Font("Serif", Font.ITALIC, 30);
  else if (bold.isSelected() && !italic.isSelected())
    f = new Font("Serif", Font.BOLD, 30);
  else if ( bold.isSelected() && italic.isSelected() )
    f = new Font("Serif", Font.BOLD + Font.ITALIC, 30);
  else 
    f = new Font("Serif", Font.PLAIN, 30);

  tf.setFont(f);// tf = object of JTextField
}