java点击不同按钮给不同文本框赋值
这个点击按钮只能给密码框添加值,不能给文本框添加。我的想法是:我鼠标光标在文本框时,我点击按钮文本框添加数字而密码框不添加数字。鼠标光标在密码框时,我点击按钮密码框添加数...
这个点击按钮只能给密码框添加值,不能给文本框添加。我的想法是:我鼠标光标在文本框时,我点击按钮文本框添加数字而密码框不添加数字。鼠标光标在密码框时,我点击按钮密码框添加数字而文本框不添加数字,求一个大神帮忙,给我一个可以实现并且能运行的代码,是代码,不需要纸上谈兵
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
frame.setSize(500,300);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(3,3));
JTextField textField=new JTextField();
JPasswordField passwordField=new JPasswordField();
for(int x=1;x<=9;x++){
JButton button=new JButton(x+"");
panel.add(button);
boolean res=textField.isFocusOwner();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(res==true){
textField.setText(textField.getText()+button.getText());
}
if(res==false){
passwordField.setText(passwordField.getText()+button.getText());
}
}
});
}
frame.add(panel);
panel.add(textField);
panel.add(passwordField);
}
} 展开
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(3);
frame.setVisible(true);
frame.setSize(500,300);
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(3,3));
JTextField textField=new JTextField();
JPasswordField passwordField=new JPasswordField();
for(int x=1;x<=9;x++){
JButton button=new JButton(x+"");
panel.add(button);
boolean res=textField.isFocusOwner();
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(res==true){
textField.setText(textField.getText()+button.getText());
}
if(res==false){
passwordField.setText(passwordField.getText()+button.getText());
}
}
});
}
frame.add(panel);
panel.add(textField);
panel.add(passwordField);
}
} 展开
展开全部
//已经符合你的要求了
//多给点财富吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener {
public static void main(String[] args) {
new test();
}
test() {
JPanel p=new JPanel(),fsp=new JPanel(),bsp=new JPanel();
JTextField tf=new JTextField(10);
JPasswordField pf=new JPasswordField(10);
FocusAdapter focus=new FocusAdapter() {
public void focusGained(FocusEvent e) {
dsttf=(JTextField)e.getSource();
}
};
tf.addFocusListener(focus);
pf.addFocusListener(focus);
fsp.add(tf);
fsp.add(pf);
bsp.setLayout(new GridLayout(3,3));
JButton b=null;
for(int x=1;x<=9;x++) {
b=new JButton(""+x);
b.addActionListener(this);
bsp.add(b);
}
p.setLayout(new GridLayout(2,1));
p.add(fsp);
p.add(bsp);
add(p);
setResizable(false);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(null!=dsttf)
{
if(dsttf instanceof JPasswordField)
{
JPasswordField pw=(JPasswordField)dsttf;
pw.setText(new String(pw.getPassword())+b.getText());
}
else
{
dsttf.setText(dsttf.getText()+b.getText());
}
}
}
JTextField dsttf=null;
}
//多给点财富吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame implements ActionListener {
public static void main(String[] args) {
new test();
}
test() {
JPanel p=new JPanel(),fsp=new JPanel(),bsp=new JPanel();
JTextField tf=new JTextField(10);
JPasswordField pf=new JPasswordField(10);
FocusAdapter focus=new FocusAdapter() {
public void focusGained(FocusEvent e) {
dsttf=(JTextField)e.getSource();
}
};
tf.addFocusListener(focus);
pf.addFocusListener(focus);
fsp.add(tf);
fsp.add(pf);
bsp.setLayout(new GridLayout(3,3));
JButton b=null;
for(int x=1;x<=9;x++) {
b=new JButton(""+x);
b.addActionListener(this);
bsp.add(b);
}
p.setLayout(new GridLayout(2,1));
p.add(fsp);
p.add(bsp);
add(p);
setResizable(false);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton b=(JButton)e.getSource();
if(null!=dsttf)
{
if(dsttf instanceof JPasswordField)
{
JPasswordField pw=(JPasswordField)dsttf;
pw.setText(new String(pw.getPassword())+b.getText());
}
else
{
dsttf.setText(dsttf.getText()+b.getText());
}
}
}
JTextField dsttf=null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询