我有一个主类,它在程序启动时加载我的应用程序窗口,如以下代码所示:
public class SwendaEye{
public static void main(String[]args){
FrameandComp frame = new FrameandComp();
JFrame win;
win = frame.mainFrame();
/*========================================================================*/
JMenuBar bar;
bar = new JMenuBar();
win.setJMenuBar(bar);
JMenu swenda = new JMenu("SWENDAEYE");// adding Swenda menu to the bar.
bar.add(swenda);
JMenuItem open = new JMenuItem("Open");
swenda.add(open);
JMenuItem exit = new JMenuItem("Exit");
swenda.add(exit);
JMenu tools = new JMenu("Tools");// adding Tools menu to the bar.
bar.add(tools);
JMenuItem convertIP = new JMenuItem("Convert IP Address");
tools.add(convertIP);
JMenuItem convertDomain = new JMenuItem("Convert Domain Name");
tools.add(convertDomain);
Domain dom = new Domain();
dom.iniTheEvent(convertDomain);
convertDomain.addActionListener(dom);
JMenu view = new JMenu("View"); // adding Swenda menu to the bar.
bar.add(view);
JMenuItem webDetail = new JMenuItem("Website header");
view.add(webDetail);
JMenuItem report = new JMenuItem("Report");
view.add(report);
win.setVisible(true);
}
}
例如,当我单击域转换器时,我应该能够触发在Domain
类中设置的新窗口。在我的情况下,窗口确实打开了,但是“域转换器”窗口包含一个 Menu
在类中定义的,Main
当它应该只有一个按钮和一个文本字段时。正如你所看到的,我还没有在课堂上实施或发起一个Menu
或任何地方。MenuItem
Domain
public class Domain implements ActionListener{
FrameandComp frame = new FrameandComp();
JFrame domFrame;
String DomainTitle= "Domain Converter";
TextField text = new TextField(20);
JButton bot = new JButton("Convert");
JMenuItem men;
public Domain(){
}
public void createFrame(){
FlowLayout flow = new FlowLayout(); // Create a layout manager
domFrame =frame.setFrame(400,200,DomainTitle,JFrame.DISPOSE_ON_CLOSE);
Container content = domFrame.getContentPane(); // Get the content pane
content.setLayout(flow);
content.add(text);
content.add(bot);
domFrame.add(content);
domFrame.setVisible(true);
}
public JMenuItem iniTheEvent(JMenuItem menuIt){
return men = menuIt;
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == men ){
this.createFrame();
}
}
}
我希望能够单击域转换器MenuItem
并能够打开一个新窗口,而无需从主窗口继承任何菜单功能。
有人能告诉我我在应用程序中做错了什么导致我的新窗口继承了主窗口的菜单吗?
您的支持将不胜感激。
该类FrameandComp
用于初始化所有JFrames
:
public class FrameandComp {
static JFrame awindow = new JFrame();
public FrameandComp(){}
public JFrame setFrame(int width, int height, String title, int exitRule){
Toolkit thekit = awindow.getToolkit();
Dimension wndsize = thekit.getScreenSize();
awindow.setBounds(wndsize.width/4,wndsize.height/4,width, height);
awindow.getContentPane().setBackground(Color.DARK_GRAY);
awindow.setTitle(title);
awindow.setDefaultCloseOperation(exitRule);
return awindow;
}
public JFrame mainFrame(){
Toolkit thekit = awindow.getToolkit();
Dimension wndsize = thekit.getScreenSize();
awindow.setBounds(wndsize.width/4,wndsize.height/4,
((2 *wndsize.width)/2), ((2 *wndsize.height)/2));
awindow.getContentPane().setBackground(Color.DARK_GRAY);
awindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
awindow.setTitle("SWENDAEYE");
return awindow;
}
}