40. Consider the GUI below, determine if the number is either of the following:
ODD-POSITIVE
ODD-NEGATIVE
EVEN-POSITIVE
EVEN-NEGATIVE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberGUI extends JFrame{
private static String str="";
private JLabel jl1,jlResult;
private JTextField jt1;
private JButton btnOK,btnCANCEL;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public NumberGUI(){
jl1=new JLabel("Enter a number: ");
jlResult=new JLabel();
jt1=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(500,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num=0;
num=Integer.parseInt(jt1.getText());
String display=ODDEVEN(num);
jlResult.setText(num + " is "+display);
}
public String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new NumberGUI();
}
}
Walang komento:
Mag-post ng isang Komento