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