多个按钮?

时间:2014-12-13 02:38:32

标签: java swing jbutton

我无法使用多个按钮在Java中工作。现在我有2个按钮,它们都应该做点什么。 “填充”按钮应该使用我在“Io”类中的“填充”方法中设置的内容填充前3个字段“标题”“导演”和“年份”。然后,“添加”按钮应该采用前3个字段中的内容并将它们复制到底部的3个文本区域中。 “保存”按钮现在没有任何功能。但是,出于某种原因,我只能让程序识别出第一个按钮。如果我在'If'语句中切换按钮并将“Add”移动到'else if',反之亦然,它仍然只能识别“Add”按钮。无论我尝试什么,我似乎无法让它执行两个按钮。如果有人能指出我正确的方向,任何帮助都会很棒!先感谢您! :)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class database extends Io implements ActionListener{
    //frame
    private JFrame window = new JFrame("Movie Database");

    //Buttons
    public JButton btnAdd = new JButton("Add");
    public JButton btnFill = new JButton("Fill");
    public JButton btnSave = new JButton("Save");

    //Label
    private JLabel lblTitle = new JLabel("Movie Title:");
    private JLabel lblDir = new JLabel("Director:");
    private JLabel lblYear = new JLabel("Year:");

    //Panel
    private Panel pnlNorth = new Panel();
    private Panel pnlSouth = new Panel();
    private Panel pnlCenter = new Panel();

    public void init(){
        //set main window
        window.setLayout(new BorderLayout());

        //add JLabel
        window.add(pnlNorth,BorderLayout.NORTH);
        window.add(pnlCenter,BorderLayout.CENTER);
        window.add(pnlSouth,BorderLayout.SOUTH);

        //set panels to gridframe
        pnlNorth.setLayout(new GridLayout(1,3));
        pnlCenter.setLayout(new GridLayout(2,3));
        pnlSouth.setLayout(new GridLayout(1,3));

        //layout center panel
        pnlNorth.add(lblTitle);
        pnlNorth.add(lblDir);
        pnlNorth.add(lblYear);

        //layout center panel
        pnlCenter.add(inTitle);
        pnlCenter.add(inDir);
        pnlCenter.add(inYear);
        pnlCenter.add(btnAdd);
        pnlCenter.add(btnFill);
        pnlCenter.add(btnSave);

        //layout south panel
        pnlSouth.add(outTitle);
        pnlSouth.add(outDir);
        pnlSouth.add(outYear);

        //actionlistener
        btnAdd.addActionListener(this);

        //generic frame operation
        window.pack();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    } //end init

    public database(){
        init();
    }

    public void actionPerformed(ActionEvent a){
        Object source = a.getSource();
        if(source==btnAdd){
            set();
        } else if(source==btnFill){
            fill();
        }           
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new database();
    } // end main
} //end class

class Io{
    String title;
    String dir;
    String year;

    //JText
    public JTextField inTitle = new JTextField("",20);
    public JTextField inDir = new JTextField("",20);
    public JTextField inYear = new JTextField("",20);
    public JTextArea outTitle = new JTextArea("",20,20);
    public JTextArea outDir = new JTextArea("",20,20);
    public JTextArea outYear = new JTextArea("",20,20);

    public void fill(){
        inTitle.setText("Interstellar");
        inDir.setText("Christopher Nolan");
        inYear.setText("2014");
    }


    public void set(){
        outTitle.append(inTitle.getText() + "\n");
        outDir.append(inDir.getText() + "\n");
        outYear.append(inYear.getText() + "\n");
    } 
}

1 个答案:

答案 0 :(得分:3)

您只在一个按钮中添加了一个ActionListener:

btnAdd.addActionListener(this);

因此,只有一个按钮btnAdd可以工作,因为按钮不会通过魔法工作,并且都需要将ActionListener添加到它们中,以便它们具有任何功能。即,

btnFill.addActionListener(....something here....);

我自己,如果可能的话,我更喜欢使用匿名的ActionListeners,比如

btnFill.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        // do some fill stuff here
    }

});

这表明你在没有先阅读教程的情况下尝试使用JButton,我建议不要这样做。请查看它们,因为它们非常有帮助:How to use Buttons