【问题标题】:JAVA GUI - two frames in one frameJAVA GUI - 一帧两帧
【发布时间】:2015-01-08 10:39:02
【问题描述】:

我尝试开发一个正在读取目录的应用程序,并为该目录中的每个地图添加一个按钮 = 第 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) {}

       }
   }

【问题讨论】:

    标签: java swing pdf user-interface frame


    【解决方案1】:

    您可以在构造函数中将this 实例从InfoPadUI 传递给Product:而不是new infopadUI().... 执行new infopadUI(this).... 并在infopadUI 中更改构造函数以接受JFrame,并使用该JFrame把按钮放在上面。 (去掉 infopadUI 中的 JFrame)

    【讨论】:

    • 但是我有两个不同的部分,一个带有 step 1 infopadui 的按钮,一个带有 step2 产品的按钮。如果我不点击第 1 步的按钮,则在第 2 部分中不会显示任何按钮。我在想jsplitpane?
    • 是的,然后在 splitPane 中添加另一个 JPane 并将其传递给 infopadUI,然后让它将其按钮添加到该 JPane
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多