我在swing中创建MDI应用程序我想使用哈希映射一次显示一个JInternalFrame。我无法传递密钥和值。我将密钥作为JInternalJframe name
传递,值为object of JInternalJframe
。我无法继续。我粘贴了不完整的代码。为了让你明白。
import java.util.*;
import javax.swing.*;
public class HashMap {
static HashMap <String, JInternalFrame> myMap = new HashMap <>();
//String(key) is the name of JInternalFrame
//JInternalFrame(value) is name of object of JInternalFrame
public static void main(String [] ashu){
myMap.put("CityMaster",cm);
myMap.put("TransportMaster",tm);
myMap.put("AccountMaster",am);
myMap.put("BankMaster",bm);
for (String str: myMap.keySet()){
System.out.println(str);
}
for (JInternalFrame jf: myMap.values()){
System.out.println(jf);
}
}
}
我已经创建了上面命名为CityMaster,BankMaster等的JInternalFrame。
答案 0 :(得分:0)
试一试。没什么好解释的。我刚刚将一些InternalFrame添加到DesktopPane,就像你应该做的那样。无视任何地方的setSize()
。这只是为了简洁和我的懒惰。你不应该使用set size
import java.awt.BorderLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.*;
import java.util.logging.*;
import javax.swing.*;
public class HashMapFrame {
Map<String, MyInternalFrame> iFrames;
String[] keys = {"TransportMaster", "AccountMaster", "BankMaster",
"GrandMaster", "WebMaster", "StackOverflowMaster"};
JDesktopPane desktop;
JButton button = new JButton("Add Frame");
int index = 0;
int x = 0;
int y = 0;
public HashMapFrame() {
desktop = new JDesktopPane();
iFrames = new HashMap<>();
for (String s : keys) {
iFrames.put(s, new MyInternalFrame(s, x, y));
x += 30;
y += 30;
}
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (index < 6) {
desktop.add(iFrames.get(keys[index]));
iFrames.get(keys[index]).setVisible(true);
try {
iFrames.get(keys[index]).setSelected(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(HashMapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
index++;
}
}
});
JFrame frame = new JFrame();
frame.add(desktop);
frame.add(button, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 550);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HashMapFrame();
}
});
}
}
class MyInternalFrame extends JInternalFrame {
public MyInternalFrame(String title, int x, int y) {
super(title);
setSize(300, 300);
setLocation(x, y);
setClosable(true);
}
}