删除Java面板而不会使程序崩溃

删除Java面板而不会使程序崩溃,java,swing,Java,Swing,因此,基本上我正在编写一个GUI程序,它使用JComboBox来显示各种金融公式。我已经准备好了显示公式的一切,但这是我的问题。每次我在组合框中选择一个新公式时,其他公式都会显示出来。我试着写一个if语句来禁用、删除和失效这些(不是一次全部),但是没有一个有效…我需要得到它,这样当我点击一个选项时,其他面板就会消失,如果有的话。谢谢 这是完整的程序,我遇到的问题是,当我选择一个新的公式时,我在它仍然有效之前选择的公式…我想禁用或删除它。谢谢 import java.awt.BorderLayou

因此,基本上我正在编写一个GUI程序,它使用JComboBox来显示各种金融公式。我已经准备好了显示公式的一切,但这是我的问题。每次我在组合框中选择一个新公式时,其他公式都会显示出来。我试着写一个if语句来禁用、删除和失效这些(不是一次全部),但是没有一个有效…我需要得到它,这样当我点击一个选项时,其他面板就会消失,如果有的话。谢谢

这是完整的程序,我遇到的问题是,当我选择一个新的公式时,我在它仍然有效之前选择的公式…我想禁用或删除它。谢谢

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;





public class newFinanceFullClass extends JFrame {


           private JMenu fileMenu;
           private JMenuItem exitItem;

           private JPanel presentValuePanel;  
           private JPanel financeFinderPanel;// A panel container
           private JLabel principalMessage;  
           private JLabel yearlyRateMessage;
           private JLabel termYearMessage;
           private JPanel simpleInterestPanel;
           private JPanel doublingTimePanel;
           private JPanel compoundInterestPanel;
           private JLabel NONEMessage;
           private JLabel imageLabel;
           private JLabel label;// A message to display
           private JMenuBar menuBar;
           private JTextField principalText; // To hold user input
           private JButton calcButton;       // Performs calculation
           private final int WINDOW_WIDTH = 600;  // Window width
           private final int WINDOW_HEIGHT = 600; // Window height
           private JTextField yearlyRateText;
           private JTextField termYearText;
           DecimalFormat dc =new DecimalFormat("####0.00"); //formater 
           private JComboBox financeBox; 
           double principal = 400.0;
           double yearlyRate = .04;
           double termYears = 4.0;

           private String[] financeFormulas = { "NONE", "Present value", "Simple interest",
                   "Doubling time", "Compound interest", "Decaf"};


           financeFormulaClass financeFormula = new financeFormulaClass(principal, yearlyRate, termYears);

           /**
            *  Constructor
            */

           public newFinanceFullClass()
           {
              // Call the JFrame constructor.
              super("Finance Class");

              // Set the size of the window.
              setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

              // Specify what happens when the close
              // button is clicked.
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              // Build the panel and add it to the frame.
              financeFinderPanel();
              setLayout(new BorderLayout());

              //buildPanel();

              menuBar = new JMenuBar();
              buildFileMenu();
              menuBar.add(fileMenu);
              setJMenuBar(menuBar);

              // Add the panel to the frame's content pane.
              add(financeFinderPanel);
              //add(panel);

              // Display the window.
              setVisible(true);
           }

           private void financeFinderPanel()
           {
              // Create a panel to hold the combo box.


              financeFinderPanel = new JPanel();

              //create label
              label = new JLabel("Pick your formula");
              ImageIcon funnyImage = new ImageIcon("funny.gif");
              imageLabel = new JLabel();
              imageLabel.setLocation(50, 55);

              label.setForeground(Color.BLUE);
              // Create the combo box
              financeBox = new JComboBox(financeFormulas);
              imageLabel.setIcon(funnyImage);
              // Register an action listener.
              financeBox.addActionListener(new financeBoxListener());
              exitItem = new JMenuItem("Exit");   
              exitItem.setMnemonic(KeyEvent.VK_X);

              // Add the combo box to the panel.
              financeFinderPanel.add(financeBox);
              financeFinderPanel.add(label);
              financeFinderPanel.add(imageLabel);


           }

