计算一段字符串中字母出现的个数
System.out.println("请输入一段字符串:");
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
// HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
TreeMap<Character, Integer> hm = new TreeMap<Character, Integer>();
for(int i=0;i<str.length();i++){
char key=str.charAt(i);
Integer value = hm.get(key);
if(value==null){
hm.put(key,1);
}else {
value++;
hm.put(key,value);
}
}
StringBuilder sb=new StringBuilder();
Set<Character> keyset = hm.keySet();
for(Character c:keyset){
Integer integer = hm.get(c);
sb.append(c).append("(").append(integer).append(")");
}
sb.toString();
System.out.println(sb);
斗地主洗牌,发牌,看牌的功能实现
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class poker {
public static void main(String[] args) {
String[]colors={"♠","♥","🔶","♣"};
String[]numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
HashMap<Integer, String> hm = new HashMap<Integer, String>();
ArrayList<Integer> array = new ArrayList<>();
int index=0;
for(String number:numbers){
for(String color:colors){
hm.put(index,color+number);
array.add(index);
index++;
}
}
hm.put(index,"小王");
array.add(index);
index++;
hm.put(index,"大王");
array.add(index);
Collections.shuffle(array);
TreeSet<Integer> p1 = new TreeSet<Integer>();
TreeSet<Integer> p2 = new TreeSet<Integer>();
TreeSet<Integer> p3 = new TreeSet<Integer>();
TreeSet<Integer> dp = new TreeSet<Integer>();
for(int i=0;i<array.size();i++){
int x=array.get(i);
if(i>=array.size()-3){
dp.add(x);
}else if(i%3==0){
p1.add(x);
}else if(i%3==1){
p2.add(x);
}else if(i%3==2){
p3.add(x);
}
}
lookPoker("底牌",dp,hm);
lookPoker("啊元",p1,hm);
lookPoker("啊铭",p2,hm);
lookPoker("啊彬",p3,hm);
}
public static void lookPoker(String name,TreeSet<Integer>ts, HashMap<Integer, String> hm){
System.out.print(name+"的扑克牌是:");
for(Integer key:ts){
System.out.print(hm.get(key)+" ");
}
System.out.println();
}
}
线程
线程默认优先级是5;线程优先级的范围是:1-10
方法一:继承Thread类
1.定义一个类MyThread继承Thread类
2.在MyThread类中重写run()方法
3.创建MyThread类的对象
4.启动线程 start();
方法二:实现Runnable接口
1.定义一个类MyRunnable实现Runnable接口
2.在MyRunnable类中重写run()方法
3.创建MyRunnable类的对象
4.创建Thread类的对象,把MyRunnable对象作为构造方法的参数
5.启动线程
同步方法:把synchronized关键字加到方法上
格式:修饰符 synchronized 返回值类型方法名(方法参数){ }
同步方法的锁对象是 this
同步静态方法:把synchronized关键字加到静态方法上
格式:修饰符static synchronized 返回值类型 方法名(方法参数){ }
同步静态方法的锁对象是 类名.class
Lock的使用
Lock lock = new Reentrantlook();
……
try{
lock.lock();
……
}finally{
lock.unlock();
}
监听
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestMonitor {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
Button b1 = new Button("Start");
Button b2 = new Button("Stop");
b2.setActionCommand("结束按钮生效");
MyMonitor mM = new MyMonitor();
b1.addActionListener(mM);
b2.addActionListener(mM);
frame.add(b1,BorderLayout.SOUTH);
frame.add(b2,BorderLayout.NORTH);
frame.pack();
}
}
class MyMonitor implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了"+e.getActionCommand());
}
}
监听窗口
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindowListener {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
WindowFrame(){
setVisible(true);
setBounds(100,100,200,200);
setBackground(Color.pink);
//addWindowListener(new MyWindowListener());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// setVisible(false);
System.out.println("windowClosing");
System.exit(0);
}
@Override
public void windowActivated(WindowEvent e) {
WindowFrame wf = (WindowFrame) e.getSource();
wf.setTitle("窗口已激活");
System.out.println("windowActivated");
}
});
}
}
监听鼠标
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame1("画图");
}
}
class MyFrame1 extends Frame{
ArrayList points;
public MyFrame1(String title){
super(title);
points= new ArrayList<>();
setBounds(200,200,300,400);
setVisible(true);
this.addMouseListener(new MyMouseListener());
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.blue);
g.fillOval(point.x,point.y,10,10);
}
}
// public void addPaint(Point point){
// points.add(point);
// }
private class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame1 mf = (MyFrame1) e.getSource();
// mf.addPaint(new Point(e.getX(),e.getY()));
mf.points.add(new Point(e.getX(),e.getY()));
mf.repaint();
}
}
}
监听文本框
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestTextField {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
MyFrame(){
TextField textField = new TextField();
textField.setEchoChar('*');
add(textField);
MyMonitor1 mM1 = new MyMonitor1();
textField.addActionListener(mM1);
setVisible(true);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
}
}
class MyMonitor1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField tf=(TextField) e.getSource();
System.out.println(tf.getText());
tf.setText("");
}
}
监听键盘
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new MyKeyFrame();
}
}
class MyKeyFrame extends Frame{
MyKeyFrame() {
setBounds(1, 1, 300, 400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if (keyCode == KeyEvent.VK_UP) {
System.out.println("您按下了上键");
}
}
});
}
}
JFrame
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame{
public void init(){
JFrame jf = new JFrame("这是一个JFrame窗口");
setVisible(true);
setBackground(Color.pink);
Container container = this.getContentPane();
container.setBackground(Color.pink);
setBounds(1,1,300,400);
JLabel jlabel = new JLabel("这是一个标签");
jlabel.setForeground(Color.white);
this.add(jlabel);
jlabel.setHorizontalAlignment(SwingConstants.CENTER);
jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
}
弹出一个对话框
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDialog extends JFrame {
TestDialog(){
this.setVisible(true);
this.setSize(300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(null);
JButton button = new JButton("点击弹出一个对话框");
button.setBounds(30,30,200,50);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();
}
});
container.add(button);
}
public static void main(String[] args) {
new TestDialog();
}
}
class MyDialog extends JDialog{
MyDialog(){
setVisible(true);
setBounds(100,100,500,500);
Container container = this.getContentPane();
// container.setLayout(null);
JLabel jLabel = new JLabel("这是一个对话框");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
// jLabel.setBounds(150,150,100,100);
container.add(jLabel);
}
}
Icon
import javax.swing.*;
import java.awt.*;
public class TestIcon extends JFrame implements Icon {
public static void main(String[] args) {
new TestIcon().init();
}
private int width,height;
public TestIcon(){};
public TestIcon(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
TestIcon testIcon = new TestIcon(15, 15);
JLabel label = new JLabel("testIcon", testIcon, SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
this.setVisible(true);
this.setBounds(100,100,200,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
ImageIcon
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
TestImageIcon(){
//获取图片
JLabel label = new JLabel("TestImageIcon");
URL url = TestImageIcon.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = getContentPane();
container.add(label);
setVisible(true);
setBounds(100,100,200,200);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
Panel
import javax.swing.*;
import java.awt.*;
public class TestPanel extends JFrame {
public static void main(String[] args) {
new TestPanel();
}
TestPanel(){
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,2,10,10));
JPanel p1 = new JPanel(new GridLayout(1,3));
p1.add(new JButton("1"));
p1.add(new JButton("1"));
p1.add(new JButton("1"));
JPanel p2 = new JPanel(new GridLayout(2,1));
p2.add(new JButton("2"));
p2.add(new JButton("2"));
JPanel p3 = new JPanel(new GridLayout(1,2));
p3.add(new JButton("3"));
p3.add(new JButton("3"));
JPanel p4 = new JPanel(new GridLayout(3,2));
p4.add(new JButton("4"));
p4.add(new JButton("4"));
p4.add(new JButton("4"));
p4.add(new JButton("4"));
p4.add(new JButton("4"));
p4.add(new JButton("4"));
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
ScrollPanel
import javax.swing.*;
import java.awt.*;
public class TestScrollPanel extends JFrame {
public static void main(String[] args) {
new TestScrollPanel();
}
TestScrollPanel(){
Container container = this.getContentPane();
JTextArea jTextArea = new JTextArea(20,50);
jTextArea.setText("这是一个文本框");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
container.add(jScrollPane);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Button
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public static void main(String[] args) {
new TestJButton();
}
TestJButton(){
Container container = this.getContentPane();
URL url = TestJButton.class.getResource("tx.jpg");
ImageIcon imageIcon = new ImageIcon(url);
JButton jButton = new JButton();
jButton.setIcon(imageIcon);
jButton.setToolTipText("图片按钮");
container.add(jButton);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
RadioButton
import javax.swing.*;
import java.awt.*;
public class TestRadioButton extends JFrame {
public static void main(String[] args) {
new TestRadioButton();
}
TestRadioButton(){
Container container = this.getContentPane();
JRadioButton jRadioButton1 = new JRadioButton("jRadioButton1");
JRadioButton jRadioButton2 = new JRadioButton("jRadioButton2");
JRadioButton jRadioButton3 = new JRadioButton("jRadioButton3");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
container.add(jRadioButton1,BorderLayout.NORTH);
container.add(jRadioButton2,BorderLayout.CENTER);
container.add(jRadioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
CheckBox
import javax.swing.*;
import java.awt.*;
public class TestCheckBox extends JFrame {
public static void main(String[] args) {
new TestCheckBox();
}
TestCheckBox(){
Container container = this.getContentPane();
Checkbox checkBox1 = new Checkbox("checkBox1");
Checkbox checkBox2 = new Checkbox("checkBox2");
container.add(checkBox1,BorderLayout.SOUTH);
container.add(checkBox2,BorderLayout.NORTH);
this.setVisible(true);
this.setBounds(100,100,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
CombBox and ListBox
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
public class TestCombBox extends JFrame {
public static void main(String[] args) {
new TestCombBox();
// new TestListBox();
}
TestCombBox() {
Container container = this.getContentPane();
JComboBox jComboBox = new JComboBox();
jComboBox.addItem(null);
jComboBox.addItem("正在上映");
jComboBox.addItem("已下架");
jComboBox.addItem("即将上映");
jComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(jComboBox.getSelectedIndex());
System.out.println(jComboBox.getSelectedItem());
}
});
container.add(jComboBox);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300, 500);
}
}
class TestListBox extends JFrame{
public static void main(String[] args) {
new TestCombBox();
}
TestListBox(){
Container container = this.getContentPane();
Vector contents = new Vector();
JList jList= new JList(contents);
contents.add("Max");
contents.add("Anthony");
container.add(jList);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setSize(300, 500);
}
}