Java 更改JTextArea字体大小的问题
我试图提示用户从JOptionPane输入内容,以更改JTextArea的字体大小,如下所示为“控制台” 问题: 但是,当我单击size JMenu项时,JOptionPane没有显示 代码:Java 更改JTextArea字体大小的问题,java,swing,jtextarea,Java,Swing,Jtextarea,我试图提示用户从JOptionPane输入内容,以更改JTextArea的字体大小,如下所示为“控制台” 问题: 但是,当我单击size JMenu项时,JOptionPane没有显示 代码: Font font = new Font("Arial", Font.PLAIN, 12); panel = new JPanel(); panel.setLayout(new BorderLayout()); add(panel, BorderLayout.CENTER); JTextArea co
Font font = new Font("Arial", Font.PLAIN, 12);
panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
JTextArea console = new JTextArea();
console.setLineWrap(true);
console.setWrapStyleWord(true);
console.setEditable(false);
console.setFont(font);
JScrollPane scroll = new JScrollPane(console);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(scroll, BorderLayout.CENTER);
JMenuBar bar = new JMenuBar();
panel.add(bar, BorderLayout.NORTH);
JMenu size = new JMenu("Size");
size.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
String fontSize = JOptionPane.showInputDialog(panel, "New font size, 6 or larger:", "Set Font Size", JOptionPane.OK_CANCEL_OPTION);
Font newFont = font.deriveFont(Integer.parseInt(fontSize));
console.setFont(newFont);
}
});
bar.add(size);
当我单击size JMenu项时,JOptionPane的问题没有显示出来,这是因为我们需要显示窗格的容器不正确 试试下面的方法
JOptionPane.showInputDialog(**this**, "New font size, 6 or larger:",
"Set Font Size", JOptionPane.OK_CANCEL_OPTION);
这似乎是一个函数,但您可以使用@TPete在本文中描述的“MenuListener”
以下是他在回答问题时提供的代码:
JMenu menu = new JMenu("MyMenu");
menu.addMenuListener(new MenuListener() {
@Override
public void menuSelected(MenuEvent e) {
System.out.println("menuSelected");
}
@Override
public void menuDeselected(MenuEvent e) {
System.out.println("menuDeselected");
}
@Override
public void menuCanceled(MenuEvent e) {
System.out.println("menuCanceled");
}
});
基本上,他使用的是MenuListener
而不是ActionListener
来成功捕捉事件
希望这有帮助 无法在Jtextarea中更新字体。已经在@VinuBibin中回答了,好的,谢谢,但是如果你阅读我的问题,当点击大小JMenu项目时,它的JOptionPane没有显示使用
JMenuItem
而不是JMenu
也可以使用JMenuItem
而不是JMenu