          /** private void menuList(){
            exitItem = new JMenuItem("Exit");   
            exitItem.setMnemonic(KeyEvent.VK_X);
            menuList.add(exitItem);
           } **/

           private void buildMenuBar()
           {
              // Create the menu bar.
              menuBar = new JMenuBar();

              // Create the file and text menus.
              buildFileMenu();
             // buildTextMenu();

              // Add the file and text menus to the menu bar.
              menuBar.add(fileMenu);
              //menuBar.add(textMenu);

              // Set the window's menu bar.
              setJMenuBar(menuBar);
           }





               private void buildFileMenu()
               {
                  // Create an Exit menu item.
                  exitItem = new JMenuItem("Exit");
                  exitItem.setMnemonic(KeyEvent.VK_X);
                  exitItem.addActionListener(new ExitListener());


                  // Create a JMenu object for the File menu.
                  fileMenu = new JMenu("File");
                  fileMenu.setMnemonic(KeyEvent.VK_F);

                  // Add the Exit menu item to the File menu.
                  fileMenu.add(exitItem);

               }





           private void presentValuePanel()
           {
              // Create the label, text field, and button components.
              principalMessage = new JLabel("principal");
              principalText = new JTextField(10);
              yearlyRateMessage = new JLabel("Yearly Rate");
              yearlyRateText = new JTextField(10);
              termYearMessage = new JLabel("Term Year");
              termYearText = new JTextField(10);
              calcButton = new JButton("Calc Present Value");

              // Add an action listener to the button.
              //calcButton.addActionListener(new CalcButtonListener());

              calcButton.addActionListener(new financeFormListener());

              // Create a panel to hold the components.
              presentValuePanel = new JPanel();

              // Add the label, text field, and button to the panel.
              presentValuePanel.add(principalMessage);
              presentValuePanel.add(principalText);
              presentValuePanel.add(yearlyRateMessage);
              presentValuePanel.add(yearlyRateText);
              presentValuePanel.add(termYearMessage);
              presentValuePanel.add(termYearText);
              presentValuePanel.add(calcButton);
           }


          private void simpleInterestPanel(){

               principalMessage = new JLabel("principal");
                  principalText = new JTextField(10);
                  yearlyRateMessage = new JLabel("Yearly Rate");
                  yearlyRateText = new JTextField(10);
                  termYearMessage = new JLabel("Term Year");
                  termYearText = new JTextField(10);
                  calcButton = new JButton("Calc Simple Interest");

                  simpleInterestPanel = new JPanel();
                  calcButton.addActionListener(new financeFormListener());

                  simpleInterestPanel.add(principalMessage);
                  simpleInterestPanel.add(principalText);
                  simpleInterestPanel.add(yearlyRateMessage);
                  simpleInterestPanel.add(yearlyRateText);
                  simpleInterestPanel.add(termYearMessage);
                  simpleInterestPanel.add(termYearText);
                  simpleInterestPanel.add(calcButton);



           }

          private void doublingTimePanel(){


              yearlyRateMessage = new JLabel("Yearly Rate");
              yearlyRateText = new JTextField(10);

              calcButton = new JButton("Calc Doubling Time");
              calcButton.addActionListener(new financeFormListener());


              doublingTimePanel = new JPanel();

              doublingTimePanel.add(yearlyRateMessage);
              doublingTimePanel.add(yearlyRateText);
              doublingTimePanel.add(calcButton);

          }

