java - MDI JTable 重叠区域重绘

标签 java swing mdi jdesktoppane

我的应用程序在 MDI 模式下遇到问题。 我在 JDesktopPane 下打开许多窗口(JInternalFrame 包含 JTable)。有些窗口是重叠的,当它们在表中接收更新时,似乎重新绘制了所有重叠的窗口,而不仅仅是其本身。这使得我的应用程序性能变得很差,拖放现有窗口或打开新窗口的响应缓慢。 为了证明这一点,我做了一个简单的例子,打开许多简单的表,并有一个线程每 200 毫秒更新一次表的值。当我打开大约20个窗口后,性能又变差了。 如果有人和我遇到同样的问题,有什么解决问题的建议吗?

请帮忙!!!!!!

public class TestMDI extends JFrame {
private static final long serialVersionUID = 1L;


private JPanel contentPanel;
private JDesktopPane desktopPane;
private JMenuBar menuBar;
private List<TestPanel> allScreens = new ArrayList<TestPanel>();
private List<JDialog> freeFloatDialogs = new ArrayList<JDialog>();
private List<JInternalFrame> mdiInternalFrm = new ArrayList<JInternalFrame>();
int x = 0;
int y = 0;
int index = 0;


private static int MDI_MODE = 0;
private static int FREE_FLOAT_MODE = 1;
private int windowMode = MDI_MODE;


public TestMDI() {
    init();
}


public static void main(String[] args) {
    new TestMDI().show();
}


public void init() {
    contentPanel = new JPanel();
    desktopPane = new JDesktopPane();
    desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
    desktopPane.setFocusTraversalKeysEnabled(false);
    desktopPane.setFocusTraversalPolicyProvider(false);
    desktopPane.setBorder(null);
    desktopPane.setIgnoreRepaint(true);
    desktopPane.setPreferredSize(new Dimension(1000, 800));
    this.setSize(new Dimension(1000, 800));
    menuBar = new JMenuBar();
    JMenu menu1 = new JMenu("Test");
    JMenuItem menuItem1 = new JMenuItem("Open Lable Screen");
    menuItem1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 1; i < 4; i++) {
                final TestJLableScreen screen = new TestJLableScreen("Screen  " + (allScreens.size() + 1));
                screen.startTime();
                if (windowMode == MDI_MODE) {
                    JInternalFrame frame = createInternalFram(screen);
                    desktopPane.add(frame);
                    mdiInternalFrm.add(frame);
                    if (allScreens.size() * 60 + 100 < 1000) {
                        x = allScreens.size() * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    frame.setLocation(x, y);
                    frame.setVisible(true);
                } else {
                    JDialog dialog = createJDialog(screen);
                    freeFloatDialogs.add(dialog);
                    if (i * 60 + 100 < 1000) {
                        x = i * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    dialog.setLocation(x, y);
                    dialog.setVisible(true);
                }


                allScreens.add(screen);
            }
        }
    });


    JMenuItem menuItem2 = new JMenuItem("Open Table Screen");
    menuItem2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 1; i < 4; i++) {
                TestTableScreen screen = new TestTableScreen("Screen  " + (allScreens.size() + 1));
                screen.startTime();
                if (windowMode == MDI_MODE) {
                    JInternalFrame frame = createInternalFram(screen);
                    desktopPane.add(frame);
                    mdiInternalFrm.add(frame);
                    if (allScreens.size() * 60 + 100 < 1000) {
                        x = allScreens.size() * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    frame.setLocation(x, y);
                    frame.setVisible(true);
                } else {
                    JDialog dialog = createJDialog(screen);
                    freeFloatDialogs.add(dialog);
                    if (i * 60 + 100 < 1000) {
                        x = i * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    dialog.setLocation(x, y);
                    dialog.setVisible(true);
                }
                allScreens.add(screen);
            }
        }
    });
    menu1.add(menuItem1);
    menu1.add(menuItem2);
    this.setJMenuBar(menuBar);
    this.getJMenuBar().add(menu1);
    this.getJMenuBar().add(createSwitchMenu());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(desktopPane);
    desktopPane.setDesktopManager(null);
}


