格式Jlabel - JTextBox

时间:2017-05-01 22:37:08

标签: java swing format binary-tree jlabel

我有一个简单的二叉树打印机:

     │           ┌── K
     │       ┌── F
     │       │   │   ┌── L
     │       │   └── J
     │   ┌── C
     │   │   │   ┌── I
     │   │   └── E
     └── A
         │       ┌── H
         │   ┌── D
         │   │   └── G
         └── B

如果我在eclipse控制台中运行它,我会得到:

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            final ComboView comboView;
            if(view == null){
                comboView = new ComboView();
                LayoutInflater layoutInflater = InputCombos.this.getLayoutInflater();
                view = layoutInflater.inflate(R.layout.input_combos_view, null);
                comboView.combo = (TextView) view.findViewById(R.id.combo);
                comboView.numOrbs = (TextView) view.findViewById(R.id.numOrbs);
                comboView.comboEdit = (EditText) view.findViewById(R.id.comboEdit);
                comboView.comboElement = (Spinner) view.findViewById(R.id.comboElement);

                ArrayAdapter<String> comboElements = new ArrayAdapter<>(InputCombos.this, android.R.layout.simple_spinner_item, elements);
                comboElements.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                comboView.comboElement.setAdapter(comboElements);

                comboView.pos = i;

                comboView.comboEdit.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        if(charSequence.length() != 0){
                            int orbCount = Integer.parseInt((charSequence).toString());
                            if(orbCount >= 6){
                                LinearLayout comboViewLayout = (LinearLayout) findViewById(R.id.comboViewLayout);
                                comboView.row = new CheckBox(InputCombos.this);
                                //CheckBox row = new CheckBox(InputCombos.this);
                                comboView.row.setText("Row");
                                comboViewLayout.addView(comboView.row);
                            }
                            comboData.set(comboView.pos,orbCount);
                            System.out.println(comboData);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });

                view.setTag(comboView);
            } else {
                comboView = (ComboView) view.getTag();
            }

            System.out.println(combosNum);
            comboView.combo.setText(combosNum.get(i));

            return view;
        }

我的问题是我试图在UI上显示它,所以当我把它放在JLabel上它不起作用时,我尝试用&lt;格式化它。 html&gt;而不是\ n - &gt; &LT; br&gt;但它也不起作用,最好的方法是什么?我尝试过使用JFormattedTextField,但它似乎不起作用。

谢谢。

1 个答案:

答案 0 :(得分:2)

您可以通过多种方式实现这一目标。您可以创建一个可以绘制结构的自定义组件;您可以使用JTree,也可以使用类似JTextArea的内容。

诀窍是确保您使用固定宽度字体

JTextArea representation

String tree = "│           ┌── K\n"
        + "│       ┌── F\n"
        + "│       │   │   ┌── L\n"
        + "│       │   └── J\n"
        + "│   ┌── C\n"
        + "│   │   │   ┌── I\n"
        + "│   │   └── E\n"
        + "└── A\n"
        + "    │       ┌── H\n"
        + "    │   ┌── D\n"
        + "    │   │   └── G\n"
        + "    └── B";
JTextArea ta = new JTextArea(15, 25);
ta.setText(tree);
ta.setFont(new Font("Monospaced", Font.PLAIN, 13));
JFrame frame = new JFrame();
frame.add(new JScrollPane(ta));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);