
import java.util.Scanner; import javax.swing.JFrame; import javax.swing.JOptionPane;
public class ShippingCost {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
JFrame frame = new JFrame();
String itemName = "";
double weight = 0.0;
double firstKg = 10.0;
double nextKg = 5.0;
double num1 = 0.0;
//itemName = JOptionPane.showInputDialog(frame, "Enter item name:");
//weight = Double.parseDouble(JOptionPane.showInputDialog(frame, "Enter item weight:"));
System.out.println("Please insert item name : ");
itemName = sc.next();
System.out.println("Please insert weight : ");
weight = sc.nextDouble();
if(weight <= 1.0){
System.out.println("Your shipping cost is RM" + firstKg );
}else {
weight = weight - 1;
num1 = weight * nextKg;
num1 = firstKg + num1;
System.out.println("Your shipping cost is RM" + num1);
}
}
}
how to do gui for this coding..tq

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ShippingCost {
public static void main(String[] args){
JFrame f=new JFrame();
JLabel lab1=new JLabel("Enter Item Name: ");
JLabel lab2=new JLabel("Enter Weight: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JPanel p=new JPanel(new GridLayout(3,2));
JButton b=new JButton("Find");
p.add(lab1);
p.add(text1);
p.add(lab2);
p.add(text2);
p.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String itemName = text1.getText();
double weight = Double.parseDouble(text2.getText());
double firstKg = 10.0;
double nextKg = 5.0;
double num1 = 0.0;
if(weight <= 1.0){
System.out.println("Your shipping cost is RM" + firstKg );
}else {
weight = weight - 1;
num1 = weight * nextKg;
num1 = firstKg + num1;
System.out.println("Your shipping cost is RM" + num1);
}
}
});
f.add(p);
f.setVisible(true);
f.setSize(300,100);
}
}

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ShippingCost {
public static void main(String[] args){
JFrame f=new JFrame();
JLabel lab1=new JLabel("Enter Item Name: ");
JLabel lab2=new JLabel("Enter Weight: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JPanel p=new JPanel(new GridLayout(3,2));
JButton b=new JButton("Find");
p.add(lab1);
p.add(text1);
p.add(lab2);
p.add(text2);
p.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String itemName = text1.getText();
double weight = Double.parseDouble(text2.getText());
double firstKg = 10.0;
double nextKg = 5.0;
double num1 = 0.0;
if(weight <= 1.0){
JOptionPane.showMessageDialog(null,"Your shipping cost is RM" + firstKg );
}else {
weight = weight - 1;
num1 = weight * nextKg;
num1 = firstKg + num1;
JOptionPane.showMessageDialog(null,"Your shipping cost is RM" + num1);
}
}
});
f.add(p);
f.setVisible(true);
f.setSize(300,100);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.