public JInternalFrame createInternalFram(final TestPanel panel) {
    final CustomeInternalFrame internalFrame = new CustomeInternalFrame(panel.getTitle(), true, true, true, true) {
        public void doDefaultCloseAction() {
            super.doDefaultCloseAction();
            allScreens.remove(panel);
        }
    };
    internalFrame.setPanel(panel);
    internalFrame.setSize(new Dimension(1010, 445));
    internalFrame.add(panel);
    internalFrame.setFocusTraversalKeysEnabled(false);
    internalFrame.setFocusTraversalPolicyProvider(false);


    desktopPane.getDesktopManager();
    internalFrame.setIgnoreRepaint(true);
    return internalFrame;
}


public JDialog createJDialog(final TestPanel panel) {
    JDialog dialog = new JDialog(this, panel.getTitle());
    dialog.setSize(new Dimension(1010, 445));
    dialog.add(panel);
    dialog.addWindowListener(new WindowAdapter() {


        public void windowClosing(WindowEvent e) {
            allScreens.remove(panel);
        }
    });
    return dialog;
}


public JMenu createSwitchMenu() {
    JMenu menu = new JMenu("Test2");
    JMenuItem menuItem1 = new JMenuItem("Switch FreeFloat");
    menuItem1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            windowMode = FREE_FLOAT_MODE;
            for (JInternalFrame frm : mdiInternalFrm) {
                frm.setVisible(false);
                frm.dispose();
                frm = null;
            }
            mdiInternalFrm.clear();
            remove(desktopPane);
            desktopPane.removeAll();
            repaint();
            add(contentPanel);
            index = 0;


            for (JDialog dialog : freeFloatDialogs) {
                dialog.setVisible(false);
                dialog.dispose();
                dialog = null;
            }


            freeFloatDialogs.clear();
            for (int i = 0; i < allScreens.size(); i++) {
                JDialog dialog = createJDialog(allScreens.get(i));
                freeFloatDialogs.add(dialog);
                if (i * 60 + 100 < 1000) {
                    x = i * 60;
                    y = 60;
                } else {
                    x = 60 * index;
                    y = 120;
                    index++;
                }
                dialog.setLocation(x, y);
                dialog.setVisible(true);
            }
        }
    });
    JMenuItem menuItem2 = new JMenuItem("Switch MDI");
    menuItem2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            windowMode = MDI_MODE;
            remove(contentPanel);
            add(desktopPane);
            for (int i = 0; i < freeFloatDialogs.size(); i++) {
                freeFloatDialogs.get(i).setVisible(false);
                freeFloatDialogs.get(i).dispose();
            }
            freeFloatDialogs.clear();
            repaint();
            for (JInternalFrame frm : mdiInternalFrm) {
                frm.setVisible(false);
                frm.dispose();
                frm = null;
            }
            mdiInternalFrm.clear();
            index = 0;
            for (int i = 0; i < allScreens.size(); i++) {
                JInternalFrame frame = createInternalFram(allScreens.get(i));
                desktopPane.add(frame);
                mdiInternalFrm.add(frame);
                if (i * 60 + 100 < 1000) {
                    x = i * 60;
                    y = 60;
                } else {
                    x = 60 * index;
                    y = 120;
                    index++;
                }
                frame.setLocation(x, y);
                frame.setVisible(true);
            }
        }
    });
    menu.add(menuItem1);
    menu.add(menuItem2);
    return menu;
}
}

public class TestTableScreen extends TestPanel {
private static final long serialVersionUID = 1L;
JTable testTable = new JTable();
MyTableModel tableModel1 = new MyTableModel(1);
private boolean notRepaint = false;
int start = 0;
JScrollPane scrollPane = new JScrollPane();
private Timer timmer = new Timer(200, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Random indexRandom = new Random();
        final int index = indexRandom.nextInt(50);
        Random valRandom = new Random();
        final int val = valRandom.nextInt(600);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                notRepaint = false;
                TestTableScreen.this.update(index + "|" + val);
            }
        });
    }
});


