gpt4 book ai didi

java - 为什么粗体 + 斜体不起作用?

转载 作者:行者123 更新时间:2023-12-01 06:43:06 27 4
gpt4 key购买 nike

我试图同时以斜体和粗体形式显示输入的文本,但由于某种原因,该条件不起作用。每次我选中这两个框时,文本都会回到第一个条件。书上的代码和我的一样,而且各个网站上的代码似乎也一样。我找不到问题。
我尝试了 ^ 和 + 而不是 |在两种字体之间,仍然不起作用。这是在 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
}

最佳答案

看看你的逻辑

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

if italic.isSelected() 则将字体设置为斜体,else if 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);
}

}

}

关于java - 为什么粗体 + 斜体不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345162/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com