简体   繁体   English

如何通过单击Java Swing中JMenuBar的另一个子菜单来清除JFrame区域?

[英]How to clear JFrame area by clicking another sub menu of JMenuBar in java Swing?

the code is there for clearing the Frame area by clicking the sub menu(sub_menu_purchase and sub_menu_sale) of main menu. 通过单击主菜单的子菜单(sub_menu_purchase和sub_menu_sale)可以清除“框架”区域中的代码。

public void clear() 
    { 

        Graphics g = getGraphics(); 

        Dimension d = getSize(); 

        g.setColor(Color.WHITE); 

        g.fillRect(0,0,d.width,d.height); 


    }
void sale()
    {
        lblinvoice =new JLabel("Invoice No. : ");
        lbldate =new JLabel("Date : ");
        lblform =new JLabel("From Party : ");
        lblto =new JLabel("To Party : ");

        txtto=new JTextField();
        txtfrom=new JTextField();

        btncancel=new JButton("Cancel");
        btnprint=new JButton("Print");
        btnreset=new JButton("Reset");
        btnsave=new JButton("Save");

        lblinvoice.setBounds(50,100,80,25);
        lbldate.setBounds(440,100,80,25);
        lblto.setBounds(50,135,80,25);
        txtto.setBounds(140,135,200,25);
        lblform.setBounds(50,170,80,25);
        txtfrom.setBounds(140,170,100,25);
        btnreset.setBounds(50,450,80,25);
        btnsave.setBounds(140,450,80,25);
        btnprint.setBounds(230,450,80,25);
        btncancel.setBounds(420,450,80,25);

        add(lblinvoice);
            add(lbldate);
            add(lblto);
                add(lblform);
                add(txtto);
                add(txtfrom);
                add(btncancel);
                add(btnprint);
            add(btnreset);
        add(btnsave);

        setVisible(true);

    }
void purchase()
    {
        lblinvoice =new JLabel("Invoice No. : ");
        lbldate =new JLabel("Date : ");
        lblparty =new JLabel("Party Name: ");

        txtparty=new JTextField();

        btncancel=new JButton("Cancel");
        btnprint=new JButton("Print");
        btnreset=new JButton("Reset");
        btnsave=new JButton("Save");

        lblinvoice.setBounds(50,100,80,25);
        lbldate.setBounds(440,100,80,25);
        lblparty.setBounds(50,135,80,25);
        txtparty.setBounds(140,135,200,25);
        btnreset.setBounds(50,450,80,25);
        btnsave.setBounds(140,450,80,25);
        btnprint.setBounds(230,450,80,25);
        btncancel.setBounds(420,450,80,25);

        add(lblinvoice);
            add(lbldate);
            add(lblparty);
                add(txtparty);
                add(btncancel);
                add(btnprint);
            add(btnreset);
        add(btnsave);

        setVisible(true);

    }
public void actionPerformed(ActionEvent event) //set up actionlistening
      {
          Object source=event.getSource();
          if (source.equals(sub_menu_purchase))
          { clear();
              purchase();
          }
          if (source.equals(sub_menu_sale))
          { clear();
              sale();
          }

     }

But it is not clear the area and override to one another. 但是尚不清楚该区域是否彼此覆盖。 what code should I write? 我应该写什么代码?

There's a lot I would do differently from what you're doing, including 我会做很多与您正在做的事情不同的事情,包括

  • Don't get a component's Graphics via getGraphics() . 不要通过getGraphics()获得组件的Graphics。 The Graphics object thus obtained will not persist, and so it is not useful for making stable changes to a GUI's appearance. 这样获得的Graphics对象将不会持久存在,因此对于稳定更改GUI的外观没有用。
  • Don't clear the GUI's graphics but rather change the view . 不要清除GUI的图形 ,而要更改视图 Even if your code worked, the components would not have been removed from the GUI with your code. 即使您的代码可以工作,组件也不会随您的代码一起从GUI中删除。 They would still exist and still sit on the GUI -- not good. 它们仍然存在并且仍然位于GUI上-不好。
  • A CardLayout would work well for allowing you to swap a container's view, and is often used to swap JPanels, each holding its own GUI. CardLayout可以很好地用于允许您交换容器的视图,并且经常用于交换JPanels,每个JPanels都有自己的GUI。
  • Avoid null layout and using setBounds(...) as this will lead to creation of GUI's that are a "witch" to upgrade and maintain and that look bad on all platforms except for one. 避免使用null布局并使用setBounds(...)因为这将导致创建GUI,这是升级和维护的“女巫”,并且在除一个平台之外的所有平台上看起来都很糟糕。 Better to nest JPanels, each using its own simple layout, to achieve complex, beautiful and easy to maintain and improve GUI's. 更好地嵌套JPanels,每个JPanels使用其自己的简单布局,以实现复杂,美观,易于维护和改进GUI的功能。
  • Read/study the Swing tutorials as all of this is well explained there. 阅读/研究Swing教程,因为所有这些都在这里得到了很好的解释。

For example: 例如:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class UglyGui2 {
   private static final String SALE = "Sale";
   private static final String PURCHASE = "Purchase";

   private JMenuItem sub_menu_sale = new JMenuItem(SALE);
   private JMenuItem sub_menu_purchase = new JMenuItem(PURCHASE);
   private CardLayout cardLayout = new CardLayout();
   private JPanel cardPanel = new JPanel(cardLayout);
   private JPanel mainPanel = new JPanel(new BorderLayout(5, 5));

   public UglyGui2() {
      cardPanel.add(new JLabel(), "");
      cardPanel.add(createSalePanel(), SALE);
      cardPanel.add(createPurchasePanel(), PURCHASE);

      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      buttonPanel.add(new JButton("Reset"));
      buttonPanel.add(new JButton("Save"));
      buttonPanel.add(new JButton("Print"));
      buttonPanel.add(new JLabel());
      buttonPanel.add(new JButton("Cancel"));

      mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      mainPanel.add(cardPanel, BorderLayout.CENTER);
      mainPanel.add(buttonPanel, BorderLayout.PAGE_END);

   }

   private JComponent createSalePanel() {
      JPanel salePanel = new JPanel(new GridBagLayout());
      salePanel.add(new JLabel("Sales"));
      salePanel.add(new JTextField(10));
      return salePanel;
   }

   private JComponent createPurchasePanel() {
      JPanel topPanel = new JPanel();
      topPanel.add(new JLabel("Purchases"));
      topPanel.add(new JTextField(10));

      JPanel purchasePanel = new JPanel(new BorderLayout());
      purchasePanel.add(topPanel, BorderLayout.PAGE_START);
      purchasePanel.add(new JScrollPane(new JTextArea(30, 60)), BorderLayout.CENTER);
      return purchasePanel;   }

   private Component getMainPanel() {
      return mainPanel;
   }

   private JMenuBar getJMenuBar() {
      ActionListener aListener = new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            cardLayout.show(cardPanel, e.getActionCommand());
         }
      };

      sub_menu_purchase.addActionListener(aListener);
      sub_menu_sale.addActionListener(aListener);

      JMenu menu = new JMenu("Menu");
      menu.add(sub_menu_purchase);
      menu.add(sub_menu_sale);

      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);
      return menuBar;
   }

   private static void createAndShowGui() {
      UglyGui2 uglyGui = new UglyGui2();

      JFrame frame = new JFrame("Ugly Gui Example");
      frame.setJMenuBar(uglyGui.getJMenuBar());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(uglyGui.getMainPanel());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

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

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