我在MDI
模式下的应用程序有问题。我在下打开了许多窗口(JInternalFrame
包含JTable
)JDesktopPane
。一些窗口是重叠的,当它们在表中收到更新时,似乎重新绘制了所有重叠的窗口,而不仅仅是它本身。这使我的应用程序性能变差,对拖放现有窗口或打开新窗口的响应缓慢。为了证明这一点,我做了一个简单的例子来打开许多简单的表,并有一个线程每 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;
}
}