JAVA GUI - 一帧中的两个帧

时间:2015-01-08 10:39:03

标签: java swing pdf user-interface frame

我尝试开发一个正在读取目录的应用程序,并为该目录中的每个地图添加一个按钮=步骤1.在步骤2中,用户点击按钮以显示该地图中的所有文档。这些(仅PDF)文档也显示为按钮,如果用户点击按钮,PDF将在查看器中打开。

我定义了两个类,依此类推。但现在我的布局有问题。步骤1中的按钮位于一帧中,而步骤2中的按钮位于另一帧中。我想有一个框架,所以在左侧是步骤1的动态按钮,在右侧我想要步骤2的按钮,或者按钮PDf也可以显示为tumblnail。如果我点击左侧的按钮,右侧将会更新。

有人可以帮助我吗?

   package infopad;

   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;

   import javax.swing.JButton;
   import javax.swing.JFrame;

   import java.io.File;


   public class infopadUI extends JFrame implements ActionListener {

   public infopadUI() {
    File directory = null;

    //Öffne Hauptverzeichnis
    directory = new File("c:/Produktordner");

    int numberfiles = 0;

    // Inhalt von directory
    File[] files = directory.listFiles();

         //Number of Products in directory
    numberfiles = files.length;

    setSize(600, 600);
    setLocationRelativeTo(null);
    setLayout(new GridLayout(5, 6));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    for (int i = 1; i < numberfiles; i++) {
        //Name für Button aus Verzeichnisliste holen
        File file = files[i];

        JButton button = new JButton(file.getName());
        button.setActionCommand(file.getName());
        button.addActionListener(this);
        add(button);

           }
       }

       public static void main(String[] args) {


    new infopadUI().setVisible(true);



       }

       public void actionPerformed(ActionEvent e) {
    String  productnumber = e.getActionCommand();

    product p1 = new product(productnumber);       

       }



   }

   package infopad;

   import java.awt.Desktop;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;

   import javax.swing.JButton;
   import javax.swing.JFrame;

   import java.io.File;




   public class product extends JFrame implements ActionListener{

public product(String productnumber)
{       
    String map;
    String doc;


    map = ("c:/Produktordner/")+productnumber;


    File directory = null;

    //Öffne Hauptverzeichnis
    directory = new File(map);

    int numberfiles = 0;

    // Inhalt von directory
    File[] files = directory.listFiles();

  //Number of Products in directory
    numberfiles = files.length;

    setSize(600, 600);
    setLocationRelativeTo(null);
    setLayout(new GridLayout(5, 6));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    for (int i = 1; i < numberfiles; i++) {
        //Name für Button aus Verzeichnisliste holen
        File file = files[i];

        JButton button = new JButton(file.getName());
        doc = map+"/"+file.getName();



        button.setActionCommand(doc);
        button.addActionListener(this);
        add(button);

    }
    setVisible(true);

}

public static void main(String[] args) {




}

public void actionPerformed(ActionEvent ae) {
    String  doc = ae.getActionCommand();

   try {
              Desktop.getDesktop().open(new File(doc));
    } catch (Exception e) {}

       }
   }

1 个答案:

答案 0 :(得分:0)

您可以在构造函数中将this实例从InfoPadUI传递到Product:而不是new infopadUI()....执行new infopadUI(this)....并在infopadUI中更改构造函数以接受JFrame,并使用JFrame按钮。 (摆脱infopadUI中的JFrame)

相关问题