简体   繁体   English

GUI组织5th BorderLayout

[英]GUI Organization 5th BorderLayout

I don't understand how I can have two classes displayed once I have already used up one of the corners (NORTH, EAST, WEST, SOUTH), how can I add a fifth class by using border layout? 我不明白一旦用完一个角落(北,东,西,南)后如何显示两个班级,如何使用边框布局添加第五个班级? as you can see in the build buttons area, I need to be able to have two classes in the are, I don't care where the tastybois class goes I just need to be able to have 5 things displayed. 如您在构建按钮区域中看到的那样,我需要能够在are中拥有两个类,我不在乎sweetbois类所处的位置,我只需要能够显示5个东西即可。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class IkeaRun extends JFrame{
private IkeaMainDish Dish;
private Drinks drinks;
private SideDish Side;
private IkeaGreetings greeting;
private TastyBois tastybois;
private JPanel mainPanel;
private JButton calcButton;
private JButton exitButton;
private final double taxRate = 0.06;
public IkeaRun(){
    setTitle("ORDER");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    greeting = new IkeaGreetings();
    Dish = new IkeaMainDish();
    drinks = new Drinks();
    Side = new SideDish();
    tastybois = new TastyBois();
    buildButtons();
    add(greeting, BorderLayout.NORTH);
    add(Dish, BorderLayout.WEST);
    add(drinks, BorderLayout.CENTER);
    add(Side, BorderLayout.EAST);
    add(mainPanel, BorderLayout.SOUTH);
    add(tastybois, BorderLayout.EAST);
    pack();
    setVisible(true);

}
private void buildButtons(){
        mainPanel = new JPanel();
        calcButton = new JButton("Order");
        exitButton = new JButton("Cancel");
        calcButton.addActionListener(new CalcButtonListener());
        exitButton.addActionListener(new ExitButtonListener());
        mainPanel.add(calcButton);
        mainPanel.add(exitButton);
 }
 private class CalcButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
          double subtotal = 0.0;
          double tax = 0.0;;
          double total = 0.0;
         subtotal = Dish.getDishPrice() + 
                    drinks.getDrinkPrice() +
                    Side.getSidePrice();
         tax = subtotal * taxRate;
         total = subtotal + tax;
         DecimalFormat dollar = new DecimalFormat("0.00");
         JOptionPane.showMessageDialog(null, "Subtotal: $" + dollar.format(subtotal) + "\n" + "Tax: $" + dollar.format(tax) + "\n" + "Total: $" + dollar.format(total));
      }
      }
 private class ExitButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
          System.exit(0);
      }
   }

} }

通常情况下,第二个JPanel具有一个层次结构,这样您的主JPanel(在这里称为容器)通过BorderLayout可以容纳另外5个JPanels,在每个子面板上设置一个新的LayoutManager并向其中添加组件。

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

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