码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA程序测试之Swing编程

时间:2015-03-21 12:31:30      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

package swingtest;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;




public class HelloApp extends JFrame{
    
    public static void main(String [] args)
    {
        
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                HelloApp inst = new HelloApp();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        }
                );
        

    }
    public HelloApp()
    {
        super();
        initGUI();
    }
    

    private void initGUI()
    {
        try
        {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            JMenuBar mbar = new JMenuBar();
            setJMenuBar(mbar);
            JMenu mfile = new JMenu();
            mbar.add(mfile);
            mfile.setText("File");
            JMenuItem showHello = new JMenuItem();
            mfile.add(showHello);
            showHello.setText("Hello");
            
            showHello.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    JOptionPane.showMessageDialog(null,"Hello World","This is a message dialog",JOptionPane.DEFAULT_OPTION);
                }
            }
            );
            
            JMenuItem exitem = new JMenuItem();
            mfile.add(exitem);
            exitem.setText("Exit");
            
            pack();
            setSize(400,300);
            
            JToolBar toolBar = new JToolBar();
            getContentPane().add(toolBar,BorderLayout.SOUTH);
            JButton bInBar = new JButton();
            toolBar.add(bInBar);
            bInBar.setText("Say Hello");
            
            JTextField textf = new JTextField();
            getContentPane().add(textf,BorderLayout.NORTH);
            textf.setText("Please input the text");
            
        
            
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    

}

 

JAVA程序测试之Swing编程

标签:

原文地址:http://www.cnblogs.com/ghmgm/p/4355287.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!