从按钮操作调用变量并在出现错误时显示错误消息

Call a variable from a button action & display an error message in case of one

已编辑:

当我单击 GUI 的“开始”按钮时,我希望执行他部分中的代码并在完成后关闭,或者打印一条错误消息(如果有来自控制台的错误消息)。

我面临 2 个需要解决的问题:

这是我所做的:

import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.Action;

public class GuiTraduttore extends JFrame implements ActionListener {

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GuiTraduttore frame = new GuiTraduttore();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


/**
 * Create the frame.
 */
//Dichiarazione button
private final Action action_btnNewButton_1 = new SwingAction_btnNewButton_1();
private final Action action_btnNewButton_1_2 = new SwingAction_btnNewButton_1_2();
private final Action action_btnSalvaIn = new SwingAction_btnSalvaIn();
private final Action action_btnStart = new SwingAction_btnStart();


public GuiTraduttore() {
    setIconImage(Toolkit.getDefaultToolkit().getImage(GuiTraduttore.class.getResource("/icon/6.png")));
    setTitle("Traduttore");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 629, 483);
    getContentPane().setLayout(null);
    
    JLabel lblNewLabel = new JLabel("Path Scope.xml :");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblNewLabel.setBounds(25, 201, 557, 26);
    getContentPane().add(lblNewLabel);
    
    JLabel lblToolchainIit = new JLabel("Location of the files: ");
    lblToolchainIit.setFont(new Font("Consolas", Font.BOLD, 22));
    lblToolchainIit.setBounds(25, 7, 577, 26);
    getContentPane().add(lblToolchainIit);
    
    JLabel lblPathBtimplementation = new JLabel("Path bt-implementation :");
    lblPathBtimplementation.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblPathBtimplementation.setBounds(25, 147, 557, 26);
    getContentPane().add(lblPathBtimplementation);
    
    JLabel lblpathDestinazione = new JLabel("Path Destination final file Scope.xml  :");
    lblpathDestinazione.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblpathDestinazione.setBounds(25, 346, 387, 26);
    getContentPane().add(lblpathDestinazione);
    
    JTextArea pathScope1 = new JTextArea();
    pathScope1.setEditable(false);
    pathScope1.setBounds(25, 228, 513, 22);
    getContentPane().add(pathScope1);
    
    JTextArea textArea_7 = new JTextArea();
    textArea_7.setEditable(false);
    textArea_7.setBounds(25, 374, 557, 22);
    getContentPane().add(textArea_7);
    
    JButton btnNewButton = new JButton("START");
    btnNewButton.setAction(action_btnStart);
    btnNewButton.setFont(new Font("Consolas", Font.PLAIN, 14));
    btnNewButton.setBounds(225, 407, 160, 26);
    getContentPane().add(btnNewButton);
    
    JButton btnNewButton_1 = new JButton("...");
    btnNewButton_1.setAction(action_btnNewButton_1);
    btnNewButton_1.setBounds(543, 173, 39, 26);
    getContentPane().add(btnNewButton_1);

    
    JButton btnNewButton_1_2 = new JButton("...");
    btnNewButton_1_2.setAction(action_btnNewButton_1_2);
    btnNewButton_1_2.setBounds(543, 227, 39, 26);
    getContentPane().add(btnNewButton_1_2);
    
    JButton btnSalvaIn = new JButton("Save to : ");
    btnSalvaIn.setAction(action_btnSalvaIn);
    btnSalvaIn.setFont(new Font("Consolas", Font.PLAIN, 14));
    btnSalvaIn.setBounds(422, 342, 160, 26);
    getContentPane().add(btnSalvaIn);
    
    JTextArea txtrInsertTheRight = new JTextArea();
    txtrInsertTheRight.setFont(new Font("Consolas", Font.PLAIN, 18));
    txtrInsertTheRight.setWrapStyleWord(true);
    txtrInsertTheRight.setLineWrap(true);
    txtrInsertTheRight.setEditable(false);
    txtrInsertTheRight.setTabSize(0);
    txtrInsertTheRight.setText("Insert the right paths of the location for the folder \"bt-implementation\" (the folder that contains all the files of the project with IIT standards files to be parsed in only one file) and the Scope.xml.");
    txtrInsertTheRight.setBounds(26, 44, 556, 92);
    getContentPane().add(txtrInsertTheRight);
    
    JTextArea txtrInsertTheRight_1 = new JTextArea();
    txtrInsertTheRight_1.setWrapStyleWord(true);
    txtrInsertTheRight_1.setText("Insert the right paths of the location where to save the final Scope.xml file and click on the start button.");
    txtrInsertTheRight_1.setTabSize(0);
    txtrInsertTheRight_1.setLineWrap(true);
    txtrInsertTheRight_1.setFont(new Font("Consolas", Font.PLAIN, 18));
    txtrInsertTheRight_1.setEditable(false);
    txtrInsertTheRight_1.setBounds(25, 261, 556, 63);
    getContentPane().add(txtrInsertTheRight_1);
    
    JTextArea pathBtImplementation = new JTextArea();
    pathBtImplementation.setText("gfbgn");
    pathBtImplementation.setEditable(false);
    pathBtImplementation.setBounds(25, 174, 513, 22);
    getContentPane().add(pathBtImplementation);
}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    
}

//Action JButton btnNewButton_1
private class SwingAction_btnNewButton_1 extends AbstractAction {
    public SwingAction_btnNewButton_1() {
        putValue(NAME, "...");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
    
            JFileChooser fileChooser1 = new JFileChooser();
            fileChooser1.setCurrentDirectory(new File("."));
            //seleziona solo cartelle
            fileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
            //prende i file da aprire 0 / 1
            int risposta = fileChooser1.showOpenDialog(null);
            if (risposta == JFileChooser.APPROVE_OPTION) {
                String cartella = new String (fileChooser1.getSelectedFile().getAbsolutePath());
                System.out.println(cartella);
            }
        
    }
}

