Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JOptionPane中将邮件拆分为两行或多行_Java_Swing_Newline_Joptionpane - Fatal编程技术网

Java 在JOptionPane中将邮件拆分为两行或多行

Java 在JOptionPane中将邮件拆分为两行或多行,java,swing,newline,joptionpane,Java,Swing,Newline,Joptionpane,当出现错误时,它会显示一个比计算机屏幕宽度长的长JOptionPane消息框。如何将e.toString()拆分为两个或多个部分。您必须使用\n将字符串拆分为不同的行。或者你可以: 完成此任务的另一种方法是对JOptionPane进行子类化 类并重写getMaxCharactersPerLineCount,使其返回 要表示为的最大字符数 一行文字 → (死链接,请参阅)。 import javax.swing.*; 类固定宽度标签{ 公共静态void main(字符串[]args){ 可运行r

当出现错误时,它会显示一个比计算机屏幕宽度长的长JOptionPane消息框。如何将e.toString()拆分为两个或多个部分。

您必须使用
\n
将字符串拆分为不同的行。或者你可以:

完成此任务的另一种方法是对JOptionPane进行子类化 类并重写getMaxCharactersPerLineCount,使其返回 要表示为的最大字符数 一行文字

→ (死链接,请参阅)。

import javax.swing.*;
类固定宽度标签{
公共静态void main(字符串[]args){
可运行r=()->{
String html=“标签宽度”
+“许多Swing组件支持HTML3.2&;”
+“(简单)CSS。通过设置正文宽度,我们可以
+“用于查找所需自然高度的构件”
+“显示组件。

” +“此文本中的正文宽度设置为%1s像素。”; //改变宽度 int w=175; showMessageDialog(null,String.format(html,w,w)); }; SwingUtilities.invokeLater(r); } }
我正在设置字符限制,然后在该环境中搜索最后一个空格字符,并在那里写一个“\n”。(如果没有空格字符,则强制使用“\n”)。像这样:

import javax.swing.*;

class FixedWidthLabel {

    public static void main(String[] args) {
        Runnable r = () -> {
            String html = "<html><body width='%1s'><h1>Label Width</h1>"
                + "<p>Many Swing components support HTML 3.2 &amp; "
                + "(simple) CSS.  By setting a body width we can cause "
                + "the component to find the natural height needed to "
                + "display the component.<br><br>"
                + "<p>The body width in this text is set to %1s pixels.";
            // change to alter the width 
            int w = 175;

            JOptionPane.showMessageDialog(null, String.format(html, w, w));
        };
        SwingUtilities.invokeLater(r);
    }
}
与的答案类似,下面的代码让您从项目根目录加载
HTML
文件,并将其显示在
JOptionPane
中。请注意,您需要添加一个。如果您希望从文件中读取格式化的HTML代码而不破坏渲染,那么使用也是一个好主意

JOptionPane.showMessageDialog(
    null, 
    "Could not create table 't_rennwagen'.\n\n"
    + breakLongString( stmt.getWarnings().toString(), 100 ), 
    "SQL Error", 
    JOptionPane.ERROR_MESSAGE
);
这使您能够比Java字符串更好地管理
HTML
代码

不要忘记创建一个名为
document.html
的文件,其中包含以下内容:

import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

public class HTMLRenderingTest
{
    public static void main(String[] arguments) throws IOException
    {
        String html = FileUtils.readFileToString(new File("document.html"));
        HtmlCompressor compressor = new HtmlCompressor();
        html = compressor.compress(html);
        JOptionPane.showMessageDialog(null, html);
    }
}

标签宽度
许多Swing组件支持HTML3.2&;(简单)CSS。通过设置主体宽度,我们可以使组件找到
显示组件所需的自然高度。

此文本中的正文宽度设置为175像素。
结果:


只有将文本放入多行组件(如
JTextArea
)中时,
行分隔符(可能不是
\n
BTW)才会起作用。用于在选项窗格中显示
字符串的组件是
JLabel
。您的回答让我省去了一些麻烦。谢谢安德鲁。
JOptionPane.showMessageDialog(
    null, 
    "Could not create table 't_rennwagen'.\n\n"
    + breakLongString( stmt.getWarnings().toString(), 100 ), 
    "SQL Error", 
    JOptionPane.ERROR_MESSAGE
);
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

public class HTMLRenderingTest
{
    public static void main(String[] arguments) throws IOException
    {
        String html = FileUtils.readFileToString(new File("document.html"));
        HtmlCompressor compressor = new HtmlCompressor();
        html = compressor.compress(html);
        JOptionPane.showMessageDialog(null, html);
    }
}
<html>
<body width='175'><h1>Label Width</h1>

<p>Many Swing components support HTML 3.2 &amp; (simple) CSS. By setting a body width we can cause the component to find
    the natural height needed to display the component.<br><br>

<p>The body width in this text is set to 175 pixels.