使用e.getsource打开一个新窗口(JFrame)

时间:2014-04-08 19:39:35

标签: java swing jframe

我在打开新窗口时遇到问题。如果我注释掉if(e.getsource()== btnAddBook),则执行的操作将打开窗口。一旦我添加if语句并尝试打开窗口,就没有任何反应。

 /* this is the code that adds the button and the action listner

    JButton btnAddBook = new JButton("Add Book");
            btnAddBook.setFont(new Font("Tahoma", Font.BOLD, 8));
            btnAddBook.setBounds(10, 327, 86, 23);
            btnAddBook.addActionListener(this);
            getContentPane().add(btnAddBook);
    */

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if(source == btnAddBook){
    ChildWindowAdd child = new ChildWindowAdd(this);
    this.setVisible(true);
    child.setVisible(true);
    child.setSize(450,400);     
    child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }
  }
}

class ChildWindowAdd extends JFrame implements ActionListener {
        LibraryDatabase parent;
        Container c;
        JLabel lblTitle, lblaName, lblISBN, lblDate,lbl1,lbl5,lbl10;
        JTextField txtTitle, txtaName, txtISBN, txtDate;
        JButton btnSave;
        JSlider sRating;
        JCheckBox chkSci, chkFant, chkRomance, chkAction, chkThriller, chkHorror;


        public ChildWindowAdd(LibraryDatabase parent){
            this.parent = parent;
            getContentPane().setLayout(null);

            txtTitle = new JTextField();
            txtTitle.setBounds(46, 29, 146, 20);
            getContentPane().add(txtTitle);
            txtTitle.setColumns(10);

            JLabel lblTitle = new JLabel("Book Title");
            lblTitle.setFont(new Font("Tahoma", Font.PLAIN, 14));
            lblTitle.setBounds(36, 11, 72, 14);
            getContentPane().add(lblTitle);

            JLabel lblaName = new JLabel("Author Name");
            lblaName.setFont(new Font("Tahoma", Font.PLAIN, 14));
            lblaName.setBounds(36, 60, 89, 14);
            getContentPane().add(lblaName);

            txtaName = new JTextField();
            txtaName.setBounds(46, 85, 146, 20);
            getContentPane().add(txtaName);
            txtaName.setColumns(10);

            JLabel lblIsbnNumber = new JLabel("ISBN Number");
            lblIsbnNumber.setFont(new Font("Tahoma", Font.PLAIN, 14));
            lblIsbnNumber.setBounds(36, 116, 86, 14);
            getContentPane().add(lblIsbnNumber);

            txtISBN = new JTextField();
            txtISBN .setBounds(46, 143, 146, 20);
            getContentPane().add(txtISBN );
            txtISBN .setColumns(10);

            JLabel lblDate = new JLabel("Date Added yyyy/mm/dd");
            lblDate.setFont(new Font("Tahoma", Font.PLAIN, 14));
            lblDate.setBounds(36, 174, 177, 20);
            getContentPane().add(lblDate);

            txtDate = new JTextField();
            txtDate.setBounds(46, 199, 146, 20);
            getContentPane().add(txtDate);
            txtDate.setColumns(10);

            JPanel genreP = new JPanel();
            genreP.setBounds(239, 29, 158, 194);
            genreP.setBorder(new TitledBorder(null, "Genre's", TitledBorder.LEADING, TitledBorder.TOP, null, null));
            getContentPane().add(genreP);
            genreP.setLayout(null);

            final JCheckBox chkSci = new JCheckBox("Sci - Fi");
            chkSci.setBounds(23, 22, 97, 23);
            genreP.add(chkSci);

            final JCheckBox chkFant = new JCheckBox("Fantasy");
            chkFant.setBounds(23, 48, 97, 23);
            genreP.add(chkFant);

            final JCheckBox chkRomance = new JCheckBox("Romance");
            chkRomance.setBounds(23, 74, 97, 23);
            genreP.add(chkRomance);

            final JCheckBox chkAction = new JCheckBox("Action");
            chkAction.setBounds(23, 100, 97, 23);
            genreP.add(chkAction);

            final JCheckBox chkThriller = new JCheckBox("Thriller");
            chkThriller.setBounds(23, 126, 97, 23);
            genreP.add(chkThriller);

            final JCheckBox chkHorror = new JCheckBox("Horror");
            chkHorror.setBounds(23, 152, 97, 23);
            genreP.add(chkHorror);

            final JSlider sRating = new JSlider();
            sRating.setBounds(118, 280, 200, 26);
            getContentPane().add(sRating);

            JLabel lbl1 = new JLabel("1");
            lbl1.setBounds(118, 257, 7, 14);
            getContentPane().add(lbl1);

            JLabel lbl10 = new JLabel("10");
            lbl10.setBounds(301, 257, 17, 14);
            getContentPane().add(lbl10);

            JLabel lbl5 = new JLabel("5");
            lbl5.setBounds(214, 257, 7, 14);
            getContentPane().add(lbl5);

            JButton btnSave = new JButton("Save Book");
            btnSave.addActionListener(this);
            btnSave.setBounds(164, 317, 110, 23);
            getContentPane().add(btnSave);

            JLabel lblRating = new JLabel("Rate the book");
            lblRating.setFont(new Font("Tahoma", Font.PLAIN, 12));
            lblRating.setBounds(118, 226, 95, 20);
            getContentPane().add(lblRating);

            btnSave.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    PrintWriter fileWriter = null;
                    try {
                        fileWriter = new PrintWriter(new BufferedWriter(new FileWriter("c:\\temp\\database.dat", true)));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    fileWriter.println( txtTitle.getText()+" , "+txtaName.getText()+" , "+txtISBN.getText()+" , "+txtDate.getText()+" , "+String.valueOf(chkSci.isSelected())+" , "+String.valueOf(chkFant.isSelected())+" , "+String.valueOf(chkRomance.isSelected())+" , "+String.valueOf(chkAction.isSelected())+" , "+String.valueOf(chkThriller.isSelected())+" , "+String.valueOf(chkHorror.isSelected())+" , "+sRating.getValue());
                    fileWriter.close();
                }
            });
        }
        @Override
        public void actionPerformed(ActionEvent arg0) {
            parent.setVisible(true);
            this.dispose();
        }

    public void actionPerformed1(ActionEvent e) {
        // TODO Auto-generated method stub

    }




    }

2 个答案:

答案 0 :(得分:1)

根据按钮文字检查

((JButton)source).getText().equals("Add Book")

这里是完整的代码

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();

    if (source instanceof JButton && ((JButton) source).getText().equals("Add Book")) {
        ...
    }
}

答案 1 :(得分:0)

这对我有用。由于e.getsource不起作用,我必须匹配String。

public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();          
            String test = ((JButton)source).getText();

            if(test == "Add Book"){
            ChildWindowAdd child = new ChildWindowAdd(this);
            this.setVisible(true);
            child.setVisible(true);
            child.setSize(450,400);     
            child.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            }