Java 在Swing中的MouseeEvent上获取值
问这个问题可能很愚蠢。。但我认为 通常我们为名为的应用程序编写java代码;我们可以在网上找到梅。但这一个可能是另一个。 我可以向你保证: 我想的是将outputtext字段设置为可编辑字段 现在我所说的输出文本字段是可编辑的。 我们输入一个数字,后跟一个运算符(*、+、-、/),然后输入一个数字,然后继续。当我们单击“=”时会得到一个结果。 这里假设输入的任何数字都不正确。这就像我应该输入5,但我输入了3,因为我的最终结果不正确,所以我点击3,得到一个弹出窗口,即MouseEvent中的popupTrigger,编辑3到5,一旦点击弹出窗口中的“确定”,就会重新计算Java 在Swing中的MouseeEvent上获取值,java,swing,jframe,awt,Java,Swing,Jframe,Awt,问这个问题可能很愚蠢。。但我认为 通常我们为名为的应用程序编写java代码;我们可以在网上找到梅。但这一个可能是另一个。 我可以向你保证: 我想的是将outputtext字段设置为可编辑字段 现在我所说的输出文本字段是可编辑的。 我们输入一个数字,后跟一个运算符(*、+、-、/),然后输入一个数字,然后继续。当我们单击“=”时会得到一个结果。 这里假设输入的任何数字都不正确。这就像我应该输入5,但我输入了3,因为我的最终结果不正确,所以我点击3,得到一个弹出窗口,即MouseEvent中的pop
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.util.List;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Calculator extends JFrame implements ActionListener {
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
JButton btnC = new JButton("C");
JButton btnP = new JButton(".");
JButton opPlus = new JButton("+");
JButton opMult = new JButton("x");
JButton opDivi = new JButton("/");
JButton opMinu = new JButton("-");
JButton Equal = new JButton("=");
JTextField txt = new JTextField();
JTextField txt2 = new JTextField();
JTextField txtotp = new JTextField();
List<String> enterdVal = new ArrayList<String>();
JButton btn = new JButton("random");
double fnum,snum,total;
String op = null;
private final JTextField textField = new JTextField();
public Calculator() {
Container c = getContentPane();
c.setLayout(null);
c.setBackground(Color.MAGENTA);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btn0.addActionListener(this);
btnC.addActionListener(this);
btnP.addActionListener(this);
opPlus.addActionListener(this);
opMult.addActionListener(this);
opDivi.addActionListener(this);
opMinu.addActionListener(this);
Equal.addActionListener(this);
txt.setBounds(35,15,360,50);
txt2.setBounds(0,330,100,20);
txtotp.setBounds(59,30,270,20);
btn7.setBounds(35,85,50,50);
btn8.setBounds(95,85,50,50);
btn9.setBounds(155,85,50,50);
btn4.setBounds(35,145,50,50);
btn5.setBounds(95,145,50,50);
btn6.setBounds(155,145,50,50);
btn1.setBounds(35,205,50,50);
btn2.setBounds(95,205,50,50);
btn3.setBounds(155,205,50,50);
btnP.setBounds(95,265,50,50);
btn0.setBounds(35,265,50,50);
btnC.setBounds(340,85,50,50);
opPlus.setBounds(280,145,50,50);
opMult.setBounds(340,145,50,50);
opMinu.setBounds(280,205,50,50);
opDivi.setBounds(340,205,50,50);
Equal.setBounds(280,265,110,50);
txt.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
System.out.println("me==="+me.getSource());
ActionEvent ae = new ActionEvent(me.getSource(), me.getID(), me.paramString());
System.out.println("ae==="+ae.getSource());
}
});
Font font = new Font("Times New Roman",Font.BOLD,20);
textField.setHorizontalAlignment(SwingConstants.RIGHT);
textField.setFont(new Font("Times New Roman", Font.BOLD, 20));
textField.setEditable(false);
textField.setBounds(35, 317, 360, 30);
getContentPane().add(textField);
txt.setFont(font);
txt.setHorizontalAlignment(JTextField.RIGHT);
c.add(txt);
c.add(btn7);
c.add(btn1);
c.add(btn2);
c.add(btn3);
c.add(btn4);
c.add(btn5);
c.add(btn6);
c.add(btn8);
c.add(btn9);
c.add(btn0);
c.add(btnC);
c.add(opPlus);
c.add(opMult);
c.add(opDivi);
c.add(opMinu);
c.add(Equal);
c.add(btnP);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
setSize(440,385);
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
setLocation(x, y);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String input = txt.getText();
// MouseEvent me = null ;
// ActionEvent ae = new ActionEvent(me.getSource(), me.getID(), me.paramString());
/* if(e.getSource()==Equal ){
System.out.println("Welcome to hell");
}
else if(e.getSource()==btnC)
{
System.out.println("Welcome to hell you should empty list");
}
else{
if(e.getSource()==btn1){
txt.setText(input + "1");
System.out.println(input);
}
else if(e.getSource()==btn2){
txt.setText(input + "2");
}
else if(e.getSource()==btn3){
txt.setText(input + "3");
}
else if(e.getSource()==btn4){
txt.setText(input + "4");
}
else if(e.getSource()==btn5){
txt.setText(input + "5");
}
else if(e.getSource()==btn6){
txt.setText(input + "6");
}
else if(e.getSource()==btn7){
txt.setText(input + "7");
}
else if(e.getSource()==btn8){
txt.setText(input + "8");
}
else if(e.getSource()==btn9){
txt.setText(input + "9");
}
else if(e.getSource()==btn0){
txt.setText(input + "0");
}
else if(e.getSource()==btnP){
txt.setText(input + ".");
}
else if(e.getSource()==opPlus){
fnum = Double.parseDouble(txt.getText());
op = "+";
txt.setText("");
}
else if(e.getSource() == opMinu){
fnum = Double.parseDouble(txt.getText());
op = "-";
txt.setText("");
}
else if(e.getSource() == opMult){
fnum = Double.parseDouble(txt.getText());
op = "x";
txt.setText("");
}
else if(e.getSource() == opDivi){
fnum = Double.parseDouble(txt.getText());
op = "/";
txt.setText("");
}
}
*/
if(e.getSource()==btnC){
txt.setText("");
}
else if(e.getSource()==btn1){
txt.setText(input + "1");
System.out.println(input);
}
else if(e.getSource()==btn2){
txt.setText(input + "2");
}
else if(e.getSource()==btn3){
txt.setText(input + "3");
}
else if(e.getSource()==btn4){
txt.setText(input + "4");
}
else if(e.getSource()==btn5){
txt.setText(input + "5");
}
else if(e.getSource()==btn6){
txt.setText(input + "6");
}
else if(e.getSource()==btn7){
txt.setText(input + "7");
}
else if(e.getSource()==btn8){
txt.setText(input + "8");
}
else if(e.getSource()==btn9){
txt.setText(input + "9");
}
else if(e.getSource()==btn0){
txt.setText(input + "0");
}
else if(e.getSource()==btnP){
txt.setText(input + ".");
}
else if(e.getSource()==opPlus){
fnum = Double.parseDouble(txt.getText());
op = "+";
enterdVal.add(txt.getText());
enterdVal.add(op);
txt.setText("");
}
else if(e.getSource() == opMinu){
fnum = Double.parseDouble(txt.getText());
op = "-";
enterdVal.add(txt.getText());
enterdVal.add(op);
txt.setText("");
}
else if(e.getSource() == opMult){
fnum = Double.parseDouble(txt.getText());
op = "x";
enterdVal.add(txt.getText());
enterdVal.add(op);
txt.setText("");
}
else if(e.getSource() == opDivi){
fnum = Double.parseDouble(txt.getText());
op = "/";
enterdVal.add(txt.getText());
enterdVal.add(op);
txt.setText("");
}
else if(e.getSource()==Equal){
snum = Double.parseDouble(txt.getText());
enterdVal.add(txt.getText());
if(op.equals("+"))
{
total=fnum+snum; }
else if(op.equals("x")){
total = fnum*snum;
}
else if(op.equals("/")){
total = fnum/snum;
}
else if(op.equals("-"))
{ total = fnum-snum;
}
textField.setText(""+total);
txt.setText(""+enterdVal.toString());
}
}
public static void main(String[] args) {
Calculator Cal = new Calculator();
}
}
当它按原样编码时,这将是不必要的困难。看看Windows计算器。在这种情况下,当您输入计算时,它会在您输入时打印整个计算。然后,当用户点击“=”时,它运行一个循环,进行实际计算。忘了把它全部放进一个数组“在路上”。只需继续在calculation textfield中添加文本,直到用户点击“=” 然后,您可以将计算文本保留在那里,并将结果保存在单独的文本字段中(就像您现在所做的那样)。然后,您只需编辑带有计算的文本字段,然后再次点击“=”,您将得到一个新的结果
可能需要重新编写一点,但对于您的目的来说,这将是一个更漂亮、更有条理的解决方案。请发布您尝试过的内容,以及为什么它不能产生预期的结果。这会让你问的问题更清楚。你如何设想“点击3”?你打算把你的文字转换成一系列的按钮吗?@PM77-1这就是我无法得到的东西,如何继续下去。我本想继续使用MouseeEvent,但没有产生任何效果。如果我将所有文本转换为按钮数组,那么如何从中获取值。@jzd我已经发布了代码,但鼠标事件部分有注释。@PM77-1如果我将其转换为按钮数组,我认为它也不会起作用,因为在输出文本字段中只能显示文本。
me===javax.swing.JTextField[,35,15,360x50,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@911f71,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=RIGHT]
ae===javax.swing.JTextField[,35,15,360x50,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@911f71,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=RIGHT]