Java GUI在两秒钟后出现问题



我很难使用Eclipse WindowBuilder GUI。我已经做了一个布局,它确实有效,但2-3秒后开始滞后,速度非常慢。我在谷歌上搜索过,找到了revalidate()validate()update()和类似的东西,但它做得很一样。你能告诉我它在哪里变得滞后吗,所以我不知道是什么原因导致的,我现在很困惑。

源代码:

package forum;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ForumView {
private JFrame frame;
private JTextField txtUsr;
private JTextField txtPasswd;
private JSeparator separator;
private JTextField txtUsr_1;
private JTextField txtPasswd_1;
private JButton btnNewButton_1;
private JSeparator separator_1;
private JButton btnNewButton;
private JButton btnNewButton_2;
private JButton btnNewButton_3;
private JButton btnAddPicture;
private JButton btnNewButton_4;
private JButton btnBack;
private JButton btnSortAlphabetically;
private JButton btnNewButton_5;
private JLabel lblNewLabel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ForumView window = new ForumView();
                window.frame.setVisible(true);
                window.frame.update(window.frame.getGraphics());
                window.frame.revalidate();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
/**
 * Create the application.
 */
public ForumView() {
    initialize();
}
public void redrawMethod() {
    frame.repaint();
    txtUsr.repaint();
    txtPasswd.repaint();
    btnNewButton.repaint();
    separator_1.repaint();
    separator.repaint();
    txtUsr_1.repaint();
    txtPasswd_1.repaint();
    btnNewButton_1.repaint();
    separator_1.repaint();
    btnNewButton.repaint();
    btnNewButton_2.repaint();
    btnNewButton_3.repaint();
    btnAddPicture.repaint();
    btnNewButton_4.repaint();
    btnBack.repaint();
    btnSortAlphabetically.repaint();
    btnNewButton_5.repaint();
    lblNewLabel.repaint();
}
/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 1318, 814);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    txtUsr = new JTextField();
    txtUsr.setBounds(10, 11, 86, 28);
    frame.getContentPane().add(txtUsr);
    txtUsr.setColumns(10);
    txtPasswd = new JTextField();
    txtPasswd.setBounds(10, 42, 86, 28);
    frame.getContentPane().add(txtPasswd);
    txtPasswd.setColumns(10);
    btnNewButton = new JButton("Login");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(txtUsr.getText());
        }
    });
    btnNewButton.setBounds(106, 10, 89, 23);
    frame.getContentPane().add(btnNewButton);
    separator = new JSeparator();
    separator.setOrientation(SwingConstants.VERTICAL);
    separator.setBounds(205, 11, 2, 51);
    frame.getContentPane().add(separator);
    txtUsr_1 = new JTextField();
    txtUsr_1.setText("usr");
    txtUsr_1.setBounds(217, 11, 86, 28);
    frame.getContentPane().add(txtUsr_1);
    txtUsr_1.setColumns(10);
    txtPasswd_1 = new JTextField();
    txtPasswd_1.setText("passwd");
    txtPasswd_1.setBounds(217, 42, 86, 28);
    frame.getContentPane().add(txtPasswd_1);
    txtPasswd_1.setColumns(10);
    btnNewButton_1 = new JButton("register");
    btnNewButton_1.setBounds(313, 10, 89, 23);
    frame.getContentPane().add(btnNewButton_1);
    separator_1 = new JSeparator();
    separator_1.setBounds(10, 73, 1282, 2);
    frame.getContentPane().add(separator_1);
    btnNewButton_2 = new JButton("Add topic");
    btnNewButton_2.setEnabled(false);
    btnNewButton_2.setBounds(10, 120, 100, 23);
    frame.getContentPane().add(btnNewButton_2);
    btnNewButton_3 = new JButton("Passwd change");
    btnNewButton_3.setEnabled(false);
    btnNewButton_3.setBounds(10, 154, 100, 23);
    frame.getContentPane().add(btnNewButton_3);
    btnAddPicture = new JButton("Add picture");
    btnAddPicture.setEnabled(false);
    btnAddPicture.setBounds(10, 186, 98, 26);
    frame.getContentPane().add(btnAddPicture);
    btnNewButton_4 = new JButton("Logout");
    btnNewButton_4.setBounds(106, 43, 89, 26);
    frame.getContentPane().add(btnNewButton_4);
    btnBack = new JButton("Back");
    btnBack.setEnabled(false);
    btnBack.setBounds(10, 223, 98, 26);
    frame.getContentPane().add(btnBack);
    btnSortAlphabetically = new JButton("Sort Alphabetically");
    btnSortAlphabetically.setEnabled(false);
    btnSortAlphabetically.setBounds(10, 257, 98, 26);
    frame.getContentPane().add(btnSortAlphabetically);
    btnNewButton_5 = new JButton("Add Comment");
    btnNewButton_5.setEnabled(false);
    btnNewButton_5.setBounds(10, 294, 98, 26);
    frame.getContentPane().add(btnNewButton_5);
    lblNewLabel = new JLabel("No Photo");
    lblNewLabel.setBounds(420, 11, 55, 51);
    frame.getContentPane().add(lblNewLabel);
    frame.validate();
    txtUsr.validate();
    txtPasswd.validate();
    separator.validate();
    txtUsr_1.validate();
    txtPasswd_1.validate();
    btnNewButton_1.validate();
    separator_1.validate();
    btnNewButton.validate();
    btnNewButton_2.validate();
    btnNewButton_3.validate();
    btnAddPicture.validate();
    btnNewButton_4.validate();
    btnBack.validate();
    btnSortAlphabetically.validate();
    btnNewButton_5.validate();
    lblNewLabel.validate();
    }
}

谢谢。

编辑:现在"主"GUI可以工作了,但其他一切都冻结了。我使用了一个openfiledialog来打开一个图像文件,但这个对话框也冻结了。这是我尝试过的代码:

btnAddPicture.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            frame.createBufferStrategy(2);
            frame.validate();
            frame.repaint();
            final JFileChooser fc = new JFileChooser();
            //RepaintManager.currentManager(fc).setDoubleBufferingEnabled(true);
             System.out.println(RepaintManager.currentManager(fc).isDoubleBufferingEnabled());
            //returns true
            //fc.repaint();
            // in response to a button click:
            int returnVal = fc.showOpenDialog(frame);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
            } else {
                System.out.println("Open command cancelled by user.");
            }
        }
    });

编辑2:在Linux机器上,一切都像一个魅力。

我测试了那个代码,它对我来说很好。窗口打开大约2分钟,一切都正常。滞后什么时候开始?单击按钮时?移动窗户?

相关内容

  • 没有找到相关文章

最新更新