swing容器的使用

目录

package first;

public abstract class Animal 
{
	private String name;
	public Animal(){}
	public Animal(String name)
	{
		this.name=name;
	}
	public String Shout()
	{
		String result="";
		for(int i=0;i<10;i++)
		{
			result+=getSound()+"~";
			
		}
		return "我的名字是:"+name+" "+result;
	}
	public abstract String getSound();
}

package first;

public class Dog extends Animal 
{
	
	public Dog(String name)
	{
		super(name);
	}
	@Override
	public String getSound() {
		
		return "汪";
	}
}

package first;

import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class MainTest 
{


	public static void main(String[] args) 
	{

	   new Testfrm().setVisible(true);
	}
}

package first;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class Testfrm extends JFrame
{
	
	 public Testfrm() 
	 {
		 
		 
		 
		 
		 
		 	Animal a[]= new Animal[5];
		 
	        this.setTitle("动物比赛大会");
	        this.setSize(300, 200);
	        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        this.setLocationRelativeTo(null);
	        this.setLayout(new FlowLayout());
	         
	        JButton button1 = new JButton("猫叫");
	         
	        // 传统按钮监听器添加方式
	        button1.addActionListener(new ActionListener()
	        {
	 
	            @Override
	            public void actionPerformed(ActionEvent arg0)
	            {
	                JOptionPane.showMessageDialog(null, "喵");
	            }
	        });
	        this.add(button1);
	        
	        
	        
	        JButton button2 = new JButton("狗叫");
	        button2.addActionListener(new ActionListener()
	        {
	 
	            @Override
	            public void actionPerformed(ActionEvent arg0)
	            {
	                JOptionPane.showMessageDialog(null, "汪");
	            }
	        });
	        this.add(button2);
	        
	        
	        JButton button3 = new JButton("动物报名");
	        // 通过函数接口方式添加按钮监听器,Java8 及以后版本支持
	        button3.addActionListener(e -> 
	        {
	        	a[0]=new Cat("小猫");
	        	a[1]=new Dog("小狗");
	        	a[2]=new Cattle("小牛");
	        	a[3]=new Sheep("小羊");
	            JOptionPane.showMessageDialog(null, "报名成功");
	        });
	        this.add(button3);
	        
	        
	        
	        
	        JButton button4 = new JButton("叫声比赛");
	        // 通过函数接口方式添加按钮监听器,Java8 及以后版本支持
	        button4.addActionListener(e -> 
	        {
	        	if(a[0]!=null)
	            {
	        		for(int i=0;i<4;i++)
	        		JOptionPane.showMessageDialog(null, a[i].Shout());
	            }
	        	else{
	        		JOptionPane.showMessageDialog(null,"还没报名呢!");
	        	}
	        });
	        this.add(button4);
	    }
}

package HH;

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

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Testfrm extends JFrame 
{
	public Testfrm() {
	 this.setTitle("动物比赛大会");
     this.setSize(300, 200);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setLocationRelativeTo(null);
     this.setLayout(new FlowLayout());
     JButton button1 = new JButton("显示");
     
  
     
     JComboBox<String> c1 = new JComboBox<String>();
     	c1.addItem("-你的最爱-");
		c1.addItem("英雄联盟");
		c1.addItem("守望先锋");
		c1.addItem("穿越火线");
		c1.addItem("绝地求生");
		this.add(c1);
		   // 传统按钮监听器添加方式
	     button1.addActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent arg0)
	         {
	             JOptionPane.showMessageDialog(null,c1.getSelectedItem().toString());
	         }
	     });
	     this.add(button1);
	     
	
//		String city = cmb.getSelectedItem().toString();
//		System.out.println(city);
	}
}

**package HH;

public class notGood {

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		 new Testfrm().setVisible(true);
		 
	}

}
**

三.

    JTextField textField = new JTextField(16);
     this.add(textField);
     JButton button1 = new JButton("确定");
     button1.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent arg0)
         {
        	 String str = textField.getText();//获取输入内容
     		//判断是否输入了
     		if(str.equals(""))
     		{
     			Object[] options = { "OK ", "CANCEL " }; 
     			JOptionPane.showOptionDialog(null, "您还没有输入 ", "提示", JOptionPane.DEFAULT_OPTION, 
     			JOptionPane.WARNING_MESSAGE,null, options, options[0]);
     		}
     		else
     			JOptionPane.showMessageDialog(null,"您输入了:"+str);
         }
     });
     this.add(button1);
     
     
     

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值