0

私はプログラミングにかなり慣れていないので、ユーザーがレコードを追加、削除、およびソートできるペットハウス プログラムの GUI を作成する必要があります。

問題は、何らかの理由で私ActionListenersが機能していないことです。GUI は問題なく開きますが、メニュー項目をクリックしても何も起こりません。Quit MenuItem は何もしませんし、add animal も何もしません。削除も機能しません (ただし、これは私の側のプログラミング エラーである可能性もあります)。

コードは次のとおりです。

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import scrap.knockoff;

public class knockoff extends JFrame {

    private ButtonHandler handler;
    static int animals;
    static Color Backgroundcolor = Color.red;
    private JTable thelist;
    private JTextField search, breed, category, buyprice, sellprice;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem menuAdd, menuDelete, menuQuit;
    private JLabel maintitle, breedquery, categoryquery, buypricequery, sellpricequery;
    private JButton sortbreed, sortcat, sortdog, sortratios, searchButton, add, delete, modify;
    private JPanel animalPane, sortPane, titlePane, searchPane, tablePane, optionPane;
    static Container container;
    static int recnumber;
    static String control[] = new String[100];
    static double buyprices, sellprices, profitratios;
    static String BreedName, CategoryName;
    static String Pets[][] = new String[50][5];

    public knockoff() {
        super("The Peel Pet House Program"); //This just changes the name at the top there.
        setSize(700, 600);
        Border paneEdge = BorderFactory.createEmptyBorder(0, 10, 10, 10);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setLocation(0, 30);
        tabbedPane.setSize(200, 100);
        //These are our panels. The sortPane just keeps the buttons responsible for sorting the animals where they are. The animalPane is for the other functions (add, delete, whatnot) and the title pane
        //just holds the title. The AnimalPane is just a constant, it's there just so that the Animals menu process has a place to be. The Table Pane holds the table.
        tablePane = new JPanel();
        tablePane.setLocation(200, 35);
        tablePane.setSize(500, 500);
        titlePane = new JPanel();
        titlePane.setSize(700, 30);
        animalPane = new JPanel();
        Border greenline = BorderFactory.createLineBorder((Color.green).darker());
        animalPane.setBorder(greenline);
        animalPane.setLocation(0, 135);
        animalPane.setSize(200, 400);
        sortPane = new JPanel();
        sortPane.setBorder(paneEdge);
        search = new JTextField();
        searchButton = new JButton("Search");
        searchPane = new JPanel();
        FlowLayout searchLayout = new FlowLayout();
        searchPane.setLayout(searchLayout);
        searchPane.setBorder(paneEdge);
        search.setPreferredSize(new Dimension(190, 30));
        searchPane.add(search);
        searchPane.add(searchButton);
        optionPane = new JPanel();
        optionPane.setSize(100, 30);
        //This establishes our table
        String[] columns = {"Breed", "Category", "Buying Price", "Selling Price", "Profit Ratio"};
        thelist = new JTable(Pets, columns);
        JScrollPane listbrowser = new JScrollPane(thelist);
        tablePane.add(listbrowser);
        //These establish the buttons for our various sorting sprees.
        sortbreed = new JButton("Breed");
        sortcat = new JButton("Cat");
        sortdog = new JButton("Dog");
        sortratios = new JButton("Profit Ratio");
        sortPane.add(sortbreed);
        sortPane.add(sortcat);
        sortPane.add(sortdog);
        sortPane.add(sortratios);
        //Our ever reliable menu bar maker.
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        menu = new JMenu("File");
        menuBar.add(menu);
        //The items of the file menu.
        menuQuit = new JMenuItem("Quit");
        menuQuit.addActionListener(handler);
        menu.add(menuQuit);
        //Animal Menu Creation
        menu = new JMenu("Animals");
        menuBar.add(menu);
        menuAdd = new JMenuItem("Add an Animal");
        menuAdd.addActionListener(handler);
        menu.add(menuAdd);
        menuDelete = new JMenuItem("Delete Animal");
        menuDelete.addActionListener(handler);
        menu.add(menuDelete);
        //Adding everything to the container now.
        Container container = getContentPane();
        container.setLayout(null);
        maintitle = new JLabel("The Piddly Penultimate Peel Pet House Program");
        titlePane.add(maintitle);
        container.setBackground(Backgroundcolor.darker()); //This just makes a darker version of red to set as the background on the Content Pane. Grey is boring.
        container.add(tablePane);
        container.add(titlePane);
        container.add(tabbedPane);
        container.add(animalPane);
        container.add(optionPane);
        tabbedPane.addTab("Sort By:", null, sortPane, "Sorts the Table");
        tabbedPane.addTab("Search", null, searchPane, "The Search Function");
    }

