简体   繁体   English

在JTabbedPane Java中添加JFileChooser

[英]adding a JFileChooser inside a JTabbedPane Java

I am a student and have been given some source code for a minesweeper game. 我是一名学生,并且已获得扫雷游戏的一些源代码。 We have a few different GUI elements to add the way we want to. 我们有一些不同的GUI元素来添加我们想要的方式。 I am trying to use JTabbedPane. 我正在尝试使用JTabbedPane。 I have the actual game showing in one tab, now I am trying to implement a JFileChooser in another tab. 我在一个选项卡中显示了实际的游戏,现在我试图在另一个选项卡中实现JFileChooser。 I have made a class called userNames: 我做了一个叫做userNames的类:

public userNames() {            
    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
}

and just put in a simple showOpenDialog(null); 然后放一个简单的showOpenDialog(null); to see if it works. 看看是否有效。 In the main, where the tabs are, I have added the file chooser: 在主要的选项卡中,我添加了文件选择器:

tp.addTab ("Saved", new userNames());

but this doesn't add it to the 'Saved' tab, it opens a whole new window. 但这不会将其添加到“已保存”标签中,而是会打开一个全新的窗口。 Can anyone tell me if what I am trying to do is even possible - add a file chooser inside a tab. 谁能告诉我我尝试做的事是否可行-在标签内添加文件选择器。

I hope I am explaining myself well enough. 我希望我能很好地解释自己。 :-) :-)

You can create a JPanel inside of that tab, create a JFileChooser object and then add that object to panel you've created for that tab. 您可以在该选项卡内部创建一个JPanel,创建一个JFileChooser对象,然后将该对象添加到为该选项卡创建的面板中。 So it should look like: 所以它应该看起来像:

JPanel panel = ...
tp.addTab(title, panel);
JFileChooser fc;
panel.add(fc);

in your usernames class you are creating a new fileChooser and opening the dialog, you aren't actually displaying it on a panel. 在用户名类中,您正在创建一个新的fileChooser并打开对话框,实际上并没有在面板上显示它。

you could try something like 你可以尝试像

public userNames() extends JPanel {

    public userNames() {
       add(chooser);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM