【问题标题】:How can I make this program multi windowed?我怎样才能使这个程序多窗口?
【发布时间】:2016-12-17 00:49:44
【问题描述】:

我正在学习 Java,但我可以制作一个简单的 GUI 程序。

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

我的所有组件也都按照我想要的方式进行。 我有 4 个面板 searchPanelreadPanelwritePanelmainMenuPanel,它们显示在大型机上 JFrame

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

即如果我按下mainMenuPanel 中的读取按钮,它将显示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);


    }
}

注意:到目前为止,我尝试的是:因为所有 4 个面板都显示在框架上(框架具有卡片布局)。我所做的是尝试将动作侦听器添加到searchBtn 并将mainMenuPanel Visible 设置为false 和searchPanel true,但什么也没发生。如何让它多窗口化?

【问题讨论】:

  • 不要。不要向用户扔很多窗口,除非你想要一个非常恼火的用户。而是使用 CardLayout 交换视图而不是窗口。仅将第二个窗口用于必要的对话框,例如输入对话框和输入验证对话框。请阅读并牢记:The Use of Multiple JFrames, Good/Bad Practice?
  • 另外,在担心添加窗口之前,您需要重构您的程序。当前,您在静态 main 方法中填充了一个单独的上帝类,该程序在与其他类的交互及其增强方面将受到极大限制。而是创建有意义的面向对象兼容类,具有低耦合和高内聚,可以独立测试。
  • 1) "(JFrame 有卡片布局)。我所做的是尝试将 actionListeners 添加到 searchBtn 并将 mainMenuPanel Visible 设置为 false 并将 searchPanel 设置为 true,但什么也没发生。” i> 该代码没有正确使用CardLayout。有关工作示例,请参阅 this answer,或通过 tutorial。 2) mainFrame.setSize(300,235); 这是对所需大小的猜测。使用pack() 获得正确的大小。 3)JTextField filePath = new JTextField (20); ..
  • .. 为了用户(以及他们的理智),提供JFileChooser 用于打开或保存文件!

标签: java swing cardlayout


【解决方案1】:

您可以将ActionListener 添加到JButton,然后添加一个ActionPerformed,以打开另一个GUI 窗口。 如果您需要文档,可以在这里找到 --> event handler

【讨论】:

    【解决方案2】:

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

    然后我使用 add() 添加面板并使用 removeall() 和 remove() 将其删除。

      private class financeBoxListener implements ActionListener
               {
                  public void actionPerformed(ActionEvent e)
                  {
    
    
                      String selection = (String) financeBox.getSelectedItem();
                      if(selection.equals("Present value")){
                          //financeFinderPanel.removeAll();
    
    
    
    
                          presentValuePanel();
                          add(presentValuePanel, BorderLayout.NORTH);
    
    
                          financeFinderPanel.removeAll();
                          remove(financeFinderPanel);
                          pack();
    
                      }
    
    
    
                     else if(selection.equals("Simple interest")){
    
    
    
                         simpleInterestPanel();
                         add(simpleInterestPanel, BorderLayout.SOUTH);
    
                         financeFinderPanel.removeAll();
                         remove(financeFinderPanel);
                         pack();                      
                      }
                     else if(selection.equals("Doubling time")){
                         doublingTimePanel();
                         add(doublingTimePanel, BorderLayout.NORTH);
    
                         financeFinderPanel.removeAll();
                         remove(financeFinderPanel);
                         pack();
                     }
                     else if(selection.equals("Compound interest")){
    
                        compoundInterestPanel();
                        add(compoundInterestPanel, BorderLayout.EAST);
                        financeFinderPanel.removeAll();
                        remove(financeFinderPanel);
                        pack();
                     }
    
                     else if(selection.equals("Future value"))
                     {
                         futureValuePanel();
                         add(futureValuePanel, BorderLayout.WEST);
                         financeFinderPanel.removeAll();
                         remove(financeFinderPanel);
                         pack();
    
                     }
                     else if(selection.equals("Choose a formula")){
    
                          setSize(200, 125);
                          financeFinderPanel();
                          add(financeFinderPanel, BorderLayout.NORTH);
                          NONEMessage = new JLabel("PICK A SELECTION");
    
    
                      }
    
    
    
    
    
    
    
                  }
               }
    

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

    像这样……

    if(actionCommand.equals("Calc Simple Interest")){
    
    
                            try{
                            double principal = Double.parseDouble(principalText.getText());
                            double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                            double termYears = Double.parseDouble(termYearsText.getText());
    
                            double interestRate = financeFormula.simpleInterest(principal, yearlyRate, termYears);
                            //double interestRate = (principal * yearlyRate * termYears);
    
                            String msg = "Simple interest is: $" + dc.format(interestRate);
                            JOptionPane.showMessageDialog(null, msg);
    
    
    
    
                            simpleInterestPanel.removeAll();
                            remove(simpleInterestPanel);
                            financeFinderPanel();
                            add(financeFinderPanel, BorderLayout.NORTH);
                            pack();
    
    
    
    }
    

    在这个例子中,我点击的按钮的名称是Calc Simple Interest

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      相关资源
      最近更新 更多