请高手帮我的代码再添加两个功能!诚心悬赏!

请高手帮我的代码再添加两个功能!诚心悬赏!,第1张

setTex为空就行了,焦点是grabFocus(),帮你加上

import javaxswing;

import javaawt;

import javaawtevent;

public class zml extends javaxswingJFrame {

private JPanel P1;

private JLabel L1;

private JLabel L2;

private JTextField name;

private JPasswordField number;

private JButton Button1;

private JButton Button2;

{

initGUI();

}

public static void main(String[] args) {

test inst = new test();

instsetLocationRelativeTo(null);

instsetVisible(true);

}

private void initGUI() {

try {

setDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);

{

P1 = new JPanel();

getContentPane()add(P1, BorderLayoutCENTER);

P1setLayout(null);

{

L1 = new JLabel();

P1add(L1);

L1setText("姓名");

L1setBounds(45, 30, 75, 25);

}

{

L2 = new JLabel();

P1add(L2);

L2setText("学号");

L2setBounds(45, 75, 55, 15);

}

{

name = new JTextField();

P1add(name);

namesetBounds(100, 30, 140, 25);

}

{

number = new JPasswordField();

P1add(number);

numbersetBounds(100, 70, 140, 25);

}

{

Button1 = new JButton();

P1add(Button1);

Button1setText("Login");

Button1setBounds(160, 110, 80, 30);

Button1addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

if (namegetText()equals("zml")

&& StringvalueOf(numbergetPassword())equals("123")) {

JOptionPane

showMessageDialog(testthis, "登录成功");

} else {

JOptionPaneshowMessageDialog(testthis,

"账号或密码错误");

namesetText(null);

namegrabFocus();

numbersetText(null);

}

}

});

Button2 = new JButton();

P1add(Button2);

Button2setText("Exit");

Button2setBounds(180, 150, 60, 30);

Button2addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

Systemexit(0);

}

});

}

}

setSize(300, 220);

} catch (Exception e) {

eprintStackTrace();

}

}

}

focusListener

用于接收组件上的键盘焦点事件的侦听器接口。对处理焦点事件感兴趣的类要么实现此接口(以及它包含的所有方法),要么扩展抽象

FocusAdapter 类(只重写感兴趣的方法)。然后,根据该类创建的侦听器对象使用组件的

addFocusListener 方法向该组件注册。当组件获得或失去键盘焦点时,可调用侦听器对象中的相关方法,并将

FocusEvent 传递给它。

找的API文档

在Java Swing编程过程中 经常需要处理键盘事件 例如处理快捷键等 这里就介绍如何定义键盘事件 以及如何处理这些事件 在jdk 中 分别针对Jponent和Text类的对象定制了不同的处理键盘事件的方法 在Jponent中 定义了registerKeyboardAction方法 使用这个方法来将需要处理的键盘事件以及处理事件的行为绑定在一起 Text类中具有keymap对象 同Jponent中的处理方法类似 这个对象保存著需要处理的键盘事件和对应的行为

而在jdk 中 使用一种新的方法来处理键盘事件 它将jdk 的两种方法整合在一起 不需要区分被处理的是Jponent还是Text类型的组件 它定义了两个新的类 InputMap和ActionMap 他们均是简单的表或映射 一个InputMap将一个Keystroke对应到一个对象 ActionMap将一个对象对应到一个行为(Action) 通常InputMap中KeyStroke所对应的对象是一个字符串 通过这个字符串可以在ActionMap中查找到相应的行为

InputMap和ActionMap中均有put方法 InputMap的put方法可以将Keystroke对应到一个对象 而ActionMap的put方法可以将一个对象对应到一个行为

在每一个Jponent组件中 会有三个缺省的InputMap和一个缺省的ActionMap 他们可以通过调用getInputMap(int condition)和getActionMap()得到 三个InputMap分别是当组件本身拥有焦点时的InputMap(WHEN_FOCUSED) 当组件的祖先拥有焦点时的InputMap(WHEN_ANCESTOR_OF_FOCUSED_PONENT)和组件所在的窗体具有焦点时的InputMap(WHEN_IN_FOCUSED_WINDOW)(括号内表示为了得到这些InputMap 应该在getInputMap中设置的参数) 以下分别说明这三种InputMap

组件本身拥有焦点时的InputMap 当组件拥有焦点时 键盘按键按下 则java在这个InputMap中查找键盘事件所对应的KeyStroke对象

组件的祖先拥有焦点时的InputMap 当组件的祖先拥有焦点时 键盘按键按下 则java查找这个InputMap

组件所在的窗口拥有焦点时的InputMap 当组件所在的窗口具有焦点时 键盘按键按下 则java查找这个InputMap

