【发布时间】: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