Java 我们可以把组合框、单选按钮、文本字段、表格放在文本区吗?

Java 我们可以把组合框、单选按钮、文本字段、表格放在文本区吗?,java,swing,jtextarea,Java,Swing,Jtextarea,我需要这种类型的功能。和微软Word一样,我们在菜单栏中选择表格,然后在表格中画图 那么如何在Java中实现呢?我想我可以使用JTextArea来制作工作表 否(对于JTextArea),但您应该能够使用JTextPane执行此操作,有关更多详细信息,请参阅 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JComboBox; import javax.swing.JFrame; impor

我需要这种类型的功能。和微软Word一样,我们在菜单栏中选择表格,然后在表格中画图

那么如何在Java中实现呢?我想我可以使用
JTextArea
来制作工作表

否(对于
JTextArea
),但您应该能够使用
JTextPane
执行此操作,有关更多详细信息,请参阅

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));
        }

    }

}

不可以(对于
JTextArea
),但您应该能够使用
JTextPane
执行此操作,有关更多详细信息,请参阅

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));
        }

    }

}