为什么粗体+斜体不起作用? [英] why are bold + italic not working?

查看:122
本文介绍了为什么粗体+斜体不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to display the entered text in both italic and bold form at the same time, but for some reason that condition is just not working. The text goes back to the first condition every time I check both the boxes. The code in the book is same as mine and also the codes on various websites seem to be the same too. I can't find the problem.
I tried ^ and + instead of | between the 2 fonts, still not working. This is within the ItemListener subclass:

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
  }

推荐答案

看看你的逻辑

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() )进行评估

if italic.isSelected() then make the font italic, else if bold.isSelected(), make it bold, it's impossible for else if ( bold.isSelected() && italic.isSelected() ) to ever be evaluated

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);
        }

    }

}

这篇关于为什么粗体+斜体不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