如何使用open_button类

时间:2017-11-24 12:37:30

标签: java swing jbutton jtextfield

目前,我学习Java gui,swing。 我想将Yam类中的j1('open'按钮)与Open_Button类的函数链接起来。 如果我在“textField”中写入文件名,这是Yam类中的全局变量,并按“打开”,则“TextArea”必须显示打开文件内容的输出。 但不知怎的,它不起作用。我认为open_button类的函数与Yam类中的“textField”没有关联。 但我真的找不到什么是错的。 (对不起我可怜的Engish ..;(我会告诉你完整的代码。我想跟随Minimal,Complete和Verifiable rull但我真的不知道哪里出错了部分..)

 Yam class )

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.io.File.*;
   import java.io.*;
   import java.nio.file.*;

   public class Yam extends JFrame {
   Container contentPane;
   JButton j1, j2, j3, j4, j5, j6;
   JTextField textField;
   JTextField textField_1; 
   TextArea textArea, textArea_1;

  public Yam() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  contentPane = getContentPane();
  contentPane.setLayout(null);

  textField = new JTextField();
  textField.setBounds(14, 12, 240, 24);
  contentPane.add(textField);
  textField.setColumns(20);

  j1 = new JButton("Open");
  j1.setBounds(294, 11, 105, 27);
  j1.addActionListener(new AB());
  contentPane.add(j1);

  textField_1 = new JTextField();
  textField_1.setBounds(14, 48, 240, 24);
  contentPane.add(textField_1);
  textField_1.setColumns(20);

  j2 = new JButton("Save");
  j2.setBounds(294, 47, 105, 27);
  contentPane.add(j2); 

  textArea = new TextArea();
  textArea.setBounds(10, 105, 405, 190);
  contentPane.add(textArea);

  j3 = new JButton("Compile");
  j3.setBounds(10, 319, 87, 25);
  j3.addActionListener(new AB());
  contentPane.add(j3);

  j4 = new JButton("Save Errors");
  j4.setBounds(111, 319, 87, 25);
  contentPane.add(j4);

  j5 = new JButton("Delete");
  j5.setBounds(217, 319, 87, 25);
  contentPane.add(j5);

  j6 = new JButton("Clear");
  j6.setBounds(321, 319, 87, 25);
  j6.addActionListener(new AB());
  contentPane.add(j6);

  textArea_1 = new TextArea();
  textArea_1.setBounds(10, 391, 405, 253);
  contentPane.add(textArea_1);

  setSize(506, 698);
  setVisible(true); }



 class AB implements ActionListener {
 private TextArea args;

  public void actionPerformed(ActionEvent e) {
  Object source = e.getSource();
  CmdClass cmd = new CmdClass();
  Open_button open = new Open_button();
  JFrame f = new JFrame("what");

     if(source == j1) {
             }
     else if(source == j2) {
         }

     else if(source == j3) { 
        if(cmd.main(args))
           textArea_1.append("Compile Success\n");
     }
     else if(source == j4) { }
     else if(source == j5) { }
     else {
        textField.setText(" ");
        textField_1.setText(" ");
        textArea.setText(" ");
        textArea_1.setText(" "); }}}



     public static void main(String[] args) {
      new Yam();
       }




       public static String getFileName() {
  // TODO Auto-generated method stub
    return null;}}
     Open_button class)

import java.io.*;
import java.nio.file.*;
import java.io.File;

public class Open_button {
    Path Path;

    public void main(String Str_paths){
        Yam y = new Yam();

        Str_paths = y.textField.getText();
        Path = FileSystems.getDefault().getPath(Str_paths);
        File file = Path.toFile();

        if(Path.toFile() != null)
            y.textArea.append("file exist!");       
        else {
            y.textArea_1.append("file not exist");  
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(Str_paths));
                String line;
                while ((line = br.readLine()) != null) {
                    y.textArea.append(line);
                }
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace(); 
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为你想出于某种原因打开并读取在jtextfield上指定文件路径的文件;最简单的方法是:

JButton openButton = new JButton("Open File");              
openButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
         String thePath = textfield.getText();
         File theFile = new File(thePath);
         ...read..write..
     }
});

我会直接在Yam类声明这个;

从我所看到的,你的代码有两个问题:第一,有两个Yam(y)实例(在OpenButton类中声明的实例与上面的实例不同),第二,你假设文件是可读并始终位于Filesystem视图...我认为您还应该考虑JFileChooser对话框。 此致,André