public TestTableScreen(String title) {
    this.title = title;
    init();
    tableModel1.setTabelName(title);
}


public void startTime() {
    timmer.start();
}


public String getTitle() {
    return title;
}


public void update(String updateStr) {
    String[] val = updateStr.split("\\|");
    if (val.length == 2) {
        int index = Integer.valueOf(val[0]);
        List vals = tableModel1.getVector();
        if (vals.size() > index) {
            vals.set(index, val[1]);
        } else {
            vals.add(val[1]);
        }
        tableModel1.fireTableDataChanged();
    }
}


public TableModel getTableModel() {
    return tableModel1;
}


public void init() {
    testTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    testTable.setRowSelectionAllowed(true);
    this.testTable.setModel(tableModel1);
    int[] width = { 160, 80, 45, 98, 60, 88, 87, 88, 80, 70, 88, 80, 75, 87, 87, 41, 88, 82, 75, 68, 69 };
    TableColumnModel columnModel = testTable.getColumnModel();
    for (int i = 0; i < width.length; i++) {
        columnModel.getColumn(i).setPreferredWidth(width[i]);
    }
    testTable.setRowHeight(20);
    tableModel1.fireTableDataChanged();
    this.setLayout(new BorderLayout());


    TableColumnModel columnMode2 = testTable.getColumnModel();
    int[] width2 = { 200 };
    for (int i = 0; i < width2.length; i++) {
        columnMode2.getColumn(i).setPreferredWidth(width2[i]);
    }
    scrollPane.getViewport().add(testTable);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    this.add(scrollPane, BorderLayout.CENTER);
}


class MyTableModel extends DefaultTableModel {
    public List list = new ArrayList();
    String titles[] = new String[] { "袨怓1", "袨怓2", "袨怓3", "袨怓4", "袨怓5", "袨怓6", "袨怓7", "袨怓8", "袨怓9", "袨怓10", "袨怓11",
            "袨怓12", "袨怓13", "袨怓14", "袨怓15", "袨怓16", "袨怓17", "袨怓18", "袨怓19", "袨怓20", "袨怓21" };
    String tabelName = "";


    int type_head = 0;
    int type_data = 1;
    int type = 1;


    public MyTableModel(int type) {
        super();
        this.type = type;
        for (int i = 0; i < 50; i++) {
            list.add(i);
        }
    }


    public void setTabelName(String name) {
        this.tabelName = name;
    }


    public int getRowCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }


    public List getVector() {
        return list;
    }


    public int getColumnCount() {
        if (type == 0) {
            return 1;
        } else {
            return titles.length;
        }
    }


    public String getColumnName(int c) {
        if (type == 0) {
            return "head";
        } else {
            return titles[c];
        }
    }


    public boolean isCellEditable(int nRow, int nCol) {
        return false;
    }


    public Object getValueAt(int r, int c) {
        if (list.size() == 0) {
            return null;
        }


        switch (c) {
        default:
            if (type == 0) {
                return r + " " + c + "  test ";
            } else {
                return list.get(r) + "   " + c;
            }
        }
    }
}


public boolean isNotRepaint() {
    return notRepaint;
}


public void setNotRepaint(boolean notRepaint) {
    this.notRepaint = notRepaint;
}
}

public class TestPanel extends JPanel {
protected String title = "";
protected boolean needRepaint = false;
protected boolean isFirstOpen = true;


public String getTitle() {
    return title;
}


public void setNeedRepaint(boolean flag) {
    this.needRepaint = flag;
}


public boolean isNeedRepaint() {
    return needRepaint;
}


public boolean isFirstOpen() {
    return isFirstOpen;
}


public void setFirstOpen(boolean isFirstOpen) {
    this.isFirstOpen = isFirstOpen;
}
}


public class TestJLableScreen extends TestPanel {
private static final long serialVersionUID = 1L;
private JLabel[] allLables = new JLabel[20];
private Timer timmer = new Timer(20, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Random indexRandom = new Random();
        final int index = indexRandom.nextInt(10);
        Random valRandom = new Random();
        final int val = valRandom.nextInt(600);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestJLableScreen.this.setNeedRepaint(true);
                TestJLableScreen.this.update(index + "|" + val);
            }
        });
    }
});


public TestJLableScreen(String title) {
    this.title = title;
    init();
}


public void startTime() {
    timmer.start();
}


public String getTitle() {
    return title;
}


public void update(String updateStr) {
    String[] val = updateStr.split("\\|");
    if (val.length == 2) {
        int index = Integer.valueOf(val[0]);
        allLables[index * 2 + 1].setText(val[1]);
    }
}


public void init() {
    this.setLayout(new GridLayout(10, 2));
    boolean flag = true;
    for (int i = 0; i < allLables.length; i++) {
        allLables[i] = new JLabel() {
            public void paint(Graphics g) {
                super.paint(g);
            }
        };
        allLables[i].setName("" + i);
        if (i % 2 == 0) {
            allLables[i].setText("Name " + i + "  : ");
        } else {
            allLables[i].setOpaque(true);
            if (flag) {
                allLables[i].setBackground(Color.YELLOW);
                flag = false;
            } else {
                allLables[i].setBackground(Color.CYAN);
                flag = true;
            }
            allLables[i].setText(i * 8 + "");
        }
    }
    for (int i = 0; i < allLables.length; i++) {
        this.add(allLables[i]);
    }
}
}


public class CustomeInternalFrame extends JInternalFrame {
protected TestPanel panel;


public CustomeInternalFrame() {
    this("", false, false, false, false);
}


public CustomeInternalFrame(String title) {
    this(title, false, false, false, false);
}


public CustomeInternalFrame(String title, boolean resizable) {
    this(title, resizable, false, false, false);
}


public CustomeInternalFrame(String title, boolean resizable, boolean closable) {
    this(title, resizable, closable, false, false);
}


public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) {
    this(title, resizable, closable, maximizable, false);
}


public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable,
        boolean iconifiable) {
    super(title, resizable, closable, maximizable, iconifiable);
}


public TestPanel getPanel() {
    return panel;
}


public void setPanel(TestPanel panel) {
    this.panel = panel;
}


}

最佳答案

  1. 请勿使用 setIgnoreRepaint,这可能会产生不需要的绘画效果或无法在需要时更新组件
  2. desktopPane.setDesktopManager(null) 正在自找麻烦。
  3. 您不应为 TestTableScreen 的每个实例使用 javax.swing.Timer,而应使用一个 Timer,然后向多个收件人提供通知。
  4. 不要调用fireTableDataChanged,这会导致整个表重新验证,包括列和所有行,这是非常非常昂贵的。
  5. 最好使用适当的监听器来监视组件的更改,而不是执行匿名方法覆盖。

这本质上是一个经过删减的版本,演示了单个“主”计时器的使用以及对表更新过程的更改。

