Example: Radio multiplication and add Eclipse in Java
package swing;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.Font;
public class swingapp {
private JFrame frame;
private JTextField textFielda;
private JTextField textFieldb;
private JTextField textFieldc;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
swingapp window = new swingapp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public swingapp() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 258, 173);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JRadioButton rdbtnAdd = new JRadioButton("ADD");
buttonGroup.add(rdbtnAdd);
rdbtnAdd.setBounds(18, 7, 75, 23);
frame.getContentPane().add(rdbtnAdd);
JRadioButton rdbtnMutis = new JRadioButton("Multi");
buttonGroup.add(rdbtnMutis);
rdbtnMutis.setBounds(18, 44, 75, 23);
frame.getContentPane().add(rdbtnMutis);
JButton btnGo = new JButton("Go");
btnGo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Enumeration<AbstractButton> bg = buttonGroup.getElements();
while(bg.hasMoreElements()){
JRadioButton jrd = (JRadioButton) bg.nextElement();
int num1, num2, ans;
num1 = Integer.parseInt(textFielda.getText());
num2 = Integer.parseInt(textFieldb.getText());
if(jrd.isSelected()){
if(jrd == rdbtnAdd){
ans =num1+num2;
textFieldc.setText(Integer.toString(ans));
}else{
ans =num1*num2;
textFieldc.setText(Integer.toString(ans));
}
}
}
}
});
btnGo.setBounds(18, 78, 59, 23);
frame.getContentPane().add(btnGo);
textFielda = new JTextField();
textFielda.setBounds(99, 8, 86, 20);
frame.getContentPane().add(textFielda);
textFielda.setColumns(10);
textFieldb = new JTextField();
textFieldb.setBounds(99, 45, 86, 20);
frame.getContentPane().add(textFieldb);
textFieldb.setColumns(10);
textFieldc = new JTextField();
textFieldc.setBounds(99, 79, 86, 20);
frame.getContentPane().add(textFieldc);
textFieldc.setColumns(10);
JLabel lblA = new JLabel("a");
lblA.setBounds(192, 11, 46, 14);
frame.getContentPane().add(lblA);
JLabel lblB = new JLabel("b");
lblB.setBounds(192, 48, 46, 14);
frame.getContentPane().add(lblB);
JLabel lblC = new JLabel("c");
lblC.setBounds(192, 82, 46, 14);
frame.getContentPane().add(lblC);
JLabel lblDesignByMr = new JLabel("Design By Mr.24!");
lblDesignByMr.setFont(new Font("Mistral", Font.PLAIN, 16));
lblDesignByMr.setBounds(99, 110, 109, 14);
frame.getContentPane().add(lblDesignByMr);
}
}
0 nhận xét:
Post a Comment