我怎么能在没有\ t的情况下在JTextArea中均匀地分隔出2d数组? [英] How can I evenly space out 2d array in JTextArea without \t?

查看:218
本文介绍了我怎么能在没有\ t的情况下在JTextArea中均匀地分隔出2d数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编码的时候遇到了一个小问题。我不是java的专家,我只是刚刚开始学习GUI,请耐心等待。



我正在创建一个包含地图的游戏那是由这个二维数组组成的,我创建了一个方法把这个二维数组放到一个textArea中。

  public void viewMap (int c = 0; c< matrix [] [] matrix)
{
for(int r = 0; r {
textArea.setText(textArea.getText()+\t+ String.valueOf(matrix [r] [c]));
}
textArea.setText(textArea.getText()+\\\
);




如果你注意到\ t用来隔开每个元素。这适用于均匀分布,但空间非常大。我的数组结果如下:

  x H ffxx 
xfxf D x
xxxxxx

这种间距很大,因为不同的字母不能抵消其他字母的位置,我的目的,但如果我只是把几个空格代替\ t,我得到的东西不像我使用/ t时那样排队,而是不同大小的字母将其余的该行。

所以我想知道是否有人可以帮我想出一个解决方案,以便在打印出来时均匀地分隔这些字母。我非常感谢任何反馈和帮助。



感谢。

解决方案

文本组件不是很好的设计用于以这种方式格式化数据(至少不是用于显示纯文本的组件)。

您可以使用<$例如,c $ c> JTable ...


$ p $ import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
$ b $ public class Test {

public static void main(String [] args){
new Test();

$ b $ public Test(){
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 TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});

$ b $ public class TestPane extends JPanel {
$ b $ public TestPane(){
setLayout(new BorderLayout());
String [] [] matrix = new String [] [] {
{x,H,f,f,x,x},
{x,f,x,f,D,x},
{x,H,f,f x,x},
};

DefaultTableModel model = new DefaultTableModel(0,6);
for(String [] row:matrix){
model.addRow(row);

$ b $ JTable table = new JTable(model){
@Override
protected void configureEnclosingScrollPane(){
}
};
table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0,0));

add(new JScrollPane(table));




$ b code $ pre

如果你不需要编辑值或者需要任何真正想要的东西(比如选择),你可以使用 JLabel HTML表格...





  setLayout(new GridBagLayout()); 
String [] [] matrix = new String [] [] {
{x,H,f,f,x,x},
{x,f,x,f,D,x},
{x,H,f,f x,x},
};

JLabel label = new JLabel();
StringBuilder sb = new StringBuilder(< html>);
sb.append(< table boder = 0>);
for(String [] row:matrix){
sb.append(< tr>);
for(String col:row){
sb.append(< td align = center>)。append(col).append(< / td>);
}
sb.append(< / tr>);
}
sb.append(< / table>);
label.setText(sb.toString());
add(label);

如果你真的想要使用文本组件,那么像 JEditorPane 可能是更好的选择,因为您不必担心单间隔字体...

$ b

  setLayout(new BorderLayout()); 
String [] [] matrix = new String [] [] {
{x,H,f,f,x,x},
{x,f,x,f,D,x},
{x,H,f,f x,x},};

JLabel label = new JLabel();
StringBuilder sb = new StringBuilder(< html>< body>);
sb.append(< table boder = 0>);
for(String [] row:matrix){
sb.append(< tr>);
for(String col:row){
sb.append(< td align = center>)。append(col).append(< / td>);
}
sb.append(< / tr>);
}
sb.append(< / table>< / body>< / html>);
JEditorPane ep = new JEditorPane();
ep.setContentType(text / html);
ep.setText(sb.toString());
add(new JScrollPane(ep));


I ran into a little problem today while I was coding. I'm no expert in java, and I'm only just starting to learn GUI, so bear with me please.

I'm creating a game that involves a map that is composed of this 2D array, I've created a method to put that 2D array in a textArea.

public void viewMap(String[][] matrix)
{
    for (int r = 0; r < matrix.length; r++) {
        for (int c = 0; c < matrix[0].length; c++)
        {
            textArea.setText(textArea.getText() + "\t" + String.valueOf(matrix[r][c]));
        }
        textArea.setText(textArea.getText() + "\n" );
    }
}

If you notice the "\t" that i use to space each element. This works well for evenly spacing it, but the space is extremely large. My array turns out something like this:

x              H              f              f              x              x
x              f              x              f              D              x
x              x              x              x              x              x

This kind of spacing is great because the different letters not offset the position of the other letters, but it is way to large for my purposes, but if i simply put a few spaces in place of the "\t" i get something that doesn't line up like it does when i use /t, instead that different sizes of the letters throws off the rest of the row.

So i was wondering if anyone could help me come up with a solution in order to evenly space these letters when printing them out. I would really appreciate any feedback and help.

Thanks.

解决方案

Text components aren't really well designed for formatting data in this way (at least not components that are designed to display plain text).

You could use a JTable instead, for example...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            String[][] matrix = new String[][]{
                {"x", "H", "f", "f", "x", "x"},
                {"x", "f", "x", "f", "D", "x"},
                {"x", "H", "f", "f", "x", "x"},
            };

            DefaultTableModel model = new DefaultTableModel(0, 6);
            for (String[] row : matrix) {
                model.addRow(row);
            }

            JTable table = new JTable(model) {
                @Override
                protected void configureEnclosingScrollPane() {
                }
            };
            table.setShowGrid(false);
            table.setIntercellSpacing(new Dimension(0, 0));

            add(new JScrollPane(table));

        }

    }

}

If you don't need to edit the values or need anything really fancy (like selection), you could use a JLabel and wrap your text within a HTML TABLE...

setLayout(new GridBagLayout());
String[][] matrix = new String[][]{
    {"x", "H", "f", "f", "x", "x"},
    {"x", "f", "x", "f", "D", "x"},
    {"x", "H", "f", "f", "x", "x"},
};

JLabel label = new JLabel();
StringBuilder sb = new StringBuilder("<html>");
sb.append("<table boder=0>");
for (String[] row : matrix) {
    sb.append("<tr>");
    for (String col : row) {
        sb.append("<td align=center>").append(col).append("</td>");
    }
    sb.append("</tr>");
}
sb.append("</table>");
label.setText(sb.toString());
add(label);

And... If you "really" want to use a text component, then something like JEditorPane would probably be a better choice, as you don't need to worry about mono-spaced fonts...

setLayout(new BorderLayout());
String[][] matrix = new String[][]{
    {"x", "H", "f", "f", "x", "x"},
    {"x", "f", "x", "f", "D", "x"},
    {"x", "H", "f", "f", "x", "x"},};

JLabel label = new JLabel();
StringBuilder sb = new StringBuilder("<html><body>");
sb.append("<table boder=0>");
for (String[] row : matrix) {
    sb.append("<tr>");
    for (String col : row) {
        sb.append("<td align=center>").append(col).append("</td>");
    }
    sb.append("</tr>");
}
sb.append("</table></body></html>");
JEditorPane ep = new JEditorPane();
ep.setContentType("text/html");
ep.setText(sb.toString());
add(new JScrollPane(ep));

这篇关于我怎么能在没有\ t的情况下在JTextArea中均匀地分隔出2d数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