简体   繁体   English

如何在Java中将表,组合框,单选按钮,文本字段和文本区域从工具栏拖放到文本窗格?

[英]how to drag and drop table, combobox , radio button , textfield and textarea from toolbar to textpane in java?

我想要当我单击工具栏上的按钮(如表格按钮)时,然后从此处拖动并放入TextPane.Combobox,Textfield等类似按钮在工具栏上给出

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JButton;
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.JToolBar;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
public class after_create_resume_button 
{
     JButton cut,copy,paste,table;
    public static void main(String[] args) 
    {
        new after_create_resume_button();
    }
    public after_create_resume_button() 
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                try 
                {
                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }
                catch(Exception ex) 
                {
                    ex.printStackTrace();
                }
                JFrame frame = new JFrame("Resume Maker");
                frame.setLayout(null);
                JToolBar jt=new JToolBar(); //in Toolbar cut,copy,paste,table,TextField
                cut=new JButton(new ImageIcon("D://final_project//Space1//img//cut.png"));
                copy=new JButton(new ImageIcon("D://final_project//Space1//img//copy.png"));
                paste=new JButton(new ImageIcon("D://final_project//Space1//img//paste.png"));
                table=new JButton(new ImageIcon("D://final_project//Space1//img//table.png"));
                JTextField jtf= new JTextField("Text field");
               // jtf.setBounds(0,0,50,50);
                cut.setToolTipText("Cut");
                copy.setToolTipText("Copy");
                paste.setToolTipText("Paste");
                table.setToolTipText("Table");
                jtf.setToolTipText("TextField");
                jtf.setEditable(false);
                jt.add(cut);
                jt.add(copy);jt.addSeparator();
                jt.add(paste);jt.addSeparator();
                jt.add(table);
                jt.addSeparator();
                jt.add(jtf);
               jt.setBounds(0,0,600,50);
               jt.setFloatable(false);
               jt.setRollover(true);

                frame.add(jt);

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(new after_create_resume_buttonPane());

                frame.setSize(900, 900);

                frame.setResizable(false);

                frame.setVisible(true);
            }
        });
    }
    public class after_create_resume_buttonPane extends JPanel 
    {
        private static final long serialVersionUID = 1L;
        public after_create_resume_buttonPane() 
        {
            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();
            }
            setLocation(200,50);

            setSize(595, 842);

           // add(new JScrollPane(tp));

            add(tp);

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM