如何让这个程序多窗口化?

时间:2016-12-17 00:49:44

标签: java swing cardlayout

我正在学习Java,虽然我可以做一个简单的GUI程序。

我希望我的程序能够搜索读写文本文件。

我的所有组件都看起来我也想要它们。 我有4个面板p.nextsearchPanelreadPanelwritePanel它们显示在大型机{{​​1}}上。

现在我想做什么(这是我可以真正使用你们的帮助的地方)是如何使这个程序多窗口?

即如果我按下mainMenuPanel中的阅读按钮,它将显示JFrame。等其他按钮。

我希望我的问题能够理解并且你们可以帮助我。

这是我的代码:

mainMenuPanel

注意:到目前为止我尝试的是:因为所有4个面板都显示在框架上(框架有卡片布局)。我所做的是尝试向readPanel添加动作侦听器并将package MyGUIStuff; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.Scanner; public class multiWinDemo { public static void main (String args []) { /* Search Panel Ingredients * Label : File Name * TextField : look for ... * Button - Search * Button - Back * TextArea - Results found ... */ JLabel fileNameStorage1 = new JLabel ("File Name"); JTextField searchFor = new JTextField (20); JButton searchBtnPanel = new JButton ("Search:"); JButton backButton1 = new JButton ("Back"); JTextArea searchedArea = new JTextArea ("Text Area Here!\nName : Misael\nIf this worked\nthen i should\nbe able to\nuse JScrollPane\ncorrectly", 5,20); searchedArea.setEditable(false); //searchedArea.setPreferredSize(new Dimension(100,100) ); JScrollPane searchedAreaScoller = new JScrollPane (searchedArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); /* Write Panel Ingredients * Label : File Name * TextField : write this text to file ... * Button : Write * Button : Back */ JLabel fileNameStorage2 = new JLabel ("File Name:"); JTextField writeThis = new JTextField (20); JButton writeButton = new JButton ("Write"); JButton backButton2 = new JButton ("Back"); /* Read Panel Ingredients * Label : File Name * Button - Back * TextArea - File That I Chose */ JLabel fileNameStorage3 = new JLabel ("File Name Here"); JButton backButton3 = new JButton ("Back"); JTextArea readTextArea = new JTextArea ("Text Area Here" ,5 , 20); readTextArea.setEditable(false); JScrollPane readScrollPane = new JScrollPane (readTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); /* Main Menu Panel Ingredients * Label : "File Name" * TextField : File Absolute Path * Button - Browse * Button - Search * Button - Write * Button - Read * Button - Exit */ JLabel lbl1 = new JLabel ("File Name:"); JTextArea howTo = new JTextArea ("Search: Search a text file.\nWrite: Write to a text file.\nRead: Read a text file.", 3, 20); howTo.setEditable(false); JTextField filePath = new JTextField (20); JButton browseBtn = new JButton ("Browse"); JButton searchBtn = new JButton ("Search"); JButton writeBtn = new JButton ("Write"); JButton readBtn = new JButton ("Read"); JButton exitBtn = new JButton ("Exit"); //Search Panel JPanel searchPanel = new JPanel (); //searchPanel.setLayout(new GridLayout (3,9,5,5) ); searchPanel.setVisible(false); searchPanel.add(fileNameStorage1); searchPanel.add(searchFor); searchPanel.add(backButton1); searchPanel.add(searchBtnPanel); searchPanel.add(searchedAreaScoller); //Write Panel JPanel writePanel = new JPanel (); //writePanel.setLayout(new GridLayout (3,9,5,5) ); writePanel.setVisible(false); writePanel.add(fileNameStorage2); writePanel.add(writeThis); writePanel.add(backButton2); writePanel.add(writeButton); //Read Panel JPanel readPanel = new JPanel (); //readPanel.setLayout(new GridLayout (3,9,5,5) ); readPanel.setVisible(false); readPanel.add(fileNameStorage3); //readPanel.add(readTextArea); readPanel.add(readScrollPane); readPanel.add(backButton3); //Main Menu Panel JPanel blank1 = new JPanel (); JPanel mainMenuPanel = new JPanel (); mainMenuPanel.setVisible(true); //mainMenuPanel.setLayout(new GridLayout (3,9,5,5) ); mainMenuPanel.add(lbl1); mainMenuPanel.add(filePath); mainMenuPanel.add(browseBtn); mainMenuPanel.add(searchBtn); mainMenuPanel.add(writeBtn); mainMenuPanel.add(readBtn); mainMenuPanel.add(howTo); mainMenuPanel.add(exitBtn); //Program Frame JFrame mainFrame = new JFrame ("File Control"); mainFrame.setSize(300,235); mainFrame.setLayout(new CardLayout() ); mainFrame.setLocationRelativeTo(null); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setVisible(true); mainFrame.add(mainMenuPanel); mainMenuPanel.add(searchPanel); mainMenuPanel.add(writePanel); mainMenuPanel.add(readPanel); } } Visible false和searchBtn设置为true,但没有任何反应。如何让它多窗口?

2 个答案:

答案 0 :(得分:0)

您可以将water添加到cup,然后添加ActionListener,以打开其他GUI窗口。 如果您需要文档,可以在此处找到 - > event handler

答案 1 :(得分:-2)

从我正在阅读的内容中,当您按下按钮时,您希望其他面板启动。有多种方法可以做到这一点,根据我的经验,你想要在你的按钮上使用actionListener。以下是我基于简单金融计算器编写的一些代码,您可以将其用作参考。请注意,它使用 Combobox 并调用getselecteditems()方法从主面板上的组合框中获取每个选项。

然后我使用add()添加面板并删除all()和remove()以删除它。

Proposal.proponent(user)
# => returns a list of proposals for the specific user

还有很多其他方法可以做到这一点,这可能不是您的最佳方式。有一个叫做actionCommand和source的东西,它可以获得被调用的实际命令,“你可以用按钮来使用它。”

像这样......

proposal = Proposal.find_by(...)
proposal.user.proposals

在此示例中,我单击的按钮的名称是 Calc Simple Interest