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);
}*/
} 展开
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);
}*/
} 展开
1个回答
展开全部
你这个程序就是卡片布局了,有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);
}*/
}
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);
}*/
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |