import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PaymentCalculator extends JFrame{
private JPanel panelTitle,panelInput,panelButtons;
private JLabel lbltuition, lblmisc, lblscholarship, lblfee;
private JTextField txttuition, txtmisc;
private JComboBox cbscholarship;
private JRadioButton optfull, opttuition,optOther;
private ButtonGroup btnpayment;
private JCheckBox chkfees1, chkfees2, chkfees3;
private JButton btncompute, btnclear;
private JList listbuy;
private DefaultListModel listmodel;
private JFrame myFrame,myFrame2;
private double fee=0;
public PaymentCalculator() {
super("Payment Calculator");
setLayout(new BorderLayout());
panelInput = new JPanel();
add(panelInput, BorderLayout.CENTER);
panelInput.setLayout(new GridLayout(10,5,5,5));
lbltuition = new JLabel("Tuition Fee:");
//lbltuition.setBounds(10, 25, 120, 30);
panelInput.add(lbltuition);
txttuition = new JTextField(10);
txttuition.setText("0.0");
//txttuition.setBounds(170, 30, 80,60);
panelInput.add(txttuition);
lblmisc = new JLabel("Miscellaneous Fee:");
// lblmisc.setBounds(10, 55, 120, 30);
panelInput.add(lblmisc);
txtmisc = new JTextField(10);
txtmisc.setText("0.0");
//txtmisc.setBounds(170, 60, 80, 30);
panelInput.add(txtmisc);
String[] strScholar = {"", "", "", "OWA"};
lblscholarship = new JLabel("Scholarship:");
lblscholarship.setBounds(10, 90, 120, 30);
panelInput.add(lblscholarship);
cbscholarship = new JComboBox(strScholar);
cbscholarship.setBounds(130, 95, 135, 20);
panelInput.add( cbscholarship);
optfull = new JRadioButton("Full");
optfull.setBounds(20, 120, 50, 20);
panelInput.add(optfull);
opttuition = new JRadioButton("Tuition Only");
opttuition.setBounds(70, 120, 100, 20);
panelInput.add(opttuition);
btnpayment = new ButtonGroup();
btnpayment.add(optfull);
btnpayment.add(opttuition);
JLabel optOther = new JLabel("Other Fees:");
optOther.setBounds(50, 140, 100, 20);
panelInput.add(optOther);
chkfees1 = new JCheckBox("PE Shirt (500.00)");
chkfees1.setBounds(125, 140, 120, 20);
chkfees2 = new JCheckBox("Lab Manual (200.00)");
chkfees2.setBounds(125, 160, 150, 20);
chkfees3 = new JCheckBox("Lab Gown (100.00)");
chkfees3.setBounds(125, 180, 150, 20);
chkfees1.setMnemonic(500);
chkfees2.setMnemonic(200);
chkfees3.setMnemonic(100);
panelInput.add(chkfees1);
panelInput.add(chkfees2);
panelInput.add(chkfees3);
panelButtons = new JPanel();
panelButtons.setLayout(new GridLayout(4,2));
btncompute = new JButton("Compute");
btncompute.setBounds(25, 220, 100, 20);
panelInput.add(btncompute);
btnclear = new JButton("Clear");
btnclear.setBounds(155, 220, 100, 20);
panelInput.add(btnclear);
ActionListener listener = new ButtonListener();
btncompute.addActionListener(listener);
btnclear.addActionListener(listener);
ItemListener item = new ItemHandler();
chkfees1.addItemListener(item);
chkfees2.addItemListener(item);
chkfees3.addItemListener(item);
listmodel = new DefaultListModel();
listbuy = new JList(listmodel);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JButton btnchoose = (JButton) evt.getSource();
double tuition = Integer.parseInt(txttuition.getText());
double misc = Integer.parseInt(txtmisc.getText());
if (btnchoose == btncompute) {
int answer = JOptionPane.showConfirmDialog(null, "Confirm Payment?", "Payment", JOptionPane.YES_NO_OPTION);
switch (answer) {
case JOptionPane.YES_OPTION:
if ((chkfees1.isSelected() == true) && (chkfees2.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic() + chkfees2.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees2.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition + misc + chkfees2.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees1.isSelected() == true) && (chkfees2.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic() + chkfees2.getMnemonic();
} else if ((chkfees1.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition+ misc + chkfees1.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees1.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic();
} else if ((chkfees2.isSelected() == true)) {
fee = tuition + misc + chkfees2.getMnemonic();
} else if ((chkfees3.isSelected() == true)) {
fee = (tuition ) + misc + chkfees3.getMnemonic();
} else {
fee = tuition + misc;
}
new Payment();
break;
case JOptionPane.NO_OPTION:
break; }
}
else if (btnchoose == btnclear) {
txttuition.setText("0");
txtmisc.setText("0");
chkfees1.setSelected(false);
chkfees2.setSelected(false);
chkfees3.setSelected(false);
cbscholarship.setSelectedIndex(0);
btnpayment.clearSelection();
lblfee.setText("");
return;
}
}
}
private class ItemHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object select = e.getItemSelectable();
if (select == chkfees1) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees1.getText());
} else {
listmodel.removeElement(chkfees1.getText());
}
} if (select == chkfees2) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees2.getText());
} else {
listmodel.removeElement(chkfees2.getText());
}
} if (select ==chkfees3) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees3.getText());
} else {
listmodel.removeElement(chkfees3.getText());
}
}
}
}
public class Payment extends JFrame{
public Payment(){
super("Total Payment");
setLayout(new FlowLayout());
getContentPane().setBackground(Color.white);
lblfee = new JLabel("Overall Fee: "+fee);
lblfee.setBounds(10, 10, 120, 50);
add(lblfee);
setBounds(120,220,200,100);
setSize(230, 100);
setVisible(true);
}
}
public void run() {
setSize(300, 300);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
PaymentCalculator tuition = new PaymentCalculator();
tuition.run();
}
}
stultuske 1,116 Posting Maven Featured Poster
by writing it in a JInternalFrame instead in a JFrame?
a few remarks on your if statements:
if ((chkfees1.isSelected() == true) && (chkfees2.isSelected() == true) && (chkfees3.isSelected() == true))
this can be rewritten as:
if ( chkfees1.isSelected() && chkfees2.isSelected() && chkfees3.isSelected())
makes it a lot easier to read.
in your itemhandler:
if (select == chkfees1) {
// further code
} if (select == chkfees2) {
//further code
} if (select ==chkfees3) {
//further code
}
especially when you have a lot of these, use if else-if, instead of if - if
now you will run all three checks, even if select is chkfees1.
if (select == chkfees1) {
// further code
}else if (select == chkfees2) {
//further code
}else if (select ==chkfees3) {
//further code
}
like this: if select is chkfees1, the other two if statements won't run, since it's already known they'll never be true.
reincom 0 Newbie Poster
Ok thanks :).. but how can i put these codes within the internal frame?
stultuske 1,116 Posting Maven Featured Poster
well, step one would be: create an internal frame.
reincom 0 Newbie Poster
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
public class MainFrame extends JFrame{
private JPanel panelTitle,panelInput,panelButtons;
private JLabel lbltuition, lblmisc, lblscholarship, lblfee;
private JTextField txttuition, txtmisc;
private JComboBox cbscholarship;
private JRadioButton optfull, opttuition,optOther;
private ButtonGroup btnpayment;
private JCheckBox chkfees1, chkfees2, chkfees3;
private JButton btncompute, btnclear;
private JList listbuy;
private DefaultListModel listmodel;
private JFrame myFrame,myFrame2;
private double fee=0;
public MainFrame(){
JInternalFrame iframe = new JInternalFrame("Payment Calculator", true, true, true, true);
panelInput = new JPanel();
add(panelInput, BorderLayout.CENTER);
panelInput.setLayout(new GridLayout(10,5,5,5));
lbltuition = new JLabel("Tuition Fee:");
panelInput.add(lbltuition);
txttuition = new JTextField(10);
txttuition.setText("0.0");
panelInput.add(txttuition);
lblmisc = new JLabel("Miscellaneous Fee:");
panelInput.add(lblmisc);
txtmisc = new JTextField(10);
txtmisc.setText("0.0");
panelInput.add(txtmisc);
String[] strScholar = {"", "", "", "OWA"};
lblscholarship = new JLabel("Scholarship:");
lblscholarship.setBounds(10, 90, 120, 30);
panelInput.add(lblscholarship);
cbscholarship = new JComboBox(strScholar);
cbscholarship.setBounds(130, 95, 135, 20);
panelInput.add( cbscholarship);
optfull = new JRadioButton("Full");
optfull.setBounds(20, 120, 50, 20);
panelInput.add(optfull);
opttuition = new JRadioButton("Tuition Only");
opttuition.setBounds(70, 120, 100, 20);
panelInput.add(opttuition);
btnpayment = new ButtonGroup();
btnpayment.add(optfull);
btnpayment.add(opttuition);
JLabel optOther = new JLabel("Other Fees:");
optOther.setBounds(50, 140, 100, 20);
panelInput.add(optOther);
chkfees1 = new JCheckBox("PE Shirt (500.00)");
chkfees1.setBounds(125, 140, 120, 20);
chkfees2 = new JCheckBox("Lab Manual (200.00)");
chkfees2.setBounds(125, 160, 150, 20);
chkfees3 = new JCheckBox("Lab Gown (100.00)");
chkfees3.setBounds(125, 180, 150, 20);
chkfees1.setMnemonic(500);
chkfees2.setMnemonic(200);
chkfees3.setMnemonic(100);
panelInput.add(chkfees1);
panelInput.add(chkfees2);
panelInput.add(chkfees3);
panelButtons = new JPanel();
panelButtons.setLayout(new GridLayout(4,2));
btncompute = new JButton("Compute");
btncompute.setBounds(25, 220, 100, 20);
panelInput.add(btncompute);
btnclear = new JButton("Clear");
btnclear.setBounds(155, 220, 100, 20);
panelInput.add(btnclear);
iframe.setSize(300,300);
iframe.setLocation(50,50);
iframe.setVisible(true);
JDesktopPane desktop = new JDesktopPane();
desktop.add(iframe);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(desktop);
f.setSize(500,500);
f.setLocation(200,200);
f.setVisible(true);
}
public class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JButton btnchoose = (JButton) evt.getSource();
double tuition = Integer.parseInt(txttuition.getText());
double misc = Integer.parseInt(txtmisc.getText());
if (btnchoose == btncompute) {
int answer = JOptionPane.showConfirmDialog(null, "Confirm Payment?", "Payment", JOptionPane.YES_NO_OPTION);
switch (answer) {
case JOptionPane.YES_OPTION:
if ((chkfees1.isSelected() == true) && (chkfees2.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic() + chkfees2.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees2.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition + misc + chkfees2.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees1.isSelected() == true) && (chkfees2.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic() + chkfees2.getMnemonic();
} else if ((chkfees1.isSelected() == true) && (chkfees3.isSelected() == true)) {
fee = tuition+ misc + chkfees1.getMnemonic() + chkfees3.getMnemonic();
} else if ((chkfees1.isSelected() == true)) {
fee = tuition + misc + chkfees1.getMnemonic();
} else if ((chkfees2.isSelected() == true)) {
fee = tuition + misc + chkfees2.getMnemonic();
} else if ((chkfees3.isSelected() == true)) {
fee = (tuition ) + misc + chkfees3.getMnemonic();
} else {
fee = tuition + misc;
}
new Payment();
break;
case JOptionPane.NO_OPTION:
break; }
}
else if (btnchoose == btnclear) {
txttuition.setText("0");
txtmisc.setText("0");
chkfees1.setSelected(false);
chkfees2.setSelected(false);
chkfees3.setSelected(false);
cbscholarship.setSelectedIndex(0);
btnpayment.clearSelection();
lblfee.setText("");
return;
}
}
}
private class ItemHandler implements ItemListener {
public void itemStateChanged(ItemEvent e) {
Object select = e.getItemSelectable();
if (select == chkfees1) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees1.getText());
} else {
listmodel.removeElement(chkfees1.getText());
}
} if (select == chkfees2) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees2.getText());
} else {
listmodel.removeElement(chkfees2.getText());
}
} if (select ==chkfees3) {
if (e.getStateChange() == ItemEvent.SELECTED) {
listmodel.addElement(chkfees3.getText());
} else {
listmodel.removeElement(chkfees3.getText());
}
}
}
}
public class Payment extends JFrame{
public Payment(){
super("Total Payment");
setLayout(new FlowLayout());
getContentPane().setBackground(Color.white);
lblfee = new JLabel("Overall Fee: "+fee);
lblfee.setBounds(10, 10, 120, 50);
add(lblfee);
setBounds(120,220,200,100);
setSize(230, 100);
setVisible(true);
}
}
public static void main(String[] args)
{
new MainFrame();
}
}
ok so this is my program with internalframe, i dont know how to put Payment Calculator codes within the internalframe
stultuske 1,116 Posting Maven Featured Poster
you have to add the components to the interalFrame. the 'add(Component comp)' method is used for that.
for instance: right before your iframe.setSize(300,300);
add the lineiframe.add(panelInput);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
You should take 5 minutes to read this Oracle tutorial (on internal frames and how to use them) before doing anything else...
http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
reincom 0 Newbie Poster
Oh! Ok thank you! :))
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.