Java图形界面编程(一)

本文详细介绍了AWT(抽象窗口工具包)和Swing在Java图形界面编程中的区别与联系,包括它们的工作原理、优势与局限性,以及如何在实际项目中灵活运用。

初步认识swing  awt  


AWT(abstract Window Toolkit)是java发布的第一个图形界面库。AWT通过调用系统本地的库,来创建和显示控件。

AWT 的组件都是使用的native code来编写的。所以AWT 的组件扩展性不够好。现在人们更愿意使用swing。

同时AWT也提供界面的事件处理机制。



Swing。Swing 的出现并不是为了取缔AWT,因为他本身也依赖AWT。同时,Swing组件是通过java2D绘制的,所以界面较AWT更加美观, 但是依然很丑(┬_┬) 。然而,Swing组件绝大多数都是用java编写的

(除了JFrame,JDialog,JWindow,JApplet),这就意味着,我们可以根据自己的需要,来扩展他们,具体做法主要是以他们为父类,派生出自己满意的控件类型。


下面是AWT 和Swing 的工作关系图。



 可以看出,Swing依赖AWT 和 Java2D绘图。

下面来感受一下。

public class demo
{
	
	public static void main(String[] args) 
	{
		
		/*****************顶层容器:窗体*********************/
		JFrame win = new JFrame();
		win.setTitle("测试");        //窗体的标题
		win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		
		/***************2个按钮******************/
		Button AWTbutton = new Button("AWT Button");
		AWTbutton.setPreferredSize(new Dimension(130,50));
		JButton Swingbutton = new JButton("Swing Button");
		Swingbutton.setPreferredSize(new Dimension(130,50));
		
		/*************主面板***************/
		JPanel panel = new JPanel();
		panel.setBackground(Color.lightGray);
		panel.setPreferredSize(new Dimension(300,240));
		panel.add(Swingbutton);     //将按钮添加到主面板
		panel.add(AWTbutton);        //将按钮添加到主面板
		

		win.getContentPane().add(panel);   //将主面板添加到窗体的内容窗格
		win.pack();                                  
		win.setVisible(true);                       
		
			
	}
	
	
	
}
                                                                                     

                                                                              



java写GUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值