2

该代码用于通过单击主菜单的子菜单(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();
          }

     }

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

4

1 回答 1

4

我会做很多与你正在做的不同的事情,包括

  • 不要通过getGraphics(). 如此获得的 Graphics 对象不会持久存在,因此它对于对 GUI 的外观进行稳定的更改没有用处。
  • 不要清除 GUI 的图形,而是更改视图。即使您的代码有效,组件也不会随您的代码从 GUI 中删除。它们仍然存在并且仍然坐在 GUI 上——不好。
  • CardLayout 可以很好地允许您交换容器的视图,并且通常用于交换 JPanel,每个 JPanel 都有自己的 GUI。
  • 避免空布局和使用setBounds(...),因为这将导致 GUI 的创建是升级和维护的“女巫”,并且在除一个平台之外的所有平台上看起来都很糟糕。最好嵌套 JPanel,每个 JPanel 都使用自己的简单布局,以实现复杂、美观且易于维护和改进的 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();
         }
      });
   }
}
于 2013-07-21T11:18:42.023 回答