用JAVA怎么编写小键盘

用JAVA怎么编写小键盘,第1张

刚做了个简单的计算器给你参考一下

小键盘的话就是添加很多按钮

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()

}

}

貌似是KeyListener里面的常量。 比如0-9就是 VK_0,VK-1..........VK_9

上下左右则是 VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT

package test

import java.awt.Button

import java.awt.event.KeyAdapter

import java.awt.event.KeyEvent

import javax.swing.JFrame

public class MyTest extends JFrame{

public void MyTest(){

this.setSize(400,500)

this.setVisible(true)

Button button = new Button("回车")

button.setBounds(20, 20, 80, 20)

this.add(button)

button.addKeyListener(new KeyAdapter(){

public void keyPressed(KeyEvent e) {

if(e.getKeyCode()==KeyEvent.VK_ENTER){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_UP){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_DOWN){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_RIGHT){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_LEFT){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_0){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_0){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_1){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_2){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_3){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_F1){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_F2){

System.out.print(e.getKeyText(e.getKeyCode()))

}

if(e.getKeyCode()==KeyEvent.VK_F3){

System.out.print(e.getKeyText(e.getKeyCode()))

}

}

})

this.setFocusable(true)

}

public static void main(String[] args) {

new MyTest().MyTest()

}

}

望采纳!!!!

// 我们需要给 JTable 指定我们自己定义的 Table Cell Editor.

JTable 工作过程如下:

当一个表格显示之前,JTable 会询问每个单元格,getCellRender().getTableCellRendererComponent() 得到一个 Swing 组件后,就用它来在指定单元格显示出来。

当某个单元格即将获得焦点,比如单击或键盘tab 移动,JTable 会询问是否目标单元格允许编辑,如果允许就会询问 getCellEditor().getTableCellEditorComponent() 得到一个编辑器,通常,默认的编辑器是一个 JTextField 类型的,只要我们给一个 JButton 类型的就可以了。

table.setCellEditor(new TableCellEditor() {

    private JButton editor = new JButton()

    private JTextField dephaut = new JTextField()

    

    {//相当于构造函数。

        editor.addActionListener() {

            /* 业务方法 */

        }        

    }    

    /* 此处省略 N 多待实现方法*/

    public Component getTableCellEditorComponent(

JTable table,

Object value,

boolean isSelected,

int row,

int column) {

if (column == 3) {

            return this.editor

        } else {

            return this.dephaut

        }            

    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11457803.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存