【问题标题】:Multiple Buttons?多个按钮?
【发布时间】:2014-12-13 02:38:32
【问题描述】:

我无法让多个按钮在 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");
    } 
}

【问题讨论】:

    标签: java swing jbutton


    【解决方案1】:

    您只向一个按钮添加了一个 ActionListener:

    btnAdd.addActionListener(this);
    

    因此,只有一个按钮 btnAdd 可以工作,因为这些按钮不会靠魔法工作,并且都需要向它们添加一个 ActionListener 才能使它们具有任何功能。即,

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

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

    btnFill.addActionListener(new ActionListener() {
    
        public void actionPerformed(ActionEvent e) {
            // do some fill stuff here
        }
    
    });
    

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

    【讨论】:

    • 哇,你是对的。我不敢相信我忘了添加第二个动作监听器。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 2013-04-06
    • 2015-01-12
    • 2012-07-02
    • 1970-01-01
    • 2019-12-29
    • 2021-08-10
    相关资源
    最近更新 更多