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.
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并向其中添加组件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.