0

我在我的 Java GUI 中遇到了这个小问题。当我运行程序时,第一个按钮已经突出显示。如果我单击另一个按钮,该按钮将执行其相应的操作,但之后它只会在我单击任何其他按钮之前保持突出显示。请帮帮我!我已经发布了包含主类的代码,因为我觉得问题来自这里。

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Graphics;


public class Terminal extends JFrame 
{
public int amountGot;
public String dishDelPos;
public int dishDeletePosition;
public int billDisplayAmount = 0;
public Graphics g;
public int dishquant;
public String dishQuantStr;
public int amount ;
Bill order1 = new Bill();
String dishQuant;
public JTextField amountField;
public JTextField billAmountField;
Scanner in = new Scanner(System.in);
public Terminal()
{
    terminalUI();
}

public final void terminalUI()
{

    JPanel panel1 = new JPanel();
    panel1.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
    panel1.setLayout(new GridLayout(5,5,5,5));

    String[] buttons = {
                                "Idli   (Rs 16)", "Uddina Vada", "Khara Bhath", "Kesari Bhath", "Chow-Chow Bhath", "Veg Pulao", "Vangi Bhath", "Puliogare", "Lemon Rice", "Veg Biryani", "Bisi Bele Bhath", "Poori,Saagu", "Curd Vada", "Plain Dosa", "Masala Dosa", "Onion Dosa", "Open Butter Masala Dosa", "Rava Dosa", "Set Dosa", "Tea", "Coffee"
                             };
    List<JButton> buttonDishes = new ArrayList<JButton>();
    for(int i = 0; i<buttons.length; i++)
    {
        buttonDishes.add(new JButton(buttons[i]));
    }
    for(int i = 0; i<buttons.length; i++)
    {
        panel1.add(buttonDishes.get(i));
        (buttonDishes.get(i)).setToolTipText("Order "+buttons[i]);
    }

    JButton displayButton = new JButton("Display The Bill");
    JButton clearButton = new JButton("Clear The Order");




    JPanel panel2 = new JPanel();
    JLabel amountFieldText = new JLabel("THE TOTAL AMOUNT IS ");
    panel2.setLayout(new FlowLayout());
    panel2.add(amountFieldText);
    panel2.add(amountField = new JTextField(20));
    amountField.setHorizontalAlignment(amountField.RIGHT);
    amountField.setEditable(false);




    JPanel panel4 = new JPanel();
    JLabel menuInstructionsLabel = new JLabel();
    menuInstructionsLabel.setText("CLICK ON THE DISHES OF YOUR CHOICE TO PLACE YOUR ORDER");
    panel4.setLayout(new FlowLayout());
    panel4.add(menuInstructionsLabel);


    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(panel4, BorderLayout.NORTH);
    panel.add(panel1, BorderLayout.NORTH);
    panel.add(panel2, BorderLayout.NORTH);
    panel.add(displayButton, BorderLayout.NORTH);
    panel.add(clearButton, BorderLayout.NORTH);

    (buttonDishes.get(0)).addActionListener(new ListenToIdli());
    (buttonDishes.get(1)).addActionListener(new ListenToVada());
    (buttonDishes.get(2)).addActionListener(new ListenToKhara());
    (buttonDishes.get(3)).addActionListener(new ListenToKesari());
    (buttonDishes.get(4)).addActionListener(new ListenToChow());
    (buttonDishes.get(5)).addActionListener(new ListenToPulao());
    (buttonDishes.get(6)).addActionListener(new ListenToVangi());
    (buttonDishes.get(7)).addActionListener(new ListenToPuliogare());
    (buttonDishes.get(8)).addActionListener(new ListenToLemon());
    (buttonDishes.get(9)).addActionListener(new ListenToBiryani());
    (buttonDishes.get(10)).addActionListener(new ListenToBisi());
    (buttonDishes.get(11)).addActionListener(new ListenToPoori());
    (buttonDishes.get(12)).addActionListener(new ListenToCurd());
    (buttonDishes.get(13)).addActionListener(new ListenToPlain());
    (buttonDishes.get(14)).addActionListener(new ListenToMasala());
    (buttonDishes.get(15)).addActionListener(new ListenToOnion());
    (buttonDishes.get(16)).addActionListener(new ListenToOpenButter());
    (buttonDishes.get(17)).addActionListener(new ListenToRava());
    (buttonDishes.get(18)).addActionListener(new ListenToSet());
    (buttonDishes.get(19)).addActionListener(new ListenToTea());
    (buttonDishes.get(20)).addActionListener(new ListenToCoffee());
    displayButton.addActionListener(new ListenToDisplay());
    clearButton.addActionListener(new ListenToClear());



    add(panel);

    setTitle("SAKKATH HOT MAGA DARSHINI");
    setSize(2000,1000);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);    
}

