Java,我想在菜单项里面再添加菜单项怎么设置,就像图里这样带三角号的
3个回答
2015-08-01
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class jiemian extends JFrame
{
private JLabel jl = new JLabel ("第一关");
private Container container = new JPanel ();
private static Thread t;
int time = 0;
public jiemian ()
{
JMenuBar menuBar = new JMenuBar ();
setJMenuBar (menuBar);
JMenu[] menu = new JMenu[] { new JMenu ("游戏"), new JMenu ("帮助") };
JMenuItem[] menuItem =
new JMenuItem[] { new JMenuItem ("新游戏"), new JMenuItem ("重新开始"), new JMenuItem ("记录"),
new JMenuItem ("退出"), new JMenuItem ("操作方法") };
for ( int i = 0; i < 2; i++ )
{
menuBar.add (menu[i]);
}
for ( int i = 0; i < 4; i++ )
{
menu[0].add (menuItem[i]);
}
menu[1].add (menuItem[4]);
menuItem[3].addActionListener (new ActionListener ()
{
public void actionPerformed ( ActionEvent e )
{
System.exit (0);
}
});
t = new Thread (new Runnable ()
{
public void run ()
{
while (time <= 200)
{
if (time % 2 == 0)
{
container.add (jl);
jl.setBounds (200, 200, 80, 80);
}
else
jl.setBounds (0, 0, 0, 0);
try
{
Thread.sleep (1000);
}
catch (Exception e)
{
e.printStackTrace ();
}
time++;
if (time == 200)
{
time = 0;
}
}
}
});
t.start ();
this.setLayout (null);
container.setLayout (null);
container.setSize (500, 500);
container.setBackground (Color.BLUE);
this.add (container);
this.setTitle ("坦克大战");
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setSize (700, 600);
this.setLocationRelativeTo (null);
this.setVisible (true);
}
public static void main ( String[] args )
{
new jiemian ();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询