          private void compoundInterestPanel(){


              principalMessage = new JLabel("principal");
              principalText = new JTextField(10);
              yearlyRateMessage = new JLabel("Yearly Rate");
              yearlyRateText = new JTextField(10);
              termYearMessage = new JLabel("Term Year");
              termYearText = new JTextField(10);
              calcButton = new JButton("Calc Compound Interest");

              compoundInterestPanel = new JPanel();
              calcButton.addActionListener(new financeFormListener());

              compoundInterestPanel.add(principalMessage);
              compoundInterestPanel.add(principalText);
              compoundInterestPanel.add(yearlyRateMessage);
              compoundInterestPanel.add(yearlyRateText);
              compoundInterestPanel.add(termYearMessage);
              compoundInterestPanel.add(termYearText);
              compoundInterestPanel.add(calcButton);

          }


           //Listener to choose which formula

           private class financeBoxListener implements ActionListener
           {
              public void actionPerformed(ActionEvent e)
              {

                  Object actionCommand = e.getSource();
                  String selection = (String) financeBox.getSelectedItem();
                  if(selection.equals("Present value")){
                      //financeFinderPanel.removeAll();




                      presentValuePanel();
                      add(presentValuePanel, BorderLayout.SOUTH);

                      pack();


                  }



                 else if(selection.equals("Simple interest")){



                     simpleInterestPanel();
                     add(simpleInterestPanel, BorderLayout.NORTH);


                     pack();

                  }
                 else if(selection.equals("Doubling time")){
                     doublingTimePanel();
                     add(doublingTimePanel, BorderLayout.SOUTH);
                     pack();

                 }
                 else if(selection.equals("Compound interest")){

                    compoundInterestPanel();
                    add(compoundInterestPanel, BorderLayout.NORTH);
                    pack();

                 }
                 else if(selection.equals("NONE")){

                      setSize(200, 150);
                      financeFinderPanel();
                      add(financeFinderPanel, BorderLayout.NORTH);
                      NONEMessage = new JLabel("PICK A SELECTION");


                  }





              }
           }



           private class financeFormListener implements ActionListener{

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub


                String actionCommand = e.getActionCommand();
                if(actionCommand.equals("Calc Simple Interest")){


                        try{
                        double principal = Double.parseDouble(principalText.getText());
                        double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                        double termYears = Double.parseDouble(termYearText.getText());
                        double interestRate = (principal * yearlyRate * termYears);

                        String msg = "Simple interest is: $" + dc.format(interestRate);
                        JOptionPane.showMessageDialog(null, msg);
                        }
                        catch(NumberFormatException r){

                            JOptionPane.showMessageDialog(null, "Please insert formula information");

                        }

                }
                else if(actionCommand.equals("Calc Present Value"))


                {

                    try{
                double principal = Double.parseDouble(principalText.getText());
                double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                double termYears = Double.parseDouble(termYearText.getText());

                double pValue = financeFormula.presentValue(principal, yearlyRate, termYears);
                //double pValue = principal * (((1- Math.pow(1 + yearlyRate, -termYears))/ yearlyRate));
                String msg = "Present value is: $" + dc.format(pValue);


                JOptionPane.showMessageDialog(null, msg);
                    }
                 catch(NumberFormatException r){

                     JOptionPane.showMessageDialog(null, "Please insert formula information");
                 }
                }

                else if(actionCommand.equals("Calc Doubling Time")){



                    try{
                    double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                    double dValue = financeFormula.doublingTime(yearlyRate);
                    String msg = "Doubling Time is: " + dc.format(dValue);
                    JOptionPane.showMessageDialog(null, msg);
                    }
                    catch(NumberFormatException r){

                        JOptionPane.showMessageDialog(null, "Please insert formula information");
                    }
                }

                else if(actionCommand.equals("Calc Compound Interest")){

                    try{
                        double principal = Double.parseDouble(principalText.getText());
                        double yearlyRate = Double.parseDouble(yearlyRateText.getText());
                        double termYears = Double.parseDouble(termYearText.getText());
                        double compoundInterest = financeFormula.compoundInterest(principal, yearlyRate, termYears);
                        String msg = "Compound Interest is: $" + dc.format(compoundInterest);
                        JOptionPane.showMessageDialog(null, msg);
                    }
                    catch(NumberFormatException r){

                        JOptionPane.showMessageDialog(null, "Please insert formula information");

                    }
                }
                }






            }

           private class ExitListener implements ActionListener
            {
              public void actionPerformed(ActionEvent e)
              {
                 System.exit(0);
              }
            }














           public static void main(String[] args)
           {
              new newFinanceFullClass();
           }

}

同样,使用CardLayout来帮助您交换JPanel。您将创建一个JPanel并将其布局设置为CardLayout,然后使用字符串常量将您希望交换的JPanel(“卡”)添加到此持有者JPanel中,字符串将传递到CardLayout的
show(…)
方法中,以允许其交换组件

例如,假设您有一个用于保存财务公式字符串的枚举,例如:

public enum FinanceFormula {
    NONE("NONE"), 
    PRESENT_VALUE("Present value"), 
    SIMPLE_INTEREST("Simple interest"),
    DOUBLING_TIME("Doubling time"), 
    COMPOUND_INTEREST("Compound interest"), 
    DECAF("Decaf");

    private String name;

    private FinanceFormula(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return getName();
    };
}
然后CardLayout及其JPanel可以这样设置:

private CardLayout cardLayout = new CardLayout();
private JPanel cardHolderPanel = new JPanel(cardLayout);
还有JComboBox,如下所示。这是因为我已重写枚举的to String方法,以便显示正确的名称:

private JComboBox<FinanceFormula> comboBox = new JComboBox<>(FinanceFormula.values());
然后,当您想要交换JPanel时,只需将适当的字符串传递到CardLayout的show方法中,就可以了:

