Java 输出中的换行符

Java 输出中的换行符,java,swing,output,Java,Swing,Output,我正在构建一个程序,该程序通过GUI接收带有换行符的输入字符串,将其拆分为行,然后拆分为单词,然后逐字将其传递给我仍然需要实现的方法,translate,该方法将以某种方式获取一个单词,并将其与适当的单词交换。目前,使用以下代码: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Tem

我正在构建一个程序,该程序通过GUI接收带有换行符的输入字符串,将其拆分为行,然后拆分为单词,然后逐字将其传递给我仍然需要实现的方法,
translate
,该方法将以某种方式获取一个单词,并将其与适当的单词交换。目前,使用以下代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author michelegorini
 */
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
public class HakkaTranslator extends JFrame {

    /**
     * Creates new form HakkaTranslator
     */
    public HakkaTranslator() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputField = new JTextArea();
        translateButton = new JButton();
        pinyinField = new JLabel();
        hakkaCharField = new JLabel();
        mandCharField = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        translateButton.setText("Translate");
        translateButton.addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {

                inpText = inputField.getText();
                String[] lines = inpText.split(System.lineSeparator());
                String[][] words = new String[lines.length][20];
                for(int i=0;i<lines.length;i++){
                    words[i] = lines[i].split(" ");
                }
                for(int i=0;i<lines.length;i++){
                    for(int j = 0;j<words[i].length;j++){
                        translate(words[i][j]);
                    }
                    translate(System.lineSeparator());
                }
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(hakkaCharField, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(mandCharField, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addComponent(translateButton, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(inputField, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pinyinField, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(SwingConstants.HORIZONTAL, new java.awt.Component[] {hakkaCharField, inputField, mandCharField, pinyinField});

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(translateButton)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(pinyinField, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(inputField, GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(hakkaCharField, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
                    .addComponent(mandCharField))
                .addContainerGap(177, Short.MAX_VALUE))
        );

        layout.linkSize(SwingConstants.VERTICAL, new java.awt.Component[] {hakkaCharField, inputField, mandCharField, pinyinField});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void translate(String word) {
        pinyinField.setText(pinyinField.getText() + word + " ");
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HakkaTranslator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HakkaTranslator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HakkaTranslator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HakkaTranslator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HakkaTranslator().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JLabel hakkaCharField;
    private JTextArea inputField;
    private JLabel mandCharField;
    private JLabel pinyinField;
    private JButton translateButton;
    // End of variables declaration//GEN-END:variables
    String inpText;
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
/**
*
*@作者michelegorini
*/
导入java.util.Scanner;
导入javax.swing.JFrame;
导入javax.swing.JTextArea;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.WindowConstants;
导入javax.swing.GroupLayout;
导入javax.swing.LayoutStyle;
导入javax.swing.SwingConstants;
公共类HakkaTranslator扩展了JFrame{
/**
*创建新表单HakkaTranslator
*/
公共HakkaTranslator(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
////GEN-BEGIN:初始化组件
私有组件(){
inputField=新的JTextArea();
translateButton=新JButton();
pinyinField=newjlabel();
hakkaCharField=new JLabel();
mandCharField=newjlabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
translateButton.setText(“Translate”);
translateButton.addActionListener(新java.awt.event.ActionListener(){
@凌驾
public void actionPerformed(java.awt.event.ActionEvent e){
InputText=inputField.getText();
String[]lines=inpText.split(System.lineSeparator());
String[][]words=新字符串[lines.length][20];
对于(int i=0;i
@覆盖
public void actionPerformed(java.awt.event.ActionEvent e){
InputText=inputField.getText();
字符串[]行=inpText.split(“\n”);
String[][]words=新字符串[lines.length][20];

对于(int i=0;i您可以尝试在您的内容周围使用
,然后html渲染器将其视为预格式化。您也可以尝试只添加

\n
而不添加输出行分隔符的html标记。我相信Swing会接收文本,但希望它是html。

发布图像,我们将对其进行编辑,使其显示出来。只需提供li即可图像的nk您能简单地解释一下您想要做什么吗?也许有一种不同的解决方案,而不是将JTextArea中的文本添加到JLabel中?我的想法是随后实现一种方法,用客家表意文字、普通话翻译表意文字和相应的拼音替换这些文字,这三个标签都存在。我将研究SQL,以避免编写无限序列的
if
s和
elses
。下面的步骤,尽管凭借我的能力,似乎非常雄心勃勃,可能太雄心勃勃,但它是开发一个类似Google Translate的界面,当鼠标经过标签上的单个单词时,它会突出显示标签上的单个单词,并显示其他单词的列表当你点击单词时,我会翻译。但是我可能会满足于添加第五个标签来打印具有多个翻译的单词的翻译列表。
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e) {

        inpText = inputField.getText();
        String[] lines = inpText.split("\n");
        String[][] words = new String[lines.length][20];
        for(int i=0;i<lines.length;i++){
            words[i] = lines[i].split(" ");
        }
        translate("<html>");
        for(int i=0;i<lines.length;i++){
            for(int j = 0;j<words[i].length;j++){
                translate(words[i][j] );
            }
            translate("<br/>");
        }
        translate("</html>");
    }