【问题标题】:Can we put combobox, radio button, text field, table on text area?我们可以将组合框、单选按钮、文本字段、表格放在文本区域吗?
【发布时间】:2015-02-06 04:16:25
【问题描述】:

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

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

【问题讨论】:

    标签: java swing jtextarea


    【解决方案1】:

    否(发给JTextArea),但您应该可以使用JTextPane 进行操作,请参阅JTextPane#insertComponent 了解更多详情

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

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 2021-06-20
      • 2020-07-21
      • 2020-12-12
      • 2018-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2016-12-23
      相关资源
      最近更新 更多