GUIを使用して問題を解決しようとしますが、文は私を混乱させている場合

Muath:

私はプロジェクトを解決しようとしているが、私は問題でこだわっています。

•ユーザーが実行することを可能にするメニューを表示すべきであるあなたのプログラム
以下の操作(注:使用GUIは):
1.新しい顧客を追加し
、顧客の削除2.
3.変更を顧客の情報//サブメニューを表示する必要があり、このオプション以下のように:
-------- 1。個々の顧客//は、基本的な情報を変更します。電話番号を...
コード:

 //Modify Customer
    if (actionEvent.getSource().equals(modifyCustomer)) {
        frame.dispose();
        frame = new JFrame();
        panel = new JPanel();

        individualCustomer = new JButton("Individual customer");
        individualCustomer.addActionListener(this);

        panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        panel.setLayout(new GridLayout(0, 1));
        panel.add(individualCustomer);

        frame.setTitle("General Distribution Center");
        frame.add(panel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        //Individual Customer
        if (actionEvent.getSource().equals(individualCustomer)) {
            frame.dispose();
            System.out.println("Enter the new phone number: ");
            int updatedPhoneNumber = input.nextInt();
        }

私の最初の問題は、次のとおりです。文は偽であるなぜ、次の場合は?

if (actionEvent.getSource().equals(individualCustomer)) {

私の第二の問題は、次のとおりです。if文がtrueの場合...例えば、私が入力した場合

if(true){
         frame.dispose();
         System.out.println("Enter the new phone number: ");
         int updatedPhoneNumber = input.nextInt();
     }

それは私が変更カスタマー・オプションを選択した直後にこのブロックを実行します。私はここで作成した個々の顧客オプション/ボタンを表示せずに:

        individualCustomer = new JButton("Individual customer");
        individualCustomer.addActionListener(this);

あなたは答えを知っているし、このプロジェクトの問題を共有するために、論理的にそれらをより良い答えを持っていると私はそこからうまくいくしないでください場合。

ウナギのホバークラフトのフル:

あなたは持っている巨大なのActionListener、一度にあまりにも多くのことをやろうとしているものを。if文、第2の理由は、あなたのコードが配線されている方法の真になることはありません。あなたが入れ子になってしまった場合の真の外である場合ならば、するかどうかインナー(到達させる場合には、内側が可能)というように、常に falseであります:

if (actionEvent.getSource().equals(modifyCustomer)) {
    // if we're here, then source will **never** be individualCustomer

    //..... code here

    // the if-test below will **always** be false
    if (actionEvent.getSource().equals(individualCustomer)) {
         // .... code here
    }
}

あなたはこれらの場合には、直列を作ることができます。

if (actionEvent.getSource().equals(modifyCustomer)) {

    //.....

} else if (actionEvent.getSource().equals(individualCustomer)) {

    //.....

}

それが動作します。

ベターそれぞれのJButtonに懸念を分離するために、独自の匿名の内部ActionListenerを与えるために

individualCustomer = new JButton("Individual customer");
individualCustomer.addActionListener(() -> {
    // button-specific listener code goes here
});

このコードについて:

if(true){
     frame.dispose();
     System.out.println("Enter the new phone number: ");
     int updatedPhoneNumber = input.nextInt();
}

それはあなたが線形コンソールスキャナを使用したプログラミングとイベント駆動型のGUIでのprintlnをミックスしようとしているように見え、ほとんど常に2つのパラダイムがうまく混ざらないように失敗することが保証されています。1つのここでのパラダイム、イベント駆動型の方法でGUIを介してすべての入力を取得してスティック、およびSystem.inに基づいて、すべてのスキャナのコードを削除してスティック。


最後に一つは、見ていませんしてください複数JFramesの使用、グッド/悪い習慣を?

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=341576&siteId=1