    public static void main(String args[]) {
        knockoff application = new knockoff();
        application.setVisible(true);
    }

    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            if (event.equals("Add an Animal")) {
                addananimal();
            } else if (event.equals("Delete Animal")) {
                deleteanimal();
            } else if (event.getSource().equals(add)) //getsource relates to the button, it just makes it so that anybody programming can freely change the text of the button without worrying about this.
            {
                addanimal();
            } else if (event.getSource().equals(delete)) {
                deleteanimal();
            } else if (event.getSource().equals(sortbreed)) {
                breedsort();
            } else if (event.getSource().equals(sortcat)) {
            } else if (event.getSource().equals(sortdog)) {
            } else if (event.getSource().equals(sortratios)) {
            } else if (event.equals("Quit")) {
                hide();
                System.exit(0);
            }
        }
        //Add new Record Method

        public void addananimal() {
            container = getContentPane();
            //Plate cleaner. Or dishwasher.
            if (animalPane != null) {
                container.remove(animalPane);
            }
            animalPane = new JPanel();
            Border greenline = BorderFactory.createLineBorder((Color.green).darker());
            animalPane.setBorder(greenline);
            animalPane.setLocation(0, 135);
            animalPane.setSize(200, 400);
            FlowLayout animalLayout = new FlowLayout();
            animalPane.setLayout(animalLayout);
            breedquery = new JLabel("Add Animal Name:");
            categoryquery = new JLabel("Cat or Dog?");
            buypricequery = new JLabel("Buying Price:");
            sellpricequery = new JLabel("Selling Price:");
            animalPane.add(breedquery);
            breed = new JTextField(18);
            animalPane.add(breed);
            animalPane.add(categoryquery);
            category = new JTextField(18);
            animalPane.add(category);
            animalPane.add(buypricequery);
            buyprice = new JTextField(18);
            animalPane.add(buyprice);
            animalPane.add(sellpricequery);
            sellprice = new JTextField(18);
            add = new JButton("Add Animal");
            //The above list of .add and JComponent things were just to establish the
//            contents of our new animalPane.
            profitratios = Double.parseDouble(sellprice.getText()) / Double.parseDouble(buyprice.getText());
            //This just makes finding the profit ratio an autonomous action.
            add.addActionListener(handler);
            animalPane.add(add);
            container.add(animalPane);
            setVisible(true);
        }

        public void addanimal() {
            animals = animals + 1;
            Pets[animals][0] = breed.getText();
            Pets[animals][1] = category.getText();
            Pets[animals][2] = buyprice.getText();
            Pets[animals][3] = sellprice.getText();
            Pets[animals][4] = profitratios + "";
            breed.setText(" ");
            category.setText(" ");
            buyprice.setText(" ");
            sellprice.setText(" ");
            thelist.repaint(); //This is supposed to update the JTable in real time.
        }

        public void deleteanimal() {
            removeSelectedRows(thelist);
        }

        public void removeSelectedRows(JTable table) {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            int[] rows = table.getSelectedRows();
            for (int x = 0; x < rows.length; ++x) {
                model.removeRow(rows[x] - x);
            }
            for (int x = 0; x < animals; ++x) {
                if (Pets[x][0].equalsIgnoreCase(table.getValueAt(table.getSelectedRow(), 0) + "")) {
                    Pets[x][0] = null;
                    Pets[x][1] = null;
                    Pets[x][2] = null;
                    Pets[x][3] = null;
                    Pets[x][4] = null;
                }
            }
        }

        public void breedsort() {
            for (int x = 0; x < animals; ++x) {
            }
        }
    }
}
4

2 に答える 2

3

を初期化することはありませんButtonHandler。つまり、呼び出すたびにaddActionListener(handler)、本質的に呼び出していることになります。addActionListener(null)

何かに追加する前に、コンストラクターでハンドラーを初期化してみてください...

public knockoff() {
    super("The Peel Pet House Program"); //This just changes the name at the top there.
    handler = new ButtonHandler();

更新しました

さらに良いことに、代わりにAction APIを見てください。

于 2013-02-28T00:04:11.423 に答える
1

どこでも ButtonHandler をインスタンス化して登録していません...

于 2013-02-28T00:04:16.850 に答える