我们可以在文本区域放置组合框,单选按钮,文本字段,表格吗? [英] Can we put combobox, radio button, text field, table on text area?

查看:103
本文介绍了我们可以在文本区域放置组合框,单选按钮,文本字段,表格吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这种功能。就像在MS Word中一样,我们在菜单栏中选择表格然后我们在表格中绘制。

I need this type of functionality. Like in MS Word, we choose table in menu bar and then we draw in our sheet.

那么如何在Java中完成?我想表单我可以使用 JTextArea

So how it can be done in Java? I thought for sheet I can use JTextArea.

推荐答案

否(到 JTextArea ),但你应该能够要使用 JTextPane 执行此操作,请参阅 JTextPane#insertComponent 了解更多详情

No (to a JTextArea), but you should be able to do it using a JTextPane, see JTextPane#insertComponent for more details

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

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());
            JTextPane tp = new JTextPane();

            Document doc = tp.getDocument();

            try {
                tp.insertComponent(new JTextField("Hello world"));
                doc.insertString(doc.getLength(), "\n", null);
                tp.insertComponent(new JComboBox(new String[]{"Banana", "Apple", "Grape"}));
                doc.insertString(doc.getLength(), "\n", null);
                tp.insertComponent(new JRadioButton("Option A"));
                doc.insertString(doc.getLength(), "\n", null);
                tp.insertComponent(new JTable(new DefaultTableModel(10, 5)));
                doc.insertString(doc.getLength(), "\n", null);
            } catch (BadLocationException exp) {
                exp.printStackTrace();
            }

            add(new JScrollPane(tp));
        }

    }

}

这篇关于我们可以在文本区域放置组合框,单选按钮,文本字段,表格吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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