gpt4 book ai didi

java - 输出中的换行符

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:20 24 4
gpt4 key购买 nike

我正在构建一个程序,它通过 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;
}

除了 translate 之外,我几乎所有的东西都正常工作了方法,这是下一步,事实上,我是否把 System.lineSeparator()"\n"作为 translate(System.lineSeparator()) 行中的参数,结果总是一个很大的空间,而我想要一个换行符。如何让换行符出现在输出中?

我会发布一张图片,但我没有足够的声誉。请注意,我刚刚尝试输入 ""<html><br/></html>""而不是 System.lineSeparator() , 鉴于 this question ,但这也没有用。我记得尝试过 MultiLineUI回答命令并获得 cannot find symbol或类似的东西。

最佳答案

    @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>");
}

关于java - 输出中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23836657/

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