[英]Using tables on a JTextArea
Am I not allowed to use an html table to print to a JTextArea? 我是否可以使用html表打印到JTextArea? When I use the following code it prints the html code and doesn't format the tables.
当我使用以下代码时,它将打印html代码,并且不格式化表格。 What do I do?
我该怎么办? This is probably a simple fix but since the question is so specific it would take me forever to google this.
这可能是一个简单的解决方法,但由于问题如此具体,将使我永远用谷歌搜索。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.*;
public class Index2 extends JPanel{
private JLabel searchLabel;
private JTextArea searchField;
private JButton resultButton;
private JTextArea resultField;
public Index2(){
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
searchLabel = new JLabel("Enter some text:");
searchField = new JTextArea("", 5, 20);
JScrollPane scrollSearch = new JScrollPane(searchField);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
resultButton = new JButton("Count Occurences of Each Letter");
resultField = new JTextArea("", 13, 15);
JScrollPane scrollResult = new JScrollPane(resultField);
resultField.setLineWrap(true);
resultField.setWrapStyleWord(true);
resultField.setOpaque(true);
resultField.setEditable(false);
c.insets = new Insets(10, 10, 10, 10);
c.gridx = 0;
c.gridy = 0;
add(searchLabel, c);
c.gridx = 0;
c.gridy = 1;
add(scrollSearch, c);
c.gridx = 0;
c.gridy = 2;
add(resultButton, c);
c.gridx = 0;
c.gridy = 3;
add(scrollResult, c);
ResultButtonHandler rbhandler = new ResultButtonHandler();
resultButton.addActionListener(rbhandler);
}
class ResultButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
String str = "<html><table>";
int count[] = new int[26];
char letter[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for (int i = 0; i < letter.length; i++ ){
int upper, lower= 0;
letter[i] = Character.toUpperCase(letter[i]);
upper = helpers.StringUtilities.numberCharacter(letter[i], searchField.getText());
letter[i] = Character.toLowerCase(letter[i]);
lower = helpers.StringUtilities.numberCharacter(letter[i], searchField.getText());
count[i] = upper + lower;
if ((i+1) % 2 == 0){
int h = i-1;
str += "<tr><td>" + letter[h] + ": " + count[h] +"</td><td>" + letter[i] + ": " + count[i] +"</td></tr>";
}
}
str += "</table>";
resultField.setText(str);
str = "";
}
}
}
I tried the JEditorPane.
我尝试了JEditorPane。 It prints the same
它打印相同
Then you're using it wrong... 那你就用错了...
Make sure you set the content type before you set the text... 确保设置内容类型,然后再设置文本...
resultField.setContentType("text/html");
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Index2 extends JPanel {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Index2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private JLabel searchLabel;
private JTextArea searchField;
private JButton resultButton;
private JEditorPane resultField;
public Index2() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.NORTH;
setBackground(Color.WHITE);
super.setLayout(gridbag);
searchLabel = new JLabel("Enter some text:");
searchField = new JTextArea("", 1, 20);
JScrollPane scrollSearch = new JScrollPane(searchField);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
searchField.setLineWrap(true);
searchField.setWrapStyleWord(true);
searchField.setOpaque(true);
resultButton = new JButton("Count Occurences of Each Letter");
resultField = new JEditorPane();
JScrollPane scrollResult = new JScrollPane(resultField);
resultField.setOpaque(true);
resultField.setEditable(false);
c.insets = new Insets(10, 10, 10, 10);
c.gridx = 0;
c.gridy = 0;
add(searchLabel, c);
c.gridx = 0;
c.gridy = 2;
add(resultButton, c);
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
add(scrollSearch, c);
c.gridx = 0;
c.gridy = 3;
add(scrollResult, c);
ResultButtonHandler rbhandler = new ResultButtonHandler();
resultButton.addActionListener(rbhandler);
}
class ResultButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = "<html><table border=1>";
int count[] = new int[26];
char letter[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
Random rnd = new Random();
for (int i = 0; i < letter.length; i++) {
if ((i + 1) % 2 == 0) {
int h = i - 1;
str += "<tr><td>" + letter[rnd.nextInt(letter.length)] + ": " + count[rnd.nextInt(letter.length)] + "</td><td>" + letter[rnd.nextInt(letter.length)] + ": " + count[rnd.nextInt(letter.length)] + "</td></tr>";
}
}
str += "</table>";
resultField.setContentType("text/html");
resultField.setText(str);
str = "";
}
}
}
You could also use a JLabel
which will format the HTML automatically, but you'll need to decide which is more suitable for your needs... 您还可以使用
JLabel
来自动格式化HTML,但是您需要确定哪种更适合您的需求...
Have a look at How to Use Editor Panes and Text Panes for more details 看看如何使用编辑器窗格和文本窗格了解更多详细信息
I just went with this 我就是这样
if ((i+1) % 2 == 0){
int h = i-1;
str += letter[h] + ": " + count[h] + "\t" + letter[i] + ": " + count[i] + "\n";
}
It eliminated the need for tables and I accomplished what I intended to do 它消除了对表格的需求,并且完成了我打算做的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.