51

package com.haizhitao.swing;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class FrameDemo
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Frame Demo");
		
		JLabel label = new JLabel("click me");
		
		frame.getContentPane().add(label, BorderLayout.CENTER);
		frame.addWindowListener(new MyHandler());
		
		frame.pack();
		frame.setVisible(true);
	}
}

class MyHandler extends WindowAdapter
{
	@Override
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}


package com.haizhitao.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;

public class ToplevelDemo
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("ToplevelDemo");
		
		JLabel label = new JLabel("welcome");
		
		label.setBackground(Color.BLACK);
		label.setPreferredSize(new Dimension(200, 180));
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(Color.cyan);
		menuBar.setPreferredSize(new Dimension(200, 20));
		
		frame.addWindowListener(new MyHandler1());
		frame.getContentPane().add(label, BorderLayout.CENTER);
		frame.setJMenuBar(menuBar);
		
		frame.pack();
		frame.setVisible(true);
	}
}

class MyHandler1 extends WindowAdapter
{
	@Override
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}



package com.haizhitao.swing;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class MenuTest
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Menu");
		
		JMenuBar menuBar = new JMenuBar();
		
		JMenu menu1 = new JMenu(" file");
		JMenu menu2 = new JMenu(" load");
		JMenu menu3 = new JMenu(" save");
		JMenu menu4 = new JMenu(" quit");
		
		JMenuItem item1 = new JMenuItem("hello");
		JMenuItem item2 = new JMenuItem("world");
		JMenuItem item3 = new JMenuItem("welcome");
		JMenuItem item4 = new JMenuItem("haitao");
		
		frame.setJMenuBar(menuBar);
		
		menuBar.add(menu1);
		menuBar.add(menu2);
		menuBar.add(menu3);
		menuBar.add(menu4);
		
		menu1.add(item1);
		menu1.add(item2);
		menu1.add(item3);
		menu1.add(item4);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(new Dimension(300, 200));
		frame.setVisible(true);
	}
}

阅读更多
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