它还使用 WindowListenerInternalFrameListener 在父窗口关闭时从 Timer 取消注册 View ,因此我们不会更新不再在屏幕上的组件。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class TestMDI extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel contentPanel;
    private JDesktopPane desktopPane;
    private JMenuBar menuBar;
    private List<TestPanel> allScreens = new ArrayList<TestPanel>();
    private List<JDialog> freeFloatDialogs = new ArrayList<JDialog>();
    private List<JInternalFrame> mdiInternalFrm = new ArrayList<JInternalFrame>();
    int x = 0;
    int y = 0;
    int index = 0;

    private static int MDI_MODE = 0;
    private static int FREE_FLOAT_MODE = 1;
    private int windowMode = MDI_MODE;

    private Timer masterTimer;

    public TestMDI() {
        init();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                TestMDI frame = new TestMDI();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public void init() {
        masterTimer = new Timer(200, null);
        masterTimer.start();
        contentPanel = new JPanel();
        desktopPane = new JDesktopPane();
        desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
        desktopPane.setFocusTraversalKeysEnabled(false);
        desktopPane.setFocusTraversalPolicyProvider(false);
        desktopPane.setBorder(null);
//        desktopPane.setIgnoreRepaint(true);
        desktopPane.setPreferredSize(new Dimension(400, 400));
//        this.setSize(new Dimension(1000, 800));
        this.pack();
        menuBar = new JMenuBar();
        JMenu menu1 = new JMenu("Test");
        JMenuItem menuItem1 = new JMenuItem("Open Lable Screen");
        menuItem1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 1; i < 4; i++) {
                    final TestJLableScreen screen = new TestJLableScreen("Screen  " + (allScreens.size() + 1));
                    masterTimer.addActionListener(screen);
                    if (windowMode == MDI_MODE) {
                        JInternalFrame frame = createInternalFram(screen);
                        desktopPane.add(frame);
                        mdiInternalFrm.add(frame);
                        if (allScreens.size() * 60 + 100 < 1000) {
                            x = allScreens.size() * 60;
                            y = 60;
                        } else {
                            x = 60 * index;
                            y = 120;
                            index++;
                        }
                        frame.setLocation(x, y);
                        frame.setVisible(true);
                    } else {
                        JDialog dialog = createJDialog(screen);
                        freeFloatDialogs.add(dialog);
                        if (i * 60 + 100 < 1000) {
                            x = i * 60;
                            y = 60;
                        } else {
                            x = 60 * index;
                            y = 120;
                            index++;
                        }
                        dialog.setLocation(x, y);
                        dialog.setVisible(true);
                    }

                    allScreens.add(screen);
                }
            }
        });

        JMenuItem menuItem2 = new JMenuItem("Open Table Screen");
        menuItem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 1; i < 4; i++) {
                    TestTableScreen screen = new TestTableScreen("Screen  " + (allScreens.size() + 1));
                    masterTimer.addActionListener(screen);
                    if (windowMode == MDI_MODE) {
                        JInternalFrame frame = createInternalFram(screen);
                        frame.addInternalFrameListener(new InternalFrameHandler(screen, masterTimer));
                        desktopPane.add(frame);
                        mdiInternalFrm.add(frame);
                        if (allScreens.size() * 60 + 100 < 1000) {
                            x = allScreens.size() * 60;
                            y = 60;
                        } else {
                            x = 60 * index;
                            y = 120;
                            index++;
                        }
                        frame.setLocation(x, y);
                        frame.setVisible(true);
                    } else {
                        JDialog dialog = createJDialog(screen);
                        dialog.addWindowListener(new WindowHandler(screen, masterTimer));
                        freeFloatDialogs.add(dialog);
                        if (i * 60 + 100 < 1000) {
                            x = i * 60;
                            y = 60;
                        } else {
                            x = 60 * index;
                            y = 120;
                            index++;
                        }
                        dialog.setLocation(x, y);
                        dialog.setVisible(true);
                    }
                    allScreens.add(screen);
                }
            }
        });
        menu1.add(menuItem1);
        menu1.add(menuItem2);
        this.setJMenuBar(menuBar);
        this.getJMenuBar().add(menu1);
        this.getJMenuBar().add(createSwitchMenu());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(desktopPane);
