- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在构建一个程序,它通过 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/
我有一个带有 的表格我想在服务器端捕获该文本区域中的任何换行符,并将它们替换为 . 这可能吗? 我尝试设置 white-space:pre在 textarea 上的 CSS,但仍然不够。 最佳答案
我正在通过 FileStream 更改文件(这是一个非常大的文件,我只需要更改标题而不重写整个文件。 该文件可以有 Unix 或 Windows 换行符,知道哪一个对我来说很重要,这样我可以在更新文件
如何以编程方式(不使用 vi)将 DOS/Windows 换行符转换为 Unix 换行符? dos2unix 和 unix2dos 命令在某些系统上不可用。 如何使用 sed、awk 和 tr 等命令
我编辑了一个 BibteX 文件,到目前为止,它显示了我需要的所有信息。 FUNCTION {electronic} { output.bibitem format.btitle "title"
您的软件是否处理来自其他系统的换行符? Linux/BSD linefeed ^J 10 x0A Windows/IBM return linefeed ^M^
我发现 Windows 命令行重定向会自动将 '\n' 替换为 '\r\n'。有什么方法可以避免这种情况?因为在 stdout 或 stderr 重定向之后,如果您将 '\r\n' 写入控制台,您将得
来自 this question ,这个…… lines = foo.value.split(/\r\n|\r|\n/); 是拆分字符串的一种方法,但如何用换行符将其连接回去? 另外,我想知道如果我说
我正在尝试获取新行,但如果我使用 \n 它不起作用。 任何通过向字符串添加一些东西来换行的方法,例如 \r\n (这也不起作用) gfx.DrawString("Project No \n" + te
我有一串数据,中间有换行符。例如: "Product Name \n Product Color \n Product Quantity \n Product Location \n Product
我正在尝试让 scmCommentPrefix 按照 http://maven.apache.org/plugins/maven-release-plugin/faq.html#scmCommentP
如何检查正则表达式 /^\n/在字符串中 blahblahblah 我似乎无法从 php 的 preg_match 获得返回值 1 . 编辑: 由于某种原因,我的坏处是 CR 本身就是我的换行符。
我很难在与文本字符串相同的行上输出变量。当我使用Write-Host而不是Write-Output时,它可以工作。我想使用Write-Output,因为这似乎是最佳做法(将内容保留在管道中),但是Wr
我正在Powershell中工作,以为here字符串中的特定单词着色。除包含回车/换行符的单词外,其他功能均有效。没有这些字符,如何计算单词的长度? 以下是我正在使用的功能和测试数据。我希望第二行上的
我有一个在Powershell中运行的脚本,并且我希望能够在脚本名称和脚本内容本身之间的结果文本文件输出中添加一行。 当前,从下面开始,行$str_msg = $file,[System.IO.Fil
有人知道smalltalk中字符串的换行符是什么吗? 我试图将字符串拆分为单独的行,但我无法弄清楚smalltalk 中的换行符是什么。 即。 string := 'smalltalk is
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
这个问题已经有答案了: how to use dotall flag for regex.exec() (4 个回答) 已关闭 7 年前。 字符串内容
我想用 CSS 换行。我正在使用内容。 td:before { content: "Test\A Test2"; } 它不工作。如何正确
考虑一个具有 UNIX 行结尾的 C++ 文件(即 '\x0a' 而不是 "\x0d\x0a")并包含以下原始字符串文字: const char foo[] = R"(hello^M )"; (其中
我有以下 printf 语句: printf ("%s (%s)\n",$row["word"], $row["definition"]); 我正在尝试解决换行符而不是: word defin
我是一名优秀的程序员,十分优秀!