当一个键被按下 这个事件被转化成一个KeyStroke对象 java会查找这个Jponent的相应InputMap(例如 当组件的祖先具有焦点时 java就查找这个Jponent的祖先拥有焦点的InputMap)中是否有这个KeyStroke 如果有 取出它所对应的对象(通常是字符串) 利用这个对象在这个Jponent的ActionMap中查找 如果找到对应的行为(Action) 则java执行这个行为的actionPerformed方法(随后介绍这个方法) 从而达到处理键盘事件的目的

每一个InputMap可以具有parent属性 这个属性的值是一个InputMap 当在一个InputMap中查找不到键盘事件的KeyStroke时 java会自动在它的parent属性指定的InputMap中查找 依次向上查找 直至找到 使用parent的好处是 当有一些固定的 不希望用户进行改动的键盘映射可以存放在parent属性所指定的InputMap中 从而避免被意外修改;另外可以将多个Jponent的缺省InputMap设置具有相同的parent 使得可以共享一些键盘绑定的设置 可以通过InputMap类的setparent()方法设置它的parent属性 ActionMap也具有相同的parent属性 使用方法也相同

以上是如何将一个键盘事件对应到一个行为 以下就简单介绍行为(Action)

行为是一个实现了Action接口的类 在Action接口中定义了 个方法 其中最关键的是actionPerformed()方法 这个方法描述了这个行为的具体 *** 作过程 其他几个方法包括setEnabled isEnabled putValue getValue addPropertyChangeListener 和removePropertyChangeListener方法 他们分别用来设置行为是否可用 判断行为可用的状态 设置和取得行为的一些属性 最后两个方法用来允许其他对象在行动对象的属性发生变化后得到通知

通常我们使用一个实现了Action接口的大部分方法的抽象类AbstractAction类作为基类 重载actionPerformed方法以实现我们的行为

我们用一个例子来具体说明如何进行实际的 *** 作

首先编写一个具体的行为 对指定的键盘事件进行处理

public class TextAction extends AbstractAction{private String a;public TextAction(String a){ this a = a; }public void actionPerformed(ActionEvent parm ){String b = parm getActionCommand(); //得到行为的命令字符串

System out println( mand= +b);System out println( prompt= +this a);}}

建立四个TextAction对象

TextAction whenFocusSon = new TextAction( focus son );TextAction whenFocusFather = new TextAction( focus father );TextAction window = new TextAction( window );TextAction ancestor = new TextAction( ancestor );

随后 在一个窗体中加入两个面板 名为sonPanel和parentPanel 使得parentPanel是sonPanel的祖先 并在sonPanel中加入一个名为son的button 在parentPanel中加入名为parent的button 在fatherPanel外加入几个button

得到son组件的三个InputMap 并创建一个名为focusFatherIm的InputMap 使得这个InputMap成为focusIm的parent

//get default inputMap (when focus inputmap) and set a parent InputMap

focusIm = son getInputMap();focusFatherIm = new InputMap();focusIm setParent(focusFatherIm);//get WHEN_ANCESTOR_OF_FOCUSED_PONENT inputMap

ancestorIm = son getInputMap(WHEN_ANCESTOR_OF_FOCUSED_PONENT);//get WHEN_IN_FOCUSED_WINDOW inputMap

windowIm = son getInputMap(WHEN_IN_FOCUSED_WINDOW);//在这些InputMap中分别加入键盘绑定

focusIm put(KeyStroke getKeyStroke( f ) actionFocusSon );focusFatherIm put(KeyStroke getKeyStroke( F ) actionFocusFather );ancestorIm put(KeyStroke getKeyStroke( a ) actionAncestor );windowIm put(KeyStroke getKeyStroke( w ) actionWindow );//得到son组件的缺省的ActionMap 并将已经建立的行为与特定的对象(字符串)进行绑定

am = son getActionMap();am put( actionFocusSon whenFocusSon);am put( actionFocusFather whenFocusFather);am put( actionAncestor ancestor);am put( actionWindow window);

运行程序及其相应结果

单击son按钮 这时如果按下 f F a w 程序均会有相应的输出 这是因为 此时的焦点在son按钮上 而son按钮组件的三个InputMap都是有效的 所以他们对应的事件都会发生

单击parent按钮 这时按下 w 程序会有相应的输出 而按下 f F a 程序没有反应 这是因为parent按钮具有焦点 这个按钮不是son按钮的祖先 而son所在的窗口具有焦点 所以只有组件所在窗口具有焦点的InputMap是有效的

单击其他的按钮(parentPanel外的按钮) 这时按下 w 程序会有相应的输出 而按下 f F a 程序没有反应 这是因为这些按钮具有焦点 他们不是son按钮的祖先 而son所在的窗口具有焦点 所以只有组件所在窗口具有焦点的InputMap是有效的

