JAVA 创建一个带菜单的窗口,点击菜单项,窗口显示不同内容,这个怎么实现?最好用卡片布局。

或者看看我这程序问题是什么?怎么改正?importjava.awt.*;importjava.awt.event.*;publicclassXtLcbextendsFra... 或者看看我这程序问题是什么?怎么改正?
import java.awt.*;
import java.awt.event.*;
public class XtLcb extends Frame
{
public static void main(String args[])
{
Frame frame = new Frame();
frame.setBackground(Color.lightGray);
frame.setSize(400,400);
frame.setVisible(true);
CardLayout cl = new CardLayout();

MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
MenuItem menuFileOpen = new MenuItem();
MenuItem menuFileOn = new MenuItem();

menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileOpen.setLabel("Open");
menuFileOn.setLabel("On");

Panel p = new Panel();
p.setLayout(cl);
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.add(new Label("Open"));
p2.add(new Label("On"));
p.add(p1,"1");
p.add(p2,"2");
frame.add(p);

// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
menuFileOpen.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.show(p,"1");
}
}

);
menuFileOn.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.show(p,"2");
}
}
);
menuFile.add(menuFileOpen);
menuFile.add(menuFileOn);
menuFile.add(menuFileExit);
menuBar.add(menuFile);

frame.setMenuBar(menuBar);

// Add window listener.
frame.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);

}
}
);
}

/**
* Shutdown procedure when run as an application.
*/
/*void windowClosed()
{

// TODO: Check if it is safe to close the application

// Exit application.
System.exit(0);
}*/
}
展开
 我来答
sayidming
2007-12-09 · TA获得超过1077个赞
知道小有建树答主
回答量:382
采纳率:100%
帮助的人:124万
展开全部
你这个程序就是卡片布局了,有2个小错误就是没有对p,c1定义为final.我帮你修改好了
import java.awt.*;
import java.awt.event.*;
public class XtLcb extends Frame
{
public static void main(String args[])
{
Frame frame = new Frame();
frame.setBackground(Color.lightGray);
frame.setSize(400,400);
frame.setVisible(true);
final CardLayout cl = new CardLayout();

MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu();
MenuItem menuFileExit = new MenuItem();
MenuItem menuFileOpen = new MenuItem();
MenuItem menuFileOn = new MenuItem();

menuFile.setLabel("File");
menuFileExit.setLabel("Exit");
menuFileOpen.setLabel("Open");
menuFileOn.setLabel("On");

final Panel p = new Panel();
p.setLayout(cl);
Panel p1 = new Panel();
Panel p2 = new Panel();
p1.add(new Label("Open"));
p2.add(new Label("On"));
p.add(p1,"1");
p.add(p2,"2");
frame.add(p);

// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
);
menuFileOpen.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.show(p,"1");
}
}

);
menuFileOn.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cl.show(p,"2");
}
}
);
menuFile.add(menuFileOpen);
menuFile.add(menuFileOn);
menuFile.add(menuFileExit);
menuBar.add(menuFile);

frame.setMenuBar(menuBar);

// Add window listener.
frame.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);

}
}
);
}

/**
* Shutdown procedure when run as an application.
*/
/*void windowClosed()
{

// TODO: Check if it is safe to close the application

// Exit application.
System.exit(0);
}*/
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式