主题:急需高手们的指点与帮助!!!
急需各位高手帮小妹检查调试一下我的程序到底哪里出错啦,我调试了整整一个下午也没有找出来.就一个地方出错.
以下是题目的要求: 在一个主窗体中加2个菜单,其中,菜单中至少包含一个子菜单,并有3-5个菜单项,实现1-2个菜单项的功能--菜单项打开一个窗体,窗体包含2个以上中间容器,按照一定的布局方式布局。
我写的代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyWindow extends JFrame implements ActionListener
{
private sonWindow win= new sonWindow();
class sonWindow extends JFrame
{
Box baseBox,boxV1,boxV2;
sonWindow()
{
super("this is an gridLayout example with JPane!");
Container con=this.getContentPane();
boxV1=Box.createVerticalBox();
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入您的姓名"));
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入email"));
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入您的职业"));
boxV2=Box.createVerticalBox();
boxV2.add(Box.createVerticalStrut(18));
boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(16));
JPanel pan1= new JPanel();
JPanel pan2= new JPanel();
pan1.setLayout(new GridLayout(2,1));
pan2.setLayout(new GridLayout(2,1));
pan1.add(boxV1);
pan2.add(boxV2);
con.setLayout(new GridLayout(1,2));
con.add(pan1);
con.add(pan2);
con.validate();
setBounds(120,125,400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("文件");
JMenuItem item1= new JMenuItem("窗体");
JMenuItem item2= new JMenuItem("保存");
JMenuItem item3= new JMenuItem("打印");
JMenu submenu=new JMenu("submenu");
JMenuItem subitem1= new JMenuItem("this is subitem1");
JMenuItem subitem2= new JMenuItem("this is subitem2");
JMenu menu2=new JMenu("编辑");
JMenuItem item4= new JMenuItem("复制");
JMenuItem item5= new JMenuItem("粘贴");
MyWindow(String s)
{
super(s);
submenu.add(subitem1);
submenu.add(subitem2);
menu1.add(submenu);
menu2.add(item4);
submenu.add(item5);
menu1.add(submenu);
menu1.add(item1);
menu1.add(item2);
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
item1.addActionListener(this);
this.setSize(600,400);
this.setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item1)
{
win.setVisible(true);
}
}
public static void main(String[]args)
{
new myWindow();
}
}
class sonWindow extends JFrame
{
public sonWindow()
{
this.setSize(300,200);
}
}
以下是题目的要求: 在一个主窗体中加2个菜单,其中,菜单中至少包含一个子菜单,并有3-5个菜单项,实现1-2个菜单项的功能--菜单项打开一个窗体,窗体包含2个以上中间容器,按照一定的布局方式布局。
我写的代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyWindow extends JFrame implements ActionListener
{
private sonWindow win= new sonWindow();
class sonWindow extends JFrame
{
Box baseBox,boxV1,boxV2;
sonWindow()
{
super("this is an gridLayout example with JPane!");
Container con=this.getContentPane();
boxV1=Box.createVerticalBox();
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入您的姓名"));
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入email"));
boxV1.add(Box.createVerticalStrut(38));
boxV1.add(new JLabel("输入您的职业"));
boxV2=Box.createVerticalBox();
boxV2.add(Box.createVerticalStrut(18));
boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(16));
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(16));
JPanel pan1= new JPanel();
JPanel pan2= new JPanel();
pan1.setLayout(new GridLayout(2,1));
pan2.setLayout(new GridLayout(2,1));
pan1.add(boxV1);
pan2.add(boxV2);
con.setLayout(new GridLayout(1,2));
con.add(pan1);
con.add(pan2);
con.validate();
setBounds(120,125,400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JMenuBar menubar=new JMenuBar();
JMenu menu1=new JMenu("文件");
JMenuItem item1= new JMenuItem("窗体");
JMenuItem item2= new JMenuItem("保存");
JMenuItem item3= new JMenuItem("打印");
JMenu submenu=new JMenu("submenu");
JMenuItem subitem1= new JMenuItem("this is subitem1");
JMenuItem subitem2= new JMenuItem("this is subitem2");
JMenu menu2=new JMenu("编辑");
JMenuItem item4= new JMenuItem("复制");
JMenuItem item5= new JMenuItem("粘贴");
MyWindow(String s)
{
super(s);
submenu.add(subitem1);
submenu.add(subitem2);
menu1.add(submenu);
menu2.add(item4);
submenu.add(item5);
menu1.add(submenu);
menu1.add(item1);
menu1.add(item2);
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
item1.addActionListener(this);
this.setSize(600,400);
this.setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item1)
{
win.setVisible(true);
}
}
public static void main(String[]args)
{
new myWindow();
}
}
class sonWindow extends JFrame
{
public sonWindow()
{
this.setSize(300,200);
}
}