class ListenToClear implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        order1.deleteAll();
        amount = 0;
        amountField.setText(Integer.toString(amount));
        billAmountField.setText(Integer.toString(amount));
    }
}

class ListenToDisplay extends JFrame implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        JButton deleteButton = new JButton("Delete Order");
        JPanel displayPanel = new JPanel();
        displayPanel.add(deleteButton);
        deleteButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                dishDelPos = JOptionPane.showInputDialog("Enter the position of the order to be deleted");
                dishDeletePosition = Integer.parseInt(dishDelPos);
                order1.posdel(dishDeletePosition);
                repaint();
            }
        });
        setTitle("CURRENT BILL");
        setSize(500,500);
        setLocationRelativeTo(null);
        add(billAmountField = new JTextField(15), BorderLayout.PAGE_END);
        billAmountField.setEditable(false);
        billAmountField.setHorizontalAlignment(billAmountField.RIGHT);
        add(displayPanel);
        setVisible(true);
        repaint();
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        order1.display(g);

    }

}



class ListenToIdli implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Idli");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Idli", dishquant,16);
        amount = amount+(16*dishquant);
        amountField.setText(Integer.toString(amount));
    }
}

class ListenToVada implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Uddina Vada");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Uddina Vada", dishquant,14);
        amount = amount+(14*dishquant);
        amountField.setText(Integer.toString(amount));
    }
}

class ListenToKhara implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Khara Bhath");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Khara Bhath",dishquant,14);
        amount = amount+(14*dishquant);
        amountField.setText(Integer.toString(amount));
        billAmountField.setText(Integer.toString(amount));
    }
}

class ListenToKesari implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Kesari Bhath");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Kesari Bhath",dishquant,14);
        amount = amount+(14*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToChow implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Chow-Chow Bhath");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Chow-Chow Bhath",dishquant,28);
        amount = amount+(28*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}   

class ListenToPulao implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Veg Pulao");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Veg Pulao",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));
                }
}

class ListenToVangi implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Vangi Bhath");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Vangi Bhath",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToPuliogare implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Puliogare");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Puliogare",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToLemon implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Lemon Rice");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Lemon Rice",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
} 

class ListenToBiryani implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Veg Biryani");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Veg Biryani",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToBisi implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Bisi Bele Bhath");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Bisi Bele Bhath",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToPoori implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Poori,Saagu");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Poori,Saagu",dishquant,25);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToCurd implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Curd Vada");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Curd Vada",dishquant,20);
        amount = amount+(20*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToPlain implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Plain Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Plain Dosa",dishquant,22);
        amount = amount+(22*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToMasala implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Masala Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Masala Dosa",dishquant,25);
        amount = amount+(25*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToOnion implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Onion Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Onion Dosa", dishquant,30);
        amount = amount+(30*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToOpenButter implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Open Butter Masala Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Open Butter Masala Dosa", dishquant,30);
        amount = amount+(30*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToRava implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Rava Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Rava Dosa", dishquant,28);
        amount = amount+(28*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}   

class ListenToSet implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Set Dosa");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Set Dosa", dishquant,23);
        amount = amount+(23*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToTea implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Tea");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Tea", dishquant,12);
        amount = amount+(12*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

class ListenToCoffee implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        dishQuantStr = JOptionPane.showInputDialog("Enter the quantity of Coffee");
        dishquant = Integer.parseInt(dishQuantStr);
        order1.placeOrder("Coffee", dishquant,12);
        amount = amount+(12*dishquant);
        amountField.setText(Integer.toString(amount));

    }
}

public static void main(String[] args)
{

    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            Terminal term = new Terminal();
            term.setVisible(true);

        }
    });
}

}
4

1 回答 1

2

将您的按钮设置为不可聚焦,以在单击后移除“突出显示”(聚焦)效果。

button.setFocusable(false);

请注意,这将删除在组件之间切换的“选项卡”功能。

于 2012-12-01T05:13:37.643 回答