「java键盘编程教学」java操作键盘
今天给各位分享java键盘编程教学的知识,其中也会对java操作键盘进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用JAVA怎么编写小键盘
- 2、Java编程中如何读取键盘输入
- 3、如何用JAVA编程从键盘输入10个整数存入整型数组中,逆序输出这10个数?
- 4、用Java写一个程序,键盘输入5个整数 并按大小的次序输出?
- 5、用java语言编程:从键盘中输入十个无序的数字,从大到小输出。
- 6、JAVA编程 1)键盘输入建立字符串对象(system.in;IO流类;string类)
用JAVA怎么编写小键盘
刚做了个简单的计算器给你参考一下
小键盘的话就是添加很多按钮
JButton jb0=new JButton("0");定义一个显示0的按钮
JPanel jp=new JPanel();定义一个面板,按钮就都放在里面
jp.setLayout(new GridLayout(3,5));设置面板里按钮的布局,这里用了3行5列的网格布局
jp.add(jb11);添加按钮jb11进面板
基本上就是这样了,希望有用
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class cal extends JFrame implements ActionListener
{
int i;
int j;
int z;
String s=new String();
String l=new String();
JLabel jl=new JLabel();
JButton jb0=new JButton("0");
JButton jb1=new JButton("1");
JButton jb2=new JButton("2");
JButton jb3=new JButton("3");
JButton jb4=new JButton("4");
JButton jb5=new JButton("5");
JButton jb6=new JButton("6");
JButton jb7=new JButton("7");
JButton jb8=new JButton("8");
JButton jb9=new JButton("9");
JButton jb10=new JButton("+");
JButton jb11=new JButton("_");
JButton jb12=new JButton("=");
JButton jb13=new JButton("CLS");
JPanel jp=new JPanel();
public cal()
{
this.setTitle("计算器");
this.setLayout(null);
jp.setLayout(new GridLayout(3,5));
this.setBounds(300,200,350,280);
jl.setBounds(5,5,150,50);
jp.setBounds(5,70,300,150);
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jb7);
jp.add(jb8);
jp.add(jb9);
jp.add(jb0);
jp.add(jb10);
jp.add(jb11);
jp.add(jb13);
jp.add(jb12);
this.add(jl);
this.add(jp);
jb0.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb10.addActionListener(this);
jb11.addActionListener(this);
jb12.addActionListener(this);
jb13.addActionListener(this);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb0)
{
s=s+"0";
l=l+"0";
jl.setText(l);
}
else if(e.getSource()==jb1)
{
s=s+"1";
l=l+"1";
jl.setText(l);
}
else if(e.getSource()==jb2)
{
s=s+"2";
l=l+"2";
jl.setText(l);
}
else if(e.getSource()==jb3)
{
s=s+"3";
l=l+"3";
jl.setText(l);
}
else if(e.getSource()==jb4)
{
s=s+"4";
l=l+"4";
jl.setText(l);
}
else if(e.getSource()==jb5)
{
s=s+"5";
l=l+"5";
jl.setText(l);
}
else if(e.getSource()==jb6)
{
s=s+"6";
l=l+"6";
jl.setText(l);
}
else if(e.getSource()==jb7)
{
s=s+"7";
l=l+"7";
jl.setText(l);
}
else if(e.getSource()==jb8)
{
s=s+"8";
l=l+"8";
jl.setText(l);
}
else if(e.getSource()==jb9)
{
s=s+"9";
l=l+"9";
jl.setText(l);
}
else if(e.getSource()==jb10)
{
i=Integer.parseInt(s);
s="";
z=1;
l=l+"+";
jl.setText(l);
}
else if(e.getSource()==jb11)
{
i=Integer.parseInt(s);
s="";
z=2;
l=l+"-";
jl.setText(l);
}
else if(e.getSource()==jb12)
{
j=Integer.parseInt(s);
if(z==1)
{
i=i+j;
}
else if(z==2)
{
i=i-j;
}
l=l+"="+i;
jl.setText(l);
}
else if(e.getSource()==jb13)
{
i=0;
j=0;
z=0;
s="";
l="";
jl.setText("0");
}
}
public static void main(String args[])
{
cal nn=new cal();
}
}
Java编程中如何读取键盘输入
使用System.in.read可以读取键盘的输入,但是一般不会这样去操作,可以使用java.util.Scanner来配合System.in来进行数据的操作,举例如下:
Scanner in=new Scanner(System.in);
String readLine = in.nextLine(); //读取键盘输入的一行(以回车换行为结束输入)
如何用JAVA编程从键盘输入10个整数存入整型数组中,逆序输出这10个数?
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int [10];
for(int i=0;i10;i++){
a[i] = sc.nextInt();
}
for(int j=a.length-1;j=0;j-- ){
System.out.print(a[j]+" ");
}
}
} 这个应该是你要的吧
用Java写一个程序,键盘输入5个整数 并按大小的次序输出?
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入5个整数:");
int[] number=new int[5];
for(int i=0;inumber.length;i++){
number[i]=scanner.nextInt();
}
Arrays.sort(number);
System.out.println("排序后输出:");
for (int num:number) {
System.out.print(num+"\t");
}
}
}
控制台:
请输入5个整数:
43
12
87
11
排序后输出:
0 11 12 43 87
用java语言编程:从键盘中输入十个无序的数字,从大到小输出。
import java.io.BufferedReader;
import java.io.InputStreamReader;public class Demo
{
public static void main(String[]args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入10个数字,每个数字之间用“,”分割:");
String str_numbers = br.readLine();
br.close();
str_numbers = str_numbers.replaceAll("\\s", "");
String [] str = str_numbers.split(",");
double [] num = new double[str.length];
for(int i=0;istr.length;i++)
{
num[i] = Double.parseDouble(str[i]);
}
for(int i=0;inum.length-1;i++)
{
for(int j=i+1;jnum.length;j++)
{
if(num[j]num[i])
{
double tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
}
}
for(int i=0;inum.length;i++)
{
System.out.print(num[i]+"\t");
}
System.out.println();
}
}
JAVA编程 1)键盘输入建立字符串对象(system.in;IO流类;string类)
package package20140910;
import java.io.*;
public class tiwen {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("请输入字符串:");
String s = br.readLine();
System.out.println("该字符串的长度是:"+s.length());
System.out.println("请输入要查找的字符:");
char[] arry=br.readLine().toCharArray();
char c=arry[0];
System.out.println("该字符在字符串中的索引:"+s.indexOf(c));
System.out.println("请输入要查找的字符串:");
String arr=br.readLine();
if(s.contains(arr))
{
System.out.println("原字符串含有此字符串");
}else{
System.out.println("原字符串不含此字符串");
}
br.close();
isr.close();
}
}
java键盘编程教学的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java操作键盘、java键盘编程教学的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。