2017-02-14 3 views
-1

距離を変換する方法を構築する必要があります。例えば、kmをマイルに変換し、その逆もあります。私はkmをマイルに変換する方法を知っていますが、複数のメソッドを記述しないようにして、コードがあまりにも多くなるようにしています。コンバータGUI(距離変換器)を実行する方法がありません

import java.awt.Color; 
import java.awt.Container; 
import java.awt.FlowLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

public class Parametri 
    extends JFrame { 
    /** Konstruktor Parametri**/ 
    public Parametri() { 
     super("Menyra 1"); 
     // setTitle ("Menyra 1")-ky funksion eshte kryer nga super("Menyra 1"); 
     this.setSize(260, 300); 
     this.setVisible(true); 
     JLabel label1 = new JLabel("Konvertimi i njesive"); 
     JLabel label2 = new JLabel("Lloj i konvertimit"); 
     // JComboBox 
     JComboBox lloj1 = new JComboBox(); 
     lloj1.addItem("Gjatesi"); 
     lloj1.addItem("Gjeresi"); 
     lloj1.addItem("Lartesi"); 
     lloj1.addItem("Trashesi"); 

     JLabel label5 = new JLabel("Vendosni vleren qe doni te konvertoni"); 
     JTextField input = new JTextField(10);// 10-gjatesia e fushes por jo numri i karaktereve 
     JLabel label6 = new JLabel("Nga:"); 
     JComboBox llojA = new JComboBox(); 
     llojA.addItem("Meter (M)"); 
     llojA.addItem("Kilometer (KM)"); 
     llojA.addItem("Centimeter (CM)"); 
     llojA.addItem("Milimeter(MM)"); 
     llojA.addItem("Inch (Inch)"); 
     llojA.addItem("Milje (Milje)"); 
     JLabel label3 = new JLabel("Konvertuar ne:"); 
     JComboBox llojB = new JComboBox(); 
     llojB.addItem("Meter (M)"); 
     llojB.addItem("Kilometer (KM)"); 
     llojB.addItem("Centimeter (CM)"); 
     llojB.addItem("Milimeter(MM)"); 
     llojB.addItem("Inch (Inch)"); 
     llojB.addItem("Milje (Milje)"); 
     JButton button3 = new JButton("Konverto"); 
     JLabel label4 = new JLabel("Vlera e konvertuar"); 
     JTextField output = new JTextField(10);// 10-gjatesia e fushes por jo numri i karaktereve 
     JButton button2 = new JButton("Fshij"); 

     Container c = this.getContentPane(); 
     c.setLayout(new FlowLayout()); 
     c.setBackground(Color.orange); 
     c.add(label1); 
     c.add(label2); 
     c.add(lloj1); 
     c.add(label5); 
     c.add(input); 
     c.add(label6); 
     c.add(llojA); 
     c.add(label3); 
     c.add(llojB); 
     c.add(button3); 
     c.add(label4); 
     c.add(output); 
     c.add(button2); 



     label1.setForeground(Color.blue); 
     button2.setBackground(Color.black); 
     button2.setForeground(Color.red); 
     button3.setBackground(Color.black); 
     button3.setForeground(Color.red); 
     this.setContentPane(c); 

    } 

    public static void main(String[] args) { 
     Parametri m1 = new Parametri(); 
    } 
} 

答えて

0

ストアます。たとえば、静的なfinalクラス変数として、それをすることによって乗算する必要があり値:

public class Parametri{ 

    public static final float MILES_TO_KILOMETERS = 1.60934; 
    public static final float KILOMETERS_TO_MILES = 0.621371; 

} 

その後、パラメータとして値と換算係数を取る方法を持っています

public float convert(float amount, float conversionFactor){ 
    return amount * conversionFactor; 
} 

そしてだけメソッドを呼び出し、所望の因子を渡し:

float miles = convert(kilometers, KILOMETERS_TO_MILES); 

それ以外の場合は、変換ごとに複数のヘルパーメソッドを作成せずに変換する方法はありません。私はなぜそれが複雑になるかわかりません、彼らはそれぞれ3行になり、毎回メソッドに変換値を渡す必要はありません。

+0

私は実際にあなたが何を言っているかを削除するには分からない – Albanian

関連する問題