在JOptionPane.showInputDialog中更改字体

时间:2013-11-10 18:03:35

标签: java swing

我正在用java语言编写程序,我想在JOptionPane.showInputDialog的一部分进行一些更改。我的对话是这样的:

JOptionPane.showInputDialog("Total Amount Deposited:\t\t" +
        totalAmount + "\n Enter Coin Value \n" + "(Enter 1 to stop)");

我希望让(Enter 1 to stop)的部分比其他部分小一点。 我是java语言的初学者(大约2个月:D),没有任何其他经验。所以,请保持简单的答案。提前谢谢。

5 个答案:

答案 0 :(得分:0)

JOptionPane将在JLabel中显示文本,该文本支持基本HTML。因此,您需要将文本字符串包装在HTML中,然后您可以使用不同的字体,颜色或其他任何内容。

简单示例:

String text = "<html>Normal text <b>and bold text</b></html>";
JOptionPane.showInputDialog(text);

答案 1 :(得分:0)

创建一个String =“the text”

放入标签pa 使用setFont();

快速举例:

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Test extends JFrame {

    public static void main(String[] args)
    {
        String a = "(enter 1 to stop)";
        JLabel pa = new JLabel();

        JFrame fr = new JFrame();
        fr.setSize(200,200);

        pa.setText(a);
        pa.setFont(pa.getFont().deriveFont(11.0f)); //change the font size from here

        fr.add(pa);
        fr.setVisible(true);
    }
}

答案 2 :(得分:0)

您还可以使用Font.pointSize()中的Font.size()java.awt.Font

答案 3 :(得分:0)

对于JDK 8.x,我发现以下作品可以扩大内置JOptionPane.showInputDialog的大部分部分的字体大小,尤其是按钮,文本框和组合框。

除了我想用粗体显示的两个部分外,它通常是通用的。

当您想要放大一个或两个输入对话框中的99%的输入对话框时,它甚至允许例外(将其视为“所有其他”策略)。

很抱歉格式不正确,但是“代码示例”工具弄乱了所有内容,我没有时间修复它。

import java.awt。*;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

导入java.util.Comparator;

import java.util.List;

导入java.util.Map;

import java.util.Objects;

导入javax.swing。*;

/**
 * Changes the font size used in JOptionPane.showInputDialogs to make them more
 * ADA section 508 compliant by making the text size larger, which is very nice
 * for older people and anyone else with vision problems.
 * 
 * @param fontSize
 *        - size of the font in pixels
 */
private static void makeDialogsEasierToSee(int fontSize)
{
    // This next one is very strange; but, without it,
    // any subsequent attempt to set InternalFrame.titleFont will
    // be ignored, so resist the temptation to remove it.
    JDialog.setDefaultLookAndFeelDecorated(true);

    // define normal and bold fonts that we will use to override the defaults
    Font normalFont = new Font(Font.MONOSPACED, Font.PLAIN, fontSize);
    Font boldFont = normalFont.deriveFont(Font.BOLD);

    // get a list of objects that we can try to adjust font size and style for
    List<Map.Entry<Object, Object>> entries = new ArrayList<>(UIManager.getLookAndFeelDefaults().entrySet());
    // System.out.println(entries.size());
    // remove anything that does NOT involve font selection
    entries.removeIf(filter -> filter.getKey().toString().indexOf(".font") == -1);
    // System.out.println(entries.size());

    // Define a list of font sections of the screen that we do NOT want to
    // enlarge/bold.
    // The following is specific to jKarel so we do not obscure the display of
    // "beeper piles" on the maps.
    List<String> exempt = Arrays.asList("Panel.font");

    // remove anything on the exempt list
    entries.removeIf(filter -> exempt.contains(filter.getKey().toString()));
    // System.out.println(entries.size());

    // optional: sort the final list
    Collections.sort(entries, Comparator.comparing(e -> Objects.toString(e.getKey())));

    // apply normal font to all font objects that survived the filters
    for (Map.Entry<Object, Object> entry : entries)
    {
        String key = entry.getKey().toString();
        // System.out.println(key);
        UIManager.put(key, normalFont);
    }

    UIManager.put("Label.font", boldFont);
    UIManager.put("InternalFrame.titleFont", boldFont);
}

答案 4 :(得分:-1)

您基本上有两个简单的选项 - 切换到JDialog或使用HTML。

JOptionPane用于简单消息或与用户交互。如果你想要打破罐装用例,JDialog是一个更好的选择,随着你变得越来越复杂,你最终可能不得不切换到它。

要满足您的直接用例,您可以发送HTML消息。规则是:

  • 您必须以<html></html>标记开头和结尾。把它们放在中间,没有任何反应。
  • 您必须删除代码中的所有“\ n”。它们不能用于html 无论如何,JPanel尝试使用每一行,如\ n的定义为 单独的HTML文档。切换到

    int totalAmount = 345; //for testing
    
    String message = "<html>"
                   +    "Total Amount Deposited:  " + totalAmount 
                   +    "<br> Enter Coin Value " 
                   +    "<br><span style='font-size:10'>(Enter 1 to stop)</span>"
                   + "</html>";     
    JOptionPane.showInputDialog(message);
    
相关问题