0

JTextFieldのテキストを受け取り、JButtonを押したときに宣言した変数にテキストを入れるプログラムを作成しようとしています。学校のプロジェクトの週給を計算する必要がありますが、必要なのはコンソールだけです。自分の楽しみのためにGUIを実行しています。私はそれを取得しようとしているので、「calc」を押すと、id、rh、oh、hpなどから入力を取得し、週給(wp)を計算します。これは、右側の列の横に印刷されます。計算ボタン。

//the calculations aren't complete yet until I finish the GUI

public class Weekly_Pay 
{

public static void calculations(String[] args) 
{

Scanner imput = new Scanner(System.in);

System.out.println("ID number: ");
int employeeId = imput.nextInt();

System.out.println("Hourly Wage: ");
Double hourlyWage = imput.nextDouble();

System.out.println("Regular Hours: ");
double regularHours = imput.nextDouble();

System.out.println("Overtime Hours: ");
double overtimeHours = imput.nextDouble();

double overtimePay = round(overtimeHours * (1.5 * hourlyWage));
double regularPay  = round(hourlyWage * regularHours);

double weeklyPay = regularPay + overtimePay;

System.out.println("Employee ID Number:" + employeeId);
System.out.printf("Weekly Pay: " + "$%.2f\n", weeklyPay);

}

public static double round(double num) 
{

// rounding to two decimal places
num *= 100;
int rounded = (int) Math.round(num);
return rounded/100.0;

}


public static void main(String[] args) 
{

JFrame window = new JFrame();
window.setTitle("Weekly Pay");
window.setSize(350, 200);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Color lGray = new Color(209, 209, 209);

JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setBackground(lGray);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);

JTextField idEntry = new JTextField(); //where the user imputs their ID
JTextField hwEntry = new JTextField(); //where the user imputs their hourly wage
JTextField rhEntry = new JTextField(); //where the user imputs their regular hours
JTextField ohEntry = new JTextField(); //where the user imputs their overtime hours

JLabel id = new JLabel("ID Number");
JLabel hw = new JLabel("Hourly Wage");
JLabel rh = new JLabel("Regular Hours");
JLabel oh = new JLabel("Overtime Hours");
JButton calc = new JButton("Calculate");
JLabel wp = new JLabel(" Weekly Pay: $" + "$%.2f\n", weeklyPay);

GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();    
hGroup.addGroup(layout.createParallelGroup().
           addComponent(id).addComponent(hw).addComponent(rh).addComponent(oh).addComponent(calc));
hGroup.addGroup(layout.createParallelGroup().
  addComponent(idEntry).addComponent(hwEntry).addComponent(rhEntry).addComponent(ohEntry).addComponent(wp));
layout.setHorizontalGroup(hGroup);

GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();    
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(id).addComponent(idEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(hw).addComponent(hwEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(rh).addComponent(rhEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(oh).addComponent(ohEntry));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
    addComponent(calc).addComponent(wp));
layout.setVerticalGroup(vGroup);

window.add(panel);
window.setVisible(true);

}  
}
4

1 に答える 1

9

例えば:

String input = new String();         
JButton mbutt = new JButton;
JTextField jtxt = new JTextField();

mbutt.addActionListener(new ActionListener(){

       public void actionPerformed(ActionEvent event){    
             input = jtxt.getText().toString();
       }
 });

//////////////////////////////////編集された部分////////////// ////////////////

コードに飛び込む前に、いくつかのことがあります。

-ActionListenerの動作と、フィールドからデータを抽出して変数に入れる方法を示したかっただけです。

-コンポーネントをに直接配置するのは悪い習慣であり、 (私にはあまりにも悪いです.. !!!)、常にオーバーのようなものを使用してから、コンポーネントをその上に配置する必要があります。シンプルに保つために、コンポーネントを保持するために意図的に直接JFrameを使用しています。JFramethats exactly what i have done hereJPanelJFrame

-そして、はい、それは常に非常に良い習慣ですUI work on the UI thread, and Non-UI work on Non-UI thread

--Swingsでは、メソッドは長続きしません。GUI の構築をスケジュールした後、終了します...したがって、GUIを処理するのはEDTの責任であるため、私が行ったように、GUIのみを処理するためにEDTを保持する必要があります。メソッド[ ]でそれ。main()Event Dispatcher Thread main()EventQueue.invokeLater()

完全なコード:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Tes extends JFrame {

    String input;
    JTextField jtxt; 
    JButton mbutt; 


    public Tes(){

 //--ALWAYS USE A JPANEL OVER JFRAME, I DID THIS TO KEEP IT SIMPLE FOR U--//

        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setComponent();
        this.setHandler();
    }

    public void setComponent(){

        jtxt =  new JTextField("Hello");

        mbutt = new JButton("Button"); 

        this.add(BorderLayout.SOUTH,mbutt);

        this.add(BorderLayout.NORTH,jtxt);

    }

    public void setHandler(){

        mbutt.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                input = jtxt.getText().toString();

                System.out.println("Input Value: "+input);

          **//--See your Console Output everytime u press the button--//**

            }
        });

    }
    public static void main(String[] args){


         EventQueue.invokeLater(new Runnable(){

            @Override
            public void run() {

                Tes t = new Tes();
                t.setVisible(true);

            }



         });
    }

}
于 2012-10-12T16:48:40.210 に答える