分数计算器-不知道怎么增加MouseClicked

zll198 2011-11-02 10:00:29
下面是我的部分code,我想要textfiled未点击之前是false,如果我用鼠标点哪一个一下它就变成true,可以输入数字,我是初学者,所以麻烦大家给详细一点的指导。先谢谢大家了
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;



public class Calculator extends JFrame implements ActionListener
{
JTextField nominator1 = new JTextField(4);
JTextField nominator2 = new JTextField(4);
JTextField nominator3 = new JTextField(4);
JTextField denominator1 = new JTextField(4);
JTextField denominator2 = new JTextField(4);
JTextField denominator3 = new JTextField(4);
JTextField sign = new JTextField(2);



JButton one = new JButton("1");
JButton two = new JButton("2");
JButton plus = new JButton("+");
JButton less = new JButton("<");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton minus = new JButton("-");
JButton great = new JButton(">");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton multi = new JButton("*");
JButton equal = new JButton("==");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton divi = new JButton("/");
JButton nonequal = new JButton("!=");
JButton nine = new JButton("9");
JButton zero = new JButton("0");
JButton clear = new JButton("CLEAR");
JButton calc = new JButton("CALC");

boolean n1 = false, n2 = false, d1 = false, d2 = false;
int N1 = 0, N2 = 0, D1 = 0, D2 = 0;

public Calculator()
{
nominator3.setEditable(false);
nominator3.setHorizontalAlignment(SwingConstants.RIGHT);
denominator3.setEditable(false);
denominator3.setHorizontalAlignment(SwingConstants.RIGHT);

JPanel p1 = new JPanel(new GridLayout(5, 4, 10, 10));
p1.add(one);
p1.add(two);
p1.add(plus);
p1.add(less);
p1.add(three);
p1.add(four);
p1.add(minus);
p1.add(great);
p1.add(five);
p1.add(six);
p1.add(multi);
p1.add(equal);
p1.add(seven);
p1.add(eight);
p1.add(divi);
p1.add(nonequal);
p1.add(nine);
p1.add(zero);
p1.add(clear);
p1.add(calc);

JPanel p2 = new JPanel(new GridLayout(3, 1, 10, 10));
p2.add(nominator1);
p2.add(new JLabel("____"));
p2.add(denominator1);

JPanel p3 = new JPanel(new GridLayout(3, 1, 10, 10));
p3.add(nominator2);
p3.add(new JLabel("____"));
p3.add(denominator2);

JPanel p4 = new JPanel(new GridLayout(3, 1, 10, 10));
p4.add(nominator3);
p4.add(new JLabel("____"));
p4.add(denominator3);

JPanel p5 = new JPanel(new BorderLayout(10, 10));
p5.add(sign, BorderLayout.WEST);
p5.add(p3, BorderLayout.CENTER);
p5.add(new JButton("="), BorderLayout.EAST);

JPanel p6 = new JPanel(new BorderLayout(10, 10));
p6.setBorder(new TitledBorder("Fraction Calculator"));
p6.add(p2, BorderLayout.WEST);
p6.add(p5, BorderLayout.CENTER);
p6.add(p4, BorderLayout.EAST);

add(p6, BorderLayout.NORTH);
add(p1, BorderLayout.SOUTH);


one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);
plus.addActionListener(this);
less.addActionListener(this);
minus.addActionListener(this);
great.addActionListener(this);
multi.addActionListener(this);
equal.addActionListener(this);
divi.addActionListener(this);
nonequal.addActionListener(this);
clear.addActionListener(this);
calc.addActionListener(this);

addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e)
{

if(e.getSource()== nominator1)
{
N1 = 1;
}
else if(e.getSource()== nominator2)
{
N2 = 1;
}
else if(e.getSource()== denominator1)
{
D1 = 1;
}
else if(e.getSource()== denominator2)
{
D2 = 1;
}
}
});
}


public void actionPerformed(ActionEvent e)
{

if (N1 == 1)
{
n1 = true;
}
else if(N2==1)
{
n2 = true;
}
else if(D1==1)
{
d1 = true;
}
else if(D2==1)
{
d2 = true;
}



if (e.getSource() == one)
{
if (n1)
{
nominator1.setText("1");
}
else if (n2)
{
nominator2.setText("1");
}
else if (d1)
{
denominator1.setText("1");
}
else if (d2)
{
denominator2.setText("1");
}

}
else if (e.getSource() == two)
{
nominator1.setText("2");
}
else if (e.getSource() == three)
{
nominator1.setText("3");
}
else if (e.getSource() == four)
{
nominator1.setText("4");
}
else if (e.getSource() == five)
{
nominator1.setText("5");
}
else if (e.getSource() == six)
{
nominator1.setText("6");
}
else if (e.getSource() == seven)
{
nominator1.setText("7");
}
else if (e.getSource() == eight)
{
nominator1.setText("8");
}
else if (e.getSource() == nine)
{
nominator1.setText("9");
}
else if (e.getSource() == zero)
{
nominator1.setText("0");
}
}

public static void main(String[] args)
{
Calculator frame = new Calculator();
frame.setTitle("Fraction Calculator");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
...全文
82 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zll198 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 healer_kx 的回复:]

加Listener
[/Quote]
我知道要加listener,可是我不会加
healer_kx 2011-11-02
  • 打赏
  • 举报
回复
加Listener
Michaelbest1 2011-11-02
  • 打赏
  • 举报
回复

nominator1.addMouseListener( new MouseAdapter(){
public void mouseClicked(MouseEvent e){
nominator1.setEditable(true);
}
});


这么写就好了。其余的都一样。
这个方法是java.awt.Component里的,这可能是你查API时没有找到的原因。
建议去仔细研究一下Java Swing的结构,对你以后使用Swing组件很有好处。
还有你那一段设置Frame属性的代码最好写在构造函数里,不要在外部去设置。以后你会明白为什么要这样做的。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