Basic Sample 1

hundredcarriageΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

88 εμφανίσεις

Basic Sample 1

To display a message in a Text Box on click of OK button



/*

* BS_1.java

* Created on Aug 20, 2009, 8:06:03 AM

*/

package basic_samples;

/**

* @author MK

*/


public class BS_1 extends javax.swing.JFrame

{

public BS_1() {

initComponents();

}

private void jButton1ActionPerformed

(java.awt.event.ActionEvent evt)

{

jTextField1.setText("Hello world");

}

public static void main(String args[])

{

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

new BS_1().setVisible(true);

}

});

}

// Variables declaration
-

do not modify

private javax.swing.JButton jButton1;

private javax.swing.JTextField jTextField1;

// End of variables declaration

}




Basic Sample 1_1

To display a message as Lebels on click of OK button


private void jButt
on1ActionPerformed

(java.awt.event.ActionEvent evt)

{

jLabel1.setText("Hello world");

}

private void jButton2ActionPerformed

(java.awt.event.ActionEvent evt)

{

if (jLabel2.getText()=="Good Morning")

jLabel2.setText("Good evening");

else

jLabel2.setText(
"Good Morning");

}












Basic Sample 2

To accept values from Text Boxes and perform arithmetic operation and display

the final result in a Text Box on click of Calculate Button


Alternative 1

private void jButton1ActionPerformed

(
java.awt.event.ActionEvent evt)

{

jTextField4.setText

(Double.toString(

(Double.parseDouble(jTextField1.getText())

*Double.parseDouble(jTextField2.getText())

*Double.parseDouble(jTextField3.getText()))/100));

}


Alternative 2


private

void jButton1ActionPerformed

(java.awt.event.ActionEvent evt)

{

double P,R,T,SI;

P= Double.parseDouble (jTextField1.getText());

R= Double.parseDouble (jTextField2.getText());

T= Double.parseDouble (jTextField3.getText());

SI = P*R*T/100;

jTextField4.setTe
xt(Double.toString(SI));

}







Basic Sample 3

To accept values from Text Boxes and perform arithmetic operation and display

in a MessageBox and final result in a Text Box on click of CalculateButton



private void jButton1ActionPerformed

(
java.awt.event.ActionEvent evt)

{

int sp = Integer.parseInt

(jTextField1.getText());

int cp = Integer.parseInt

(jTextField2.getText());

if (sp < cp )

{

JOptionPane.showMessageDialog

(this, "You have suffered a loss");

jTextField3.setText

(Integer.toString(
cp
-

sp));

}

else

{

JOptionPane.showMessageDialog

(null, "You have made a profit");

jTextField3.setText

(Integer.toString(sp
-

cp));

}

}






Basic Sample 4

To accept values from Radio Button and Check Boxes and display relevant

message

on Dialog Box and Text Box on click of Submit Button



private void jButton1ActionPerformed

(java.awt.event.ActionEvent evt)

{

String Gender;

String Disp = "You R playing
-

";

if (jRadioButton3.isSelected())

{

Gender = "Boy";

JOptionPane.showMessageDialog

(null,"Please go to the Boys stadium");

}

if (jRadioButton4.isSelected())

{

Gender = "Girl";

JOptionPane.showMessageDialog

(null,"Please go to the Girls stadium");

}

if (jCheckBox1.isSelected())

{

Disp = Disp + " Basketball ";

}

if (jCheckBox2.isSelected())

{

Disp = Disp + " Football ";

}

if (jCheckBox3.isSelected())

{

Disp = Disp + " Swimming ";

}

if (jCheckBox4.isSelected())

{

Disp = Disp + " Hockey ";

}

jTextField1.setText(Disp);

}


Basic Sample 5

To accept value from
ComboBox and display relevant message on Dialog Box on

click of Display Button







private void jButton1ActionPerformed

(java.awt.event.ActionEvent evt)

{

String chosen;

chosen
="Great to know that you are from "

+(String)

jComboBox1.getSelectedItem();

JOptionPane.showMessageDialog

(null, chosen);

}





Comma separated

options for ComboBox
to

be entered in model

property

Basic Sample 6

To accept value from Text Boxes and display relevant message in a Dialog Box

and

Concatenated Message in a Text area on click of OK Button



On Click of [OK] Button

private void jButton1ActionPerformed

(java.awt.event.ActionEvent evt)

{

JOptionPane.showMessageDialog

(null,"Something Happening...");

String Text="Hi";

for (int I=Integ
er.parseInt(jTextField1.getText());

I<=Integer.parseInt(jTextField2.getText());

I++)

{

Text=Text+" Hello";

}

jTextArea1.setText(Text);

}

On Click of [Cancel] Button

private void jButton2ActionPerformed

(java.awt.event.ActionEvent evt)

{

System.exit(0);

}