一
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("动物报名");
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("叫声比赛");
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)
{
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);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/990c2d8cfc76e6d90fb123e241102afa.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b56370249d7ee3c149d05b74f8a81293.png)