java - 按钮按下事件

标签 java swing jbutton jfilechooser

我做了一个简单的文件菜单。单击文件后,您会看到一个名为“新建”的子菜单。单击时会弹出一个对话框。对话框上有一个用户输入文本字段和一个按钮。我对如何创建一个事件感到困惑,当您单击按钮时,文件选择器就会出现。

这是我的代码:

--NewFileBox.java--

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class NewFileBox implements ActionListener{

JTextField projectName = new JTextField();
JButton saveFile = new JButton();

public NewFileBox(){
    saveFile.addActionListener(this);
}

final JComponent[] inputs = new JComponent[]{
        new JLabel("Project Name"),
        projectName,
        new JButton("Save")
};

public void actionPerformed(ActionEvent E){
    if(E.getSource() == saveFile){
        JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(null);
    }
}
}

--Window.java--

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class Window implements ActionListener{

//Create SubMenus
private JMenuItem item1 = new JMenuItem("New...");
private JMenuItem item2 = new JMenuItem("Open");
private JMenuItem item3 = new JMenuItem("Exit");

//Import Class
public NewFileBox nfb = new NewFileBox();

public Window(int w, int h, String title){
    JFrame f = new JFrame(title);
    JMenuBar menubar = new JMenuBar();

    //Menu Options
    JMenu menu = new JMenu("File");

    //Add ActionListener to SubMenu
    item1.addActionListener(this);
    item2.addActionListener(this);
    item3.addActionListener(this);


    menubar.add(menu);
    f.setJMenuBar(menubar);

    //Add SubMenus to File
    menu.add(item1);
    menu.add(item2);
    menu.add(item3);

    f.setSize(w, h);
    f.setVisible(true);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent E){
    //FILE
    if(E.getSource() == item1){     //New
        JOptionPane.showMessageDialog(null, nfb.inputs);
    }
    if(E.getSource() == item2){     //Open
        JFileChooser fc = new JFileChooser("Open A File");
        fc.showOpenDialog(null);
    }
    if(E.getSource() == item3){     //Exit
        System.exit(0);
    }
}
}

--Game.java--

public class Game {

public static void main(String args[]){
    new Window(640, 480, "Autumn Engine");
}
}

此外,如果知道如何创建用户在按下创建按钮(我稍后将创建该按钮)时保存的新文件以及用户输入其项目名称时的新文件,那就太好了;将名称添加到 FileChooser 中的文件名中。

我知道这是非常多的信息。如果您有任何疑问,请告诉我。

谢谢。

最佳答案

同样,您有两个 JButton,其中一个已添加 ActionListener 但不执行任何操作,另一个是您尝试使用的 JButton,但未添加 ActionListener。解决方案:去掉第二个按钮。

JButton saveFile = new JButton("Save"); // Put text into the button!

public NewFileBox(){
    saveFile.addActionListener(this);
}

final JComponent[] inputs = new JComponent[]{
        new JLabel("Project Name"),
        projectName,
        // new JButton("Save")  // Get rid of this!
        saveFile  // add this!
};

关于java - 按钮按下事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28203933/

相关文章:

java - 按钮消失,照片出现在其位置

java - JFrame 中的按钮,访问 AbstractTableModel 中的方法

java - 跨字段 Java Bean 验证不验证来自 JSF 的字段

java - 以图像为背景的 Jbutton 无法单击

java - 更改 JComponent 上的文本

java - 如何对表列求和并将总值设置为文本字段?

java - 如何获取当前状态对应的按钮图标?

java - 保存到内部存储文件-Android

java - 如何在点击android时全屏显示图像?

java - 矩阵乘法java 1.8