【发布时间】:2015-03-20 02:32:31
【问题描述】:
我正在开发一个使用 JTable 来捕获用户输入的 Java 应用程序。用户必须打印表格中捕获的数据。 我希望用户打开多个文档来处理。我是这样实现的:我有我的 JFrame 作为我的主窗口;在这个 JFrame 上,我添加了 JTabbedPane,以便用户可以在文件、设置和工具之间切换。单击新文件后,用户将被带到 JTabbedPane(位于 JFrame 的中心)使用 JTable 进行输入。我希望下次用户单击新文件时,将一个新的 JPanel 添加到 JTabbledPane 中;但是这个新的 JPanel 还应该包含用于输入的 JTable。每次用户创建新文件时,这种行为都应该继续。这显示在我上传的图片中。(请原谅画得不好)。
我通过这段代码实现了这一点:
public final class QuotPane {
//components to be used
JFrame frame;
JTabbedPane tabbedPane,tablePane;
JPanel quotPane, topPane, pane1, pane2, pane3,tablePanel;
JSeparator sep;
JButton newFile;
QuotPane() {
this.createQuotPane();
this.createGUI();
ButtonActionListener lits= new ButtonActionListener();
newFile.addActionListener(lits);
}
public void createGUI() {
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Create Quot", quotPane);
frame = new JFrame("Qout Interface");
frame.setSize(new Dimension(700, 650));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//adding the tabbed pane to the frmae
frame.add(tabbedPane, BorderLayout.NORTH);
}
public void createQuotPane() {
quotPane = new JPanel();
quotPane.setLayout(new BorderLayout());
//creating the top pane
topPane = new JPanel();
topPane.setPreferredSize(new Dimension(650, 145));
topPane.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.lightGray, Color.lightGray, Color.white, Color.orange));
topPane.setLayout(new MigLayout());
//add the top pane on the quot panel
quotPane.add(topPane, BorderLayout.NORTH);
//adding the panes on the top pane
this.createPanels();
topPane.add(pane1);
topPane.add(pane2);
topPane.add(pane3);
}
//a method to create panes for options
public void createPanels() {
pane1 = new JPanel();
pane1.setPreferredSize(new Dimension(200, 140));
pane1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
//lets set the icons then
pane1.setLayout(new MigLayout());
Icon fileIcon = new ImageIcon(this.getClass().getResource("/images/fil.jpg"));
newFile = new JButton(fileIcon);
pane1.add(new JLabel("New File"), "wrap");
pane1.add(newFile,"width 20!");
pane2 = new JPanel();
pane2.setPreferredSize(new Dimension(200, 140));
pane2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
pane3 = new JPanel();
pane3.setPreferredSize(new Dimension(200, 140));
pane3.setMaximumSize(new Dimension(300, 140));
pane3.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.BLUE, Color.lightGray, Color.white, Color.orange));
}
public static void main(String[] args) {
QuotPane qp = new QuotPane();
}
public void createTablePane(){
tablePanel= new JPanel();
}
class ButtonActionListener implements ActionListener{
protected int count=0;
@Override
public void actionPerformed(ActionEvent e) {
if(count==0){
if(e.getSource().equals(QuotPane.this.newFile)){
tablePane=new JTabbedPane();
QuotPane.this.createTablePane();
tablePane.addTab("New Qout", tablePanel);
frame.add(tablePane,BorderLayout.CENTER);
count ++;
}
}else if(count>0)
{
tablePane.add("New Quote",new JPanel());
}}}}
我在这里面临的挑战是我无法将我的 JTable 添加到运行时创建的每个面板中。我已经尝试过:tablePane.add("New Quote",myCreatedBeforePanleWithTable) 但它覆盖了前一个选项卡。
这是我想要的东西的图片。我阅读了有关 JDeskTopPane 和 JInternalFrame 的信息,但不知道如何让它按我想要的方式工作。
如何让它按照我想要的方式工作,如图所示?
【问题讨论】:
-
“我无法将我的 JTable 添加到运行时创建的每个面板中” 听起来确实像应用程序。应该有 一个 表、no 选项卡和 3 个用于 3 个操作的按钮。否则,请在任意数量的
JTable组件中使用相同的TableModel。 -
@AndrewThompson 如何为运行时创建的每个面板使用一个表格模型?示例代码和链接会有所帮助。
-
@AndrewThompson 但我发布了在用户点击“新建文件”按钮时执行的必要代码。我认为这会有所帮助。
-
@AndrewThompson 好的,那我来补充吧。