附 主要程序代码

import java awt ;import javax swing ;import borland jbcl layout ;import java awt event ActionEvent;import java awt event ActionListener;import sun java swing plaf motif ;public class EventPanel extends JPanel implements ActionListener{JButton btnYellow = new JButton();JButton btnBlue = new JButton();JButton btnRed = new JButton();JPanel parentPanel = new JPanel();JPanel sonPanel = new JPanel();XYLayout xYLayout = new XYLayout();JButton son = new JButton();JButton parent = new JButton();public EventPanel(){try{jbInit();}catch(Exception ex){ ex printStackTrace(); }}void jbInit() throws Exception{btnYellow setText( Yellow );btnYellow setBounds(new Rectangle( ));this setLayout(null);btnBlue setBounds(new Rectangle( ));btnBlue setText( Blue );btnRed setBounds(new Rectangle( ));btnRed setText( Red );parentPanel setBorder(BorderFactory createRaisedBevelBorder());parentPanel setBounds(new Rectangle( ));parentPanel setLayout(xYLayout );sonPanel setBorder(BorderFactory createLoweredBevelBorder());son setText( son );parent setText( parent );this add(btnYellow null);this add(btnBlue null);this add(btnRed null);this add(parentPanel null);parentPanel add(sonPanel new XYConstraints( ));sonPanel add(son null);parentPanel add(parent new XYConstraints( ));btnYellow addActionListener(this);btnRed addActionListener(this);btnBlue addActionListener(this);InputMap focusIm focusFatherIm ancestorIm windowIm;ActionMap am;//create four TextAction for diff purpose

TextAction whenFocusSon = new TextAction( focus son );TextAction whenFocusFather = new TextAction( focus father );TextAction window = new TextAction( window );TextAction ancestor = new TextAction( ancestor );//get default inputMap (when focus inputmap) and set a parent InputMap

focusIm = son getInputMap();focusFatherIm = new InputMap();focusIm setParent(focusFatherIm);//get WHEN_ANCESTOR_OF_FOCUSED_PONENT inputMap

ancestorIm = son getInputMap(WHEN_ANCESTOR_OF_FOCUSED_PONENT);//get WHEN_IN_FOCUSED_WINDOW inputMap

windowIm = son getInputMap(WHEN_IN_FOCUSED_WINDOW);//put the keyStroke to the InputMap

focusIm put(KeyStroke getKeyStroke( f ) actionFocusSon );focusFatherIm put(KeyStroke getKeyStroke( F ) actionFocusFather );ancestorIm put(KeyStroke getKeyStroke( a ) actionAncestor );windowIm put(KeyStroke getKeyStroke( w ) actionWindow );//get the actionMap

am = son getActionMap();am put( actionFocusSon whenFocusSon);am put( actionFocusFather whenFocusFather);am put( actionAncestor ancestor);am put( actionWindow window);}public void actionPerformed(ActionEvent e){//this code is used to change the backgracolor

Object source=e getSource();Color color=null;//=getBackground();

lishixinzhi/Article/program/Java/hx/201311/26711

Swing JTable 有一个东西叫 Table Model,它在编辑和显示时是这样工作:

1,当表格要显示前,它会依次询问每一行每一列,你的 getCellRenderer 是什么,对于一般的字符串,我们回答 JLabel,这也是默认的。我们可以提供自已的 TableCellRenderer 来告诉 JTable 这个格子显示成 Checkbox 或下拉框。

2,当我们单击某个格子或键盘移动焦点到某个格子,JTable 会询问,这个格子 (x, y) 是否支持编辑?我们需要在 getTableCellEditor()isCellEditable() 中回答是或否,当否时,JTable 什么也不做,当回答是时,JTable 会接着询问,getTableCellEditor()getTableCellEditorComponent() 是什么,默认时它是一个 JTextField,当然我们可以回答它是一个 JCheckBox 或 JComboBox 来告诉 JTable 我们有一个自定义的编辑器。

在准备显示值时都会从 Table Model 中把值交给当前的 Cell Renderer 或 Cell Editor,当用户按确定或鼠标移走导致焦点丢失时需要验证这个正在编辑的值是否正确合法(我们基于业务规则来验证,比如是否是个数字)并在正确时 commit 回 Table Mode (commit 过程本身我们不需要 *** 作,只需要告诉 JTable true 或 false),验证失败时直接丢弃这个编辑值。

以上就是关于请高手帮我的代码再添加两个功能!诚心悬赏!全部的内容,包括:请高手帮我的代码再添加两个功能!诚心悬赏!、java的swing....TextField jt2=new JTextField("请单击我", 10); // 焦点是什么起什么作用、解析Swing中的键盘事件处理等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/web/9626994.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存