-1

奇怪的事情发生在我身上。由于某种原因,它需要很长时间(1.60900 秒)才能运行以下行:

textArea = new JTextArea();

我将 textArea 变量声明为全局变量。这只发生在一个窗口(Jframe)中。在其他情况下,它不会发生。

public class FAQ extends JFrame 
{
    /*--------attributes--------*/
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JPanel panel;
    private JScrollPane scrollPaneInput;
    private JScrollPane scrollPaneQuestions;
    private JPanel paneQuestions;
    private JPanel paneSelectOrNewFAQ;
    private JButton btnEditSelection;
    private JButton btnNewFAQ;
    public JTextArea textArea;
    private JLabel lblQuestions;
    public JList list;
    private User user;
    private FAQ currentWindow;
    private int selectedFaq = 0;
    private DatabaseManager DManager;
    private Vector<FAQ_class> Faqs = new Vector<FAQ_class>();
    private JButton btnNewButton;


    /*--------methods--------*/

    public FAQ(User _user,DatabaseManager DM) 
    {
        setResizable(false);
        DManager = DM;
        addWindowStateListener(new WindowStateListener() {
            public void windowStateChanged(WindowEvent arg0) {
            }
        });
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent arg0) {
                Menu menu = new Menu(user,DManager);
                menu.setVisible(true);
            }
        });
        currentWindow = this;
        user = _user;
        addGui();
        if(!user.rule.equals("patient"))
        {
            btnEditSelection.setEnabled(true);
            btnNewFAQ.setEnabled(true);
        }
        loadFaqs();
    }//end of FAQ
    public void loadFaqs()
    {
        Faqs = DManager.getQuestionsList();
        Vector<String> temp = new Vector<String>();
        for(int i = 0 ; i < Faqs.size();i++)
        {
            temp.addElement(Faqs.get(i).question);
        }
        list.setListData(temp);

    }
    public void addGui()
    {
        setTitle("FAQ - Online medical help");
        setIconImage(Toolkit.getDefaultToolkit().getImage(FAQ.class.getResource("/Images/question.png")));
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 708, 438);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(1, 0, 0, 0));

        addPanel();
        addPanes();
        addButtons();
        addGroupLayout();
        addJTextArea();
    }//end of addGui

    public void addPanel()
    {
        panel = new JPanel();
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.add(panel);
    }//end of addPanel

    public void addPanes()
    {
        scrollPaneInput = new JScrollPane();
        scrollPaneInput.setBounds(327, 0, 365, 398);

        paneQuestions = new JPanel();
        paneQuestions.setBounds(0, 0, 317, 38);
        paneQuestions.setBackground(new Color(154, 205, 50));
    }//end of addScrollPanes

    public void addButtons()
    {

    }//end of addButtons

    public void addJTextArea()
    {
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font("Courier New", Font.PLAIN, 14));
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setAlignmentX(Component.RIGHT_ALIGNMENT);
        scrollPaneInput.setViewportView(textArea);
    }//end of addJTextArea

    public void addGroupLayout()
    {

        lblQuestions = new JLabel("Questions");
        lblQuestions.setHorizontalAlignment(SwingConstants.CENTER);
        lblQuestions.setBounds(0, 0, 317, 38);
        lblQuestions.setForeground(new Color(255, 255, 255));
        lblQuestions.setFont(new Font("Tahoma", Font.BOLD, 22));
        panel.setLayout(null);
        scrollPaneQuestions = new JScrollPane();
        scrollPaneQuestions.setBounds(0, 37, 317, 318);

                list = new JList();
                list.setSelectionBackground(new Color(154, 205, 50));
                list.addListSelectionListener(new ListSelectionListener() {
                    public void valueChanged(ListSelectionEvent arg0) {
                        try
                        {

                            for(int i = 0; i<Faqs.size();i++)
                            {
                                if(!list.isSelectionEmpty())
                                    if(Faqs.get(i).question.equals(list.getSelectedValue().toString()))
                                    {
                                        textArea.setText(Faqs.get(i).answer);
                                        selectedFaq = i;
                                        break;
                                    }
                            }

                        }
                        catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
                scrollPaneQuestions.setViewportView(list);
                panel.add(scrollPaneQuestions);
        panel.add(paneQuestions);
        paneQuestions.setLayout(null);
        paneQuestions.add(lblQuestions);
        panel.add(scrollPaneInput);

        paneSelectOrNewFAQ = new JPanel();
        paneSelectOrNewFAQ.setBounds(0, 348, 317, 50);
        btnEditSelection = new JButton("Edit Selected");
        btnEditSelection.setBounds(68, 11, 131, 40);
        btnEditSelection.setEnabled(false);
        btnEditSelection.addActionListener(new ActionListener() {


            //open EditFAQ to edit FAQ
            public void actionPerformed(ActionEvent e) {
                if(!list.isSelectionEmpty())
                {
                    EditFAQ faq = new EditFAQ(user,Faqs.get(selectedFaq),currentWindow,DManager);
                    faq.setVisible(true);
                    currentWindow.setEnabled(false);
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"You must select for the list first.");
                }
            }
        });
        btnEditSelection.setIcon(new ImageIcon(FAQ.class.getResource("/Images/tool.png")));

        btnNewFAQ = new JButton("New FAQ");
        btnNewFAQ.setBounds(203, 11, 114, 40);
        btnNewFAQ.setEnabled(false);
        btnNewFAQ.addActionListener(new ActionListener() {

            //open EditFAQ to make new FAQ
            public void actionPerformed(ActionEvent e) {
                EditFAQ faq = new EditFAQ(user,null,currentWindow,DManager);
                faq.setVisible(true);
                currentWindow.setEnabled(false);
            }


        });
        btnNewFAQ.setMinimumSize(new Dimension(95, 23));
        btnNewFAQ.setMaximumSize(new Dimension(95, 23));
        btnNewFAQ.setPreferredSize(new Dimension(95, 23));
        btnNewFAQ.setIcon(new ImageIcon(FAQ.class.getResource("/Images/add.png")));

        btnNewButton = new JButton("");
        btnNewButton.setBounds(0, 10, 42, 41);
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        btnNewButton.setIcon(new ImageIcon(FAQ.class.getResource("/Images/left.png")));
        panel.add(paneSelectOrNewFAQ);
        paneSelectOrNewFAQ.setLayout(null);
        paneSelectOrNewFAQ.add(btnNewButton);
        paneSelectOrNewFAQ.add(btnEditSelection);
        paneSelectOrNewFAQ.add(btnNewFAQ);
    }//end of addGroupLayout

}//end of class
4

1 回答 1

1

奇怪的事情发生在我身上。出于某种原因,它需要很长时间(5 秒~)才能运行以下行:
运行这个类并给我结果:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JTextArea;
public class JTextAreaRunningTime {
    JTextArea textArea;
    public JTextAreaRunningTime(){
    long startTime = System.currentTimeMillis();
    textArea = new JTextArea();
    long endTime = System.currentTimeMillis();

NumberFormat nf = new DecimalFormat("#0.00000");
String totalTime = nf.format((endTime-startTime)/1000d);
System.out.println("Execution time is " + totalTime + " seconds");
    }
    public static void main (String...argW){
        new JTextAreaRunningTime();
    }

}
于 2013-05-22T16:32:02.370 回答