//        desktopPane.setDesktopManager(null);
    }

    public JInternalFrame createInternalFram(final TestPanel panel) {
        final CustomeInternalFrame internalFrame = new CustomeInternalFrame(panel.getTitle(), true, true, true, true) {
            public void doDefaultCloseAction() {
                super.doDefaultCloseAction();
                allScreens.remove(panel);
            }
        };
        internalFrame.setPanel(panel);
        internalFrame.setSize(new Dimension(1010, 445));
        internalFrame.add(panel);
        internalFrame.setFocusTraversalKeysEnabled(false);
        internalFrame.setFocusTraversalPolicyProvider(false);

        desktopPane.getDesktopManager();
//        internalFrame.setIgnoreRepaint(true);
        return internalFrame;
    }

    public JDialog createJDialog(final TestPanel panel) {
        JDialog dialog = new JDialog(this, panel.getTitle());
        dialog.setSize(new Dimension(1010, 445));
        dialog.add(panel);
        dialog.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                allScreens.remove(panel);
            }
        });
        return dialog;
    }

    public JMenu createSwitchMenu() {
        JMenu menu = new JMenu("Test2");
        JMenuItem menuItem1 = new JMenuItem("Switch FreeFloat");
        menuItem1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                windowMode = FREE_FLOAT_MODE;
                for (JInternalFrame frm : mdiInternalFrm) {
                    frm.setVisible(false);
                    frm.dispose();
                    frm = null;
                }
                mdiInternalFrm.clear();
                remove(desktopPane);
                desktopPane.removeAll();
                repaint();
                add(contentPanel);
                index = 0;

                for (JDialog dialog : freeFloatDialogs) {
                    dialog.setVisible(false);
                    dialog.dispose();
                    dialog = null;
                }

                freeFloatDialogs.clear();
                for (int i = 0; i < allScreens.size(); i++) {
                    JDialog dialog = createJDialog(allScreens.get(i));
                    dialog.addWindowListener(new WindowHandler((ActionListener)allScreens.get(i), masterTimer));
                    freeFloatDialogs.add(dialog);
                    if (i * 60 + 100 < 1000) {
                        x = i * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    dialog.setLocation(x, y);
                    dialog.setVisible(true);
                }
            }
        });
        JMenuItem menuItem2 = new JMenuItem("Switch MDI");
        menuItem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                windowMode = MDI_MODE;
                remove(contentPanel);
                add(desktopPane);
                for (int i = 0; i < freeFloatDialogs.size(); i++) {
                    freeFloatDialogs.get(i).setVisible(false);
                    freeFloatDialogs.get(i).dispose();
                }
                freeFloatDialogs.clear();
                repaint();
                for (JInternalFrame frm : mdiInternalFrm) {
                    frm.setVisible(false);
                    frm.dispose();
                    frm = null;
                }
                mdiInternalFrm.clear();
                index = 0;
                for (int i = 0; i < allScreens.size(); i++) {
                    JInternalFrame frame = createInternalFram(allScreens.get(i));
                    frame.addInternalFrameListener(new InternalFrameHandler((ActionListener)allScreens.get(i), masterTimer));
                    desktopPane.add(frame);
                    mdiInternalFrm.add(frame);
                    if (i * 60 + 100 < 1000) {
                        x = i * 60;
                        y = 60;
                    } else {
                        x = 60 * index;
                        y = 120;
                        index++;
                    }
                    frame.setLocation(x, y);
                    frame.setVisible(true);
                }
            }
        });
        menu.add(menuItem1);
        menu.add(menuItem2);
        return menu;
    }

    public class InternalFrameHandler extends InternalFrameAdapter {

        private ActionListener listener;
        private Timer timer;

        public InternalFrameHandler(ActionListener listener, Timer timer) {
            this.listener = listener;
            this.timer = timer;
        }

        @Override
        public void internalFrameClosing(InternalFrameEvent e) {
            timer.removeActionListener(listener);
        }

    }

    public class WindowHandler extends WindowAdapter {

        private ActionListener listener;
        private Timer timer;

        public WindowHandler(ActionListener listener, Timer timer) {
            this.listener = listener;
            this.timer = timer;
        }

        @Override
        public void windowClosing(WindowEvent e) {
            timer.removeActionListener(listener);
        }

    }

    public class TestTableScreen extends TestPanel implements ActionListener {

        private static final long serialVersionUID = 1L;
        JTable testTable = new JTable();
        MyTableModel tableModel1 = new MyTableModel(1);
//        private boolean notRepaint = false;
        int start = 0;
        JScrollPane scrollPane = new JScrollPane();
//        private Timer timmer = new Timer(200, new ActionListener() {
//            public void actionPerformed(ActionEvent e) {
//                Random indexRandom = new Random();
//                final int index = indexRandom.nextInt(50);
//                Random valRandom = new Random();
//                final int val = valRandom.nextInt(600);
//                TestTableScreen.this.update(index + "|" + val);
////                SwingUtilities.invokeLater(new Runnable() {
////                    public void run() {
//////                        notRepaint = false;
////                    }
////                });
//            }
//        });

        public TestTableScreen(String title) {
            this.title = title;
            init();
            tableModel1.setTabelName(title);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Random indexRandom = new Random();
            final int index = indexRandom.nextInt(50);
            Random valRandom = new Random();
            final int val = valRandom.nextInt(600);
            update(index + "|" + val);
        }

//        public void startTime() {
//            timmer.start();
//        }
        public String getTitle() {
            return title;
        }

        public void update(String updateStr) {
            String[] val = updateStr.split("\\|");
            if (val.length == 2) {
                int index = Integer.valueOf(val[0]);

                if (tableModel1.getRowCount() > index) {
                    tableModel1.setValueAt(val[1], index, 0);
                } else {
                    tableModel1.add(val[1]);
                }

//                List vals = tableModel1.getVector();
//                if (vals.size() > index) {
//                    vals.set(index, val[1]);
//                } else {
//                    vals.add(val[1]);
//                }
//                tableModel1.fireTableDataChanged();
            }
        }

        public TableModel getTableModel() {
            return tableModel1;
        }

        public void init() {
            testTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            testTable.setRowSelectionAllowed(true);
            this.testTable.setModel(tableModel1);
            int[] width = {160, 80, 45, 98, 60, 88, 87, 88, 80, 70, 88, 80, 75, 87, 87, 41, 88, 82, 75, 68, 69};
            TableColumnModel columnModel = testTable.getColumnModel();
            for (int i = 0; i < width.length; i++) {
                columnModel.getColumn(i).setPreferredWidth(width[i]);
            }
            testTable.setRowHeight(20);
            tableModel1.fireTableDataChanged();
            this.setLayout(new BorderLayout());

            TableColumnModel columnMode2 = testTable.getColumnModel();
            int[] width2 = {200};
            for (int i = 0; i < width2.length; i++) {
                columnMode2.getColumn(i).setPreferredWidth(width2[i]);
            }
            scrollPane.getViewport().add(testTable);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            this.add(scrollPane, BorderLayout.CENTER);
        }

        class MyTableModel extends AbstractTableModel {

            public List list = new ArrayList();
            String titles[] = new String[]{"??1", "??2", "??3", "??4", "??5", "??6", "??7", "??8", "??9", "??10", "??11",
                "??12", "??13", "??14", "??15", "??16", "??17", "??18", "??19", "??20", "??21"};
            String tabelName = "";

            int type_head = 0;
            int type_data = 1;
            int type = 1;

            public MyTableModel(int type) {
                super();
                this.type = type;
                for (int i = 0; i < 50; i++) {
                    list.add(i);
                }
            }

            public void add(String value) {
                list.add(value);
            }

            public void setTabelName(String name) {
                this.tabelName = name;
            }

            public int getRowCount() {
                if (list != null) {
                    return list.size();
                }
                return 0;
            }

            public List getVector() {
                return list;
            }

            public int getColumnCount() {
                if (type == 0) {
                    return 1;
                } else {
                    return titles.length;
                }
            }

            public String getColumnName(int c) {
                if (type == 0) {
                    return "head";
                } else {
                    return titles[c];
                }
            }

            public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }

            public Object getValueAt(int r, int c) {
                if (list.size() == 0) {
                    return null;
                }

                switch (c) {
                    default:
                        if (type == 0) {
                            return r + " " + c + "  test ";
                        } else {
                            return list.get(r) + "   " + c;
                        }
                }
            }

            @Override
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
                if (aValue instanceof String) {
                    list.set(rowIndex, aValue);
                    fireTableRowsUpdated(rowIndex, rowIndex);
                }
            }
        }

