import javax.swing.*; import java.awt.event.*; public class MyFrame implements ActionListener{ JTextField tf1,tf2; JButton b1; MyFrame(){ JFrame f= new JFrame(); tf1=new JTextField("0"); tf1.setBounds(50,50,150,20); tf2=new JTextField(); tf2.setBounds(50,100,150,20); tf2.setEditable(false); b1=new JButton("2x"); b1.setBounds(50,150,50,50); b1.addActionListener(this); f.add(tf1); f.add(tf2); f.add(b1); f.setSize(260,300); f.setLayout(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { String s1=tf1.getText(); int a=Integer.parseInt(s1); int c=0; if(e.getSource()==b1){ c=a*2; } String result=String.valueOf(c); tf2.setText(result); } public static void main(String[] args) { MyFrame obj = new MyFrame(); } } |
Головна > Java Swing >