2

私は次のコードを持っています.2つのボタンを持つ単純なウィンドウを作成し、それぞれがウィンドウを開くと想定しています.メインウィンドウは問題なく開きますが、ボタンをクリックしても何も起こりません...

package presentation;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ShowInventory extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 7479750059244371227L;
    private JPanel contentPane;
    private JButton catBtn = new JButton ("Display inventory by category");
    private JButton allBtn = new JButton ("Display all inventory");


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShowInventory frame = new ShowInventory();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Exception - Sorry");
                }
            }
        });
    }

    /**
     * Default - details to be added
     */
    public ShowInventory() { // title bar name

        // layout here
        Container container = getContentPane();
        FlowLayout layout = new FlowLayout();
        container.setLayout(layout);
        layout.setAlignment(FlowLayout.CENTER);
        container.add(new JButton("Display inventory by category"));
        container.add(new JButton("Display all inventory"));


        catBtn.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                // controller code
                ShowByCategory frame = new ShowByCategory();
                frame.setVisible(true);
            }
        });



        allBtn.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                // controller code
                ShowAllInventory frame = new ShowAllInventory();
                frame.setVisible(true);
            }
        });

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

}
4

1 に答える 1

4

おそらく、これらの行を置き換える必要があります (これにより、新しいボタンが作成されます)。

container.add(new JButton("Display inventory by category"));
container.add(new JButton("Display all inventory"));

これにより(クラスのボタンを使用し、その上にリスナーを追加します):

container.add(catBtn);
container.add(allBtn);
于 2012-06-21T14:22:20.843 に答える