//        public boolean isNotRepaint() {
//            return notRepaint;
//        }
//
//        public void setNotRepaint(boolean notRepaint) {
//            this.notRepaint = notRepaint;
//        }
    }

    public class TestPanel extends JPanel {

        protected String title = "";
//        protected boolean needRepaint = false;
        protected boolean isFirstOpen = true;

        public String getTitle() {
            return title;
        }
//
//        public void setNeedRepaint(boolean flag) {
//            this.needRepaint = flag;
//        }
//
//        public boolean isNeedRepaint() {
//            return needRepaint;
//        }

        public boolean isFirstOpen() {
            return isFirstOpen;
        }

        public void setFirstOpen(boolean isFirstOpen) {
            this.isFirstOpen = isFirstOpen;
        }
    }

    public class TestJLableScreen extends TestPanel implements ActionListener {

        private static final long serialVersionUID = 1L;
        private JLabel[] allLables = new JLabel[20];
//        private Timer timmer = new Timer(20, new ActionListener() {
//            public void actionPerformed(ActionEvent e) {
//                Random indexRandom = new Random();
//                final int index = indexRandom.nextInt(10);
//                Random valRandom = new Random();
//                final int val = valRandom.nextInt(600);
//                SwingUtilities.invokeLater(new Runnable() {
//                    public void run() {
//                        TestJLableScreen.this.setNeedRepaint(true);
//                        TestJLableScreen.this.update(index + "|" + val);
//                    }
//                });
//            }
//        });

        public TestJLableScreen(String title) {
            this.title = title;
            init();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Random indexRandom = new Random();
            final int index = indexRandom.nextInt(10);
            Random valRandom = new Random();
            final int val = valRandom.nextInt(600);
            update(index + "|" + val);
        }

//        public void startTime() {
//            timmer.start();
//        }

        public String getTitle() {
            return title;
        }

        public void update(String updateStr) {
            String[] val = updateStr.split("\\|");
            if (val.length == 2) {
                int index = Integer.valueOf(val[0]);
                allLables[index * 2 + 1].setText(val[1]);
            }
        }

        public void init() {
            this.setLayout(new GridLayout(10, 2));
            boolean flag = true;
            for (int i = 0; i < allLables.length; i++) {
                allLables[i] = new JLabel() {
                    public void paint(Graphics g) {
                        super.paint(g);
                    }
                };
                allLables[i].setName("" + i);
                if (i % 2 == 0) {
                    allLables[i].setText("Name " + i + "  : ");
                } else {
                    allLables[i].setOpaque(true);
                    if (flag) {
                        allLables[i].setBackground(Color.YELLOW);
                        flag = false;
                    } else {
                        allLables[i].setBackground(Color.CYAN);
                        flag = true;
                    }
                    allLables[i].setText(i * 8 + "");
                }
            }
            for (JLabel allLable : allLables) {
                this.add(allLable);
            }
        }
    }

    public class CustomeInternalFrame extends JInternalFrame {

        protected TestPanel panel;

        public CustomeInternalFrame() {
            this("", false, false, false, false);
        }

        public CustomeInternalFrame(String title) {
            this(title, false, false, false, false);
        }

        public CustomeInternalFrame(String title, boolean resizable) {
            this(title, resizable, false, false, false);
        }

        public CustomeInternalFrame(String title, boolean resizable, boolean closable) {
            this(title, resizable, closable, false, false);
        }

        public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) {
            this(title, resizable, closable, maximizable, false);
        }

        public CustomeInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable,
                boolean iconifiable) {
            super(title, resizable, closable, maximizable, iconifiable);
        }

        public TestPanel getPanel() {
            return panel;
        }

        public void setPanel(TestPanel panel) {
            this.panel = panel;
        }

    }
}

关于java - MDI JTable 重叠区域重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23362015/

相关文章:

java - 在 Java 中处理具体类型时 instanceof 的替代方法

java - Android java utf-8编码字符串

标题栏下的 Java Swing 白色间隙

java - JFrame 找不到 'pack' 方法

delphi - 在表单的每个不同实例中,如何在变量中具有不同的值?

c# - 合并工具条 MDI 子级 - 父级

java - 在 java 中,我如何让一个类既实现 MouseListener 又抛出 IOException?

java - 通过清理杀死一个可运行的 jar ?

java swing线程回调

c# - 将窗体停靠在 MDI 上