//JButton btnNewButton_1_2
private class SwingAction_btnNewButton_1_2 extends AbstractAction {
    public SwingAction_btnNewButton_1_2() {
        putValue(NAME, "...");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        
        JFileChooser fileChooser2 = new JFileChooser();
        fileChooser2.setCurrentDirectory(new File("."));
        //seleziona solo cartelle
        fileChooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
        //prende i file da aprire 0 / 1
        int risposta = fileChooser2.showOpenDialog(null);
        if (risposta == JFileChooser.APPROVE_OPTION) {
            String scopeCartella = new String (fileChooser2.getSelectedFile().getAbsolutePath());
            System.out.println(scopeCartella);
        }
        
    }
}

//JButton btnSalvaIn
private class SwingAction_btnSalvaIn extends AbstractAction {
    public SwingAction_btnSalvaIn() {
        putValue(NAME, "Save to :");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        
        JFileChooser fileChooserSave = new JFileChooser();
        fileChooserSave.setCurrentDirectory(new File("."));
        //seleziona solo cartelle
        fileChooserSave.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
        //prende i file da aprire 0 / 1
        int risposta = fileChooserSave.showSaveDialog(null);
        if (risposta == JFileChooser.APPROVE_OPTION) {
            String scopeFinal = new String (fileChooserSave.getSelectedFile().getAbsolutePath());
            System.out.println(scopeFinal);
        }
        
    }
}

//button action start


private class SwingAction_btnStart extends AbstractAction {
    public SwingAction_btnStart() {
        putValue(NAME, "START");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        
        //button action code where to pass the 3 paths
        // 1) cartella 2) scopeCartella 3) scopeFinal
        
    }
}
}

你需要做的是利用“依赖注入”。

也就是说,您需要创建某种可以在你们之间共享的“模型”Action,它提供了“设置”他们控制的属性和“获取”他们控制的属性的方法他们需要。

现在,您可以使用多个 interface 来进一步限制每个 Action 可以做什么(因为并非所有操作都需要访问所有功能),但我会把它留到你.

现在从一个或多个 interfaces

开始
public interface Model {
    public void setSourceFile(File file);
    public void setDestinationPath(File file);
    
    public File getSourceFile();
    public File getDestinationPath();
}

也许可以通过提供 abstract 操作来填充一些常见功能来减少代码重复...

private abstract class ModelAction extends AbstractAction {
    
    private Model model;

    public ModelAction(Model model) {
        this.model = model;
    }

    public Model getModel() {
        return model;
    }
    
}

然后填写需要的需求...

private class SwingAction_btnNewButton_1 extends ModelAction {
    
    public SwingAction_btnNewButton_1(Model model) {
        super(model);
        putValue(NAME, "...");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }

    public void actionPerformed(ActionEvent e) {

        JFileChooser fileChooser1 = new JFileChooser();
        fileChooser1.setCurrentDirectory(new File("."));
        //seleziona solo cartelle
        fileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //prende i file da aprire 0 / 1
        int risposta = fileChooser1.showOpenDialog(null);
        if (risposta == JFileChooser.APPROVE_OPTION) {
            getModel().setSourceFile(fileChooser1.getSelectedFile());
        }

    }
}

//J按钮btnNewButton_1_2 private class SwingAction_btnNewButton_1_2 extends ModelAction {

    public SwingAction_btnNewButton_1_2(Model model) {
        super(model);
        putValue(NAME, "...");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }

    public void actionPerformed(ActionEvent e) {

        JFileChooser fileChooser2 = new JFileChooser();
        fileChooser2.setCurrentDirectory(new File("."));
        //seleziona solo cartelle
        fileChooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //prende i file da aprire 0 / 1
        int risposta = fileChooser2.showOpenDialog(null);
        if (risposta == JFileChooser.APPROVE_OPTION) {
            getModel().setDestinationPath(fileChooser2.getSelectedFile());
        }

    }
}

//JButton btnSalvaIn private class SwingAction_btnSalvaIn extends ModelAction {

    public SwingAction_btnSalvaIn() {
        putValue(NAME, "Save to :");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }

    public void actionPerformed(ActionEvent e) {

        JFileChooser fileChooserSave = new JFileChooser();
        fileChooserSave.setCurrentDirectory(new File("."));
        //seleziona solo cartelle
        fileChooserSave.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //prende i file da aprire 0 / 1
        int risposta = fileChooserSave.showSaveDialog(null);
        if (risposta == JFileChooser.APPROVE_OPTION) {
            getModel().setDestinationPath(fileChooserSave.getSelectedFile());
        }

    }
}

//button action start
private class SwingAction_btnStart extends ModelAction {

    public SwingAction_btnStart(Model model) {
        super(model);
        putValue(NAME, "START");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }

    public void actionPerformed(ActionEvent e) {
        File sourceFile = getModel().getSourceFile();
        File destinationPath = getModel().getDestinationPath();
        //button action code where to pass the 3 paths
        // 1) cartella 2) scopeCartella 3) scopeFinal
    }
}

nb:我无法理解你的代码,所以我猜测每个 Action 在做什么,所以你需要填写这些细节

您可能还会发现:

  • How do you dynamically compile and load external java classes?
  • How to compile and run inside the java program
  • how to get the output of a java program?

有些用处

null 布局

而且因为它们是......代码中的一个完全痛苦。 null 布局是一个糟糕的主意,它会不断地回来咬你。相反,花时间和精力学习如何使用 Swing 中可用的布局功能。

有关详细信息,请参阅 Laying Out Components Within a Container