[英]Can someone Tell me how can i get Input from jtf1 and jtf2 and Display the output on jtf3?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Event implements ActionListener{
JButton jb1;
void setGui()
{
JFrame x=new JFrame("CALCULATOR");
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setSize(800,800);
x.setVisible(true);
Container c=x.getContentPane();
c.setLayout(null);
c.setBackground(Color.BLACK);
JLabel jl1=new JLabel("NUMBER 1");
jl1.setSize(100,50);
jl1.setLocation(0,0);
jl1.setForeground(Color.YELLOW);
c.add(jl1);
JLabel jl2=new JLabel("NUMBER 2");
jl2.setSize(100,50);
jl2.setLocation(0,150);
jl2.setForeground(Color.YELLOW);
c.add(jl2);
JLabel jl3=new JLabel("ANSWER");
jl3.setSize(100,50);
jl3.setLocation(0,400);
jl3.setForeground(Color.YELLOW);
c.add(jl3);
JTextField jtf1=new JTextField();
jtf1.setSize(100,50);
jtf1.setLocation(80,0);
jtf1.setBackground(Color.GREEN);
c.add(jtf1);
JTextField jtf2=new JTextField();
jtf2.setSize(100,50);
jtf2.setLocation(80,150);
jtf2.setBackground(Color.GREEN);
c.add(jtf2);
JTextField jtf3=new JTextField();
jtf3.setSize(100,50);
jtf3.setLocation(80,400);
jtf3.setBackground(Color.GREEN);
c.add(jtf3);
jb1=new JButton("ADD");
jb1.setSize(100,100);
jb1.setLocation(300,500);
jb1.setBackground(Color.RED);
c.add(jb1);
JButton jb2=new JButton("MULTIPLY");
jb2.setSize(100,100);
jb2.setLocation(450,500);
jb2.setBackground(Color.RED);
c.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent q)
{ if(q.getSource()==jb1)(jb1=o,jb2=n)
{
}
}
}
class DX
{
public static void main(String args[])
{
Event et=new Event();
et.setGui();
}
}
//這是我編寫的Java代碼,對此需要一點幫助。 謝謝// // https://lh4.googleusercontent.com/-P9R0IIsAcz8/Vk6QQSs63SI/AAAAAAAAABk/d0q1jfpm9cE/w908-h809-no/fffffffff.PNG // https://lh4.googleusercontent.com/-5yjWTnQ8X8o/Vk6QV /17k1HnHGmlc/w869-h629-no/hhhh.PNG
請參考此內容,只需將以下代碼復制到您的機器上並運行。 我希望這就是你想要的。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DX {
public static void main(String args[])
{
System.out.println("Hello");
Event et=new Event();
et.setGui();
}
}
class Event implements ActionListener{
JButton jb1;
JTextField jtf1;
JTextField jtf2;
JTextField jtf3;
void setGui()
{
JFrame x=new JFrame("CALCULATOR");
x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x.setSize(800,800);
x.setVisible(true);
Container c=x.getContentPane();
c.setLayout(null);
c.setBackground(Color.BLACK);
JLabel jl1=new JLabel("NUMBER 1");
jl1.setSize(100,50);
jl1.setLocation(0,0);
jl1.setForeground(Color.YELLOW);
c.add(jl1);
JLabel jl2=new JLabel("NUMBER 2");
jl2.setSize(100,50);
jl2.setLocation(0,150);
jl2.setForeground(Color.YELLOW);
c.add(jl2);
JLabel jl3=new JLabel("ANSWER");
jl3.setSize(100,50);
jl3.setLocation(0,400);
jl3.setForeground(Color.YELLOW);
c.add(jl3);
jtf1=new JTextField();
jtf1.setSize(100,50);
jtf1.setLocation(80,0);
jtf1.setBackground(Color.GREEN);
c.add(jtf1);
jtf2=new JTextField();
jtf2.setSize(100,50);
jtf2.setLocation(80,150);
jtf2.setBackground(Color.GREEN);
c.add(jtf2);
jtf3=new JTextField();
jtf3.setSize(100,50);
jtf3.setLocation(80,400);
jtf3.setBackground(Color.GREEN);
c.add(jtf3);
jb1=new JButton("ADD");
jb1.setSize(100,100);
jb1.setLocation(300,500);
jb1.setBackground(Color.RED);
c.add(jb1);
JButton jb2=new JButton("MULTIPLY");
jb2.setSize(100,100);
jb2.setLocation(450,500);
jb2.setBackground(Color.RED);
c.add(jb2);
jb1.addActionListener(this);
jb2.addActionListener(this);
}
public void actionPerformed(ActionEvent q)
{
System.out.println(q.getActionCommand());
if((jtf1.getText() != null && jtf1.getText().length() > 0) && (jtf2.getText() != null && jtf2.getText().length() > 0)){
try {
int val1 = Integer.parseInt(jtf1.getText()) ;
int val2 = Integer.parseInt(jtf2.getText()) ;
if("MULTIPLY".equals(q.getActionCommand())){
int ans = (val1*val2);
jtf3.setText(ans+"");
}else if("ADD".equals(q.getActionCommand())){
int ans = (val1+val2);
jtf3.setText(ans+"");
}
} catch (Exception e) {
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
JOptionPane.showMessageDialog(null, "Please Enter Valid Value", "Error", 0);
}
}else{
jtf1.setText("");
jtf2.setText("");
jtf3.setText("");
JOptionPane.showMessageDialog(null, "Please Enter Text First", "Error", 0);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.