Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用e.getsource打开新窗口(JFrame)_Java_Swing_Jframe - Fatal编程技术网

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

Java 使用e.getsource打开新窗口(JFrame),java,swing,jframe,Java,Swing,Jframe,我在打开新窗口时遇到问题。如果我注释掉If(例如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("Tahom

我在打开新窗口时遇到问题。如果我注释掉If(例如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

    }




    }

根据按钮文本进行检查

((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")) {
        ...
    }
}

这对我有用。我必须匹配字符串,因为e.getsource无法工作

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);
            }

您在哪里添加了
ActionListener
?您是否在任何地方调用了btnAddBook.addActionListener()any where?到btnAddBook按钮如果您仅将侦听器与一个按钮关联,为什么需要检查源代码?请在添加它的地方共享该部分代码。尝试此部分
System.out.print(source.getClass().getName())查找源对象类名。我已经发布了它。你的回答中还有一个问题。如果对除
JButton
以外的其他组件使用相同的操作侦听器,则您没有检查可能会产生问题的
JButton
实例。您在做什么?不要使用
==
来比较字符串。请使用
equals()
来比较字符串内容。