GUI Organization 5th BorderLayout

时间:2018-06-12 17:28:54

标签: java swing layout-manager

我不明白一旦我用完了其中一个角(NORTH,EAST,WEST,SOUTH)后我怎么能显示两个类,如何使用边框布局添加第五个类?正如您在构建按钮区域中看到的,我需要能够有两个类,我不关心在Tastybois类的哪个位置,我只需要能够显示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);
      }
   }

}

1 个答案:

答案 0 :(得分:0)

通常使用第二个JPanel,所以你有一个层次结构,你的主要JPanel(这里称为容器)通过BorderLayout保存5个其他JPanel,在每个子面板上设置一个新的LayoutManager并向其添加组件。