private void combBoxActionPerformed(ActionEvent e) {
    FinanceFormula selectedFormula = (FinanceFormula) comboBox.getSelectedItem();
    cardLayout.show(cardHolderPanel, selectedFormula.getName());
}
以下程序不会创建任何奇特的JPanel公式,但也不必创建,因为它是一个MCVE,仅用于演示如何交换JPanel:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class NewFinance2 extends JPanel {
    private CardLayout cardLayout = new CardLayout();
    private JPanel cardHolderPanel = new JPanel(cardLayout);
    private JComboBox<FinanceFormula> comboBox = new JComboBox<>(FinanceFormula.values());

    public NewFinance2() {
        // fill the cardHolderPanel with "cards", JPanels with formula:
        cardHolderPanel.add(createNonePanel(), FinanceFormula.NONE.getName());
        cardHolderPanel.add(createPresentValuePanel(), FinanceFormula.PRESENT_VALUE.getName());
        cardHolderPanel.add(createSimpleInterestPanel(), FinanceFormula.SIMPLE_INTEREST.getName());
        cardHolderPanel.add(createDoublingTimePanel(), FinanceFormula.DOUBLING_TIME.getName());
        cardHolderPanel.add(createCompoundInterestPanel(), FinanceFormula.COMPOUND_INTEREST.getName());
        cardHolderPanel.add(createDecafPanel(), FinanceFormula.DECAF.getName());

        comboBox.addActionListener(e -> combBoxActionPerformed(e));
        JPanel northPanel = new JPanel();
        northPanel.add(comboBox);

        setLayout(new BorderLayout());
        add(cardHolderPanel, BorderLayout.CENTER);
        add(northPanel, BorderLayout.NORTH);
    }

    private void combBoxActionPerformed(ActionEvent e) {
        FinanceFormula selectedFormula = (FinanceFormula) comboBox.getSelectedItem();
        cardLayout.show(cardHolderPanel, selectedFormula.getName());
    }


    // A bunch of dummy methods that don't create much
    // Your real methods would create more complex JPanels
    private JPanel createDecafPanel() {
        return createPanel(FinanceFormula.DECAF);
    }

    private JPanel createCompoundInterestPanel() {
        return createPanel(FinanceFormula.COMPOUND_INTEREST);
    }

    private JPanel createDoublingTimePanel() {
        return createPanel(FinanceFormula.DOUBLING_TIME);
    }

    private JPanel createSimpleInterestPanel() {
        return createPanel(FinanceFormula.SIMPLE_INTEREST);
    }

    private JPanel createPresentValuePanel() {
        return createPanel(FinanceFormula.PRESENT_VALUE);
    }

    private JPanel createNonePanel() {
        return createPanel(FinanceFormula.NONE);
    }   

    // temporary method just for demonstration purposes
    private JPanel createPanel(FinanceFormula financeFormula) {
        JLabel label = new JLabel(financeFormula.getName());
        label.setFont(label.getFont().deriveFont(Font.BOLD, 24f));

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(label);
        panel.setPreferredSize(new Dimension(400, 300));
        float split = 0.7f;
        float h = (float) (split + Math.random() * (1f - split));
        float s = (float) (split + Math.random() * (1f - split));
        float b = (float) (split + Math.random() * (1f - split));
        Color color = Color.getHSBColor(h, s, b);
        panel.setBackground(color);
        return panel;
    }

    private static void createAndShowGui() {
        NewFinance2 mainPanel = new NewFinance2();

        JFrame frame = new JFrame("Finance");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
导入java.awt.BorderLayout;
导入java.awt.CardLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
@抑制警告(“串行”)
公共类新金融2扩展JPanel{
private CardLayout CardLayout=新的CardLayout();
专用JPanel卡面板=新JPanel(卡布局);
private JComboBox comboBox=新的JComboBox(FinanceFormula.values());
公共新金融2(){
//用“卡”填充持卡人面板,用公式填充JPanels:
添加(createNonePanel(),FinanceFormula.NONE.getName());
添加(createPresentValuePanel(),FinanceFormula.PRESENT_VALUE.getName());
添加(createSimpleInterestPanel(),FinanceFormula.SIMPLE_INTEREST.getName());
添加(createDoublingTimePanel(),FinanceFormula.DOUBLING_TIME.getName());
添加(createCompoundInterestPanel(),FinanceFormula.composite_INTEREST.getName());
add(createDecafPanel(),FinanceFormula.DECAF.getName());
comboBox.addActionListener(e->ComboxActionPerformed(e));
JPanel northPanel=新的JPanel();
添加(组合框);
setLayout(新的BorderLayout());
添加(面板、边框布局、中心);
添加(northPanel,BorderLayout.NORTH);
}
已执行的私有操作(操作事件e){
FinanceFormula selectedFormula=(FinanceFormula)组合框。getSelectedItem();
显示(持卡人面板,selectedFormula.getName());
}
//一堆不会产生太多结果的伪方法
//您的实际方法将创建更复杂的JPanel
私有JPanel createDecafPanel(){
返回createPanel(FinanceFormula.DECAF);
}
私有JPanel CreateCompondinInterestPanel(){
返回createPanel(FinanceFormula.复利);
}
私有JPanel createDoublingTimePanel(){
返回createPanel(FinanceFormula.倍增时间);
}
私有JPanel createSimpleInterestPanel(){
返回createPanel(FinanceFormula.单利);
}
私有JPanel createPresentValuePanel(){
返回createPanel(FinanceFormula.现值);
}
私有JPanel createNonePanel(){
返回createPanel(FinanceFormula.NONE);
}   
//仅用于演示目的的临时方法
私人JPanel createPanel(金融公式金融公式){
JLabel=新的JLabel(financeFormula.getName());
label.setFont(label.getFont().deriveFont(Font.BOLD,24f));
JPanel panel=newjpanel(newgridbagloayout());
面板。添加(标签);
面板。设置首选尺寸(新尺寸(400300));
浮动分割=0.7f;
浮点h=(浮点)(拆分+数学随机()*(1f-拆分));
float s=(float)(split+Math.random()*(1f-split));
float b=(float)(split+Math.random()*(1f-split));
Color Color=Color.getHSBColor(h、s、b);
面板.立根背景(颜色);
返回面板;
}
私有静态void createAndShowGui(){
NewFinance2主面板=new NewFinance2();
JFrame=新JFrame(“财务”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
调用器(()->createAndShowGui());
}
}

if块失败是因为比较字符串时出错。您不应该使用
==
来比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的问题所在。另一方面,您应该使用a交换组件。当我单击一个新字符串时,是否有人可以简单地删除它们?对不起,我不确定您的意思。你的意思是移除一个JPanel并在其位置上不显示任何内容吗?如果是这样的话,你就是你
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class NewFinance2 extends JPanel {
    private CardLayout cardLayout = new CardLayout();
    private JPanel cardHolderPanel = new JPanel(cardLayout);
    private JComboBox<FinanceFormula> comboBox = new JComboBox<>(FinanceFormula.values());

    public NewFinance2() {
        // fill the cardHolderPanel with "cards", JPanels with formula:
        cardHolderPanel.add(createNonePanel(), FinanceFormula.NONE.getName());
        cardHolderPanel.add(createPresentValuePanel(), FinanceFormula.PRESENT_VALUE.getName());
        cardHolderPanel.add(createSimpleInterestPanel(), FinanceFormula.SIMPLE_INTEREST.getName());
        cardHolderPanel.add(createDoublingTimePanel(), FinanceFormula.DOUBLING_TIME.getName());
        cardHolderPanel.add(createCompoundInterestPanel(), FinanceFormula.COMPOUND_INTEREST.getName());
        cardHolderPanel.add(createDecafPanel(), FinanceFormula.DECAF.getName());

        comboBox.addActionListener(e -> combBoxActionPerformed(e));
        JPanel northPanel = new JPanel();
        northPanel.add(comboBox);

        setLayout(new BorderLayout());
        add(cardHolderPanel, BorderLayout.CENTER);
        add(northPanel, BorderLayout.NORTH);
    }

    private void combBoxActionPerformed(ActionEvent e) {
        FinanceFormula selectedFormula = (FinanceFormula) comboBox.getSelectedItem();
        cardLayout.show(cardHolderPanel, selectedFormula.getName());
    }


    // A bunch of dummy methods that don't create much
    // Your real methods would create more complex JPanels
    private JPanel createDecafPanel() {
        return createPanel(FinanceFormula.DECAF);
    }

    private JPanel createCompoundInterestPanel() {
        return createPanel(FinanceFormula.COMPOUND_INTEREST);
    }

    private JPanel createDoublingTimePanel() {
        return createPanel(FinanceFormula.DOUBLING_TIME);
    }

    private JPanel createSimpleInterestPanel() {
        return createPanel(FinanceFormula.SIMPLE_INTEREST);
    }

    private JPanel createPresentValuePanel() {
        return createPanel(FinanceFormula.PRESENT_VALUE);
    }

    private JPanel createNonePanel() {
        return createPanel(FinanceFormula.NONE);
    }   

    // temporary method just for demonstration purposes
    private JPanel createPanel(FinanceFormula financeFormula) {
        JLabel label = new JLabel(financeFormula.getName());
        label.setFont(label.getFont().deriveFont(Font.BOLD, 24f));

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(label);
        panel.setPreferredSize(new Dimension(400, 300));
        float split = 0.7f;
        float h = (float) (split + Math.random() * (1f - split));
        float s = (float) (split + Math.random() * (1f - split));
        float b = (float) (split + Math.random() * (1f - split));
        Color color = Color.getHSBColor(h, s, b);
        panel.setBackground(color);
        return panel;
    }

    private static void createAndShowGui() {
        NewFinance2 mainPanel = new NewFinance2();

        JFrame frame = new JFrame("Finance");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}