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

时间:2013-07-21 11:17:32

标签: java swing actionlistener jmenuitem

通过单击主菜单的子菜单(sub_menu_purchase和sub_menu_sale)清除Frame区域的代码。

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();
          }

     }

但目前尚不清楚该区域并相互覆盖。 我应该写什么代码?

1 个答案:

答案 0 :(得分:4)

我会采取与你正在做的事情不同的事情,包括

  • 不要通过getGraphics()获取组件的图形。这样获得的Graphics对象不会持久存在,因此对GUI的外观进行稳定的更改没有用。
  • 不要清除GUI的图形,而是更改视图。即使您的代码有效,也不会使用您的代码从GUI中删除组件。它们仍然存在并仍然在GUI上 - 不好。
  • CardLayout可以很好地允许您交换容器的视图,并且通常用于交换JPanel,每个JPanel都拥有自己的GUI。
  • 避免使用空布局并使用setBounds(...),因为这将导致创建GUI,这是升级和维护的“巫婆”,并且在除了一个平台之外的所有平台上看起来都很糟糕。最好嵌套JPanels,每个都使用自己的简单布局,以实现复杂,美观,易于维护和改进GUI。
  • 阅读/学习Swing教程,因为这里有很好的解释。

例如:

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();
         }
      });
   }
}