2012-12-01 2 views
0

Java GUI에서 직면 한이 작은 문제가 있습니다. 프로그램을 실행하면 첫 번째 버튼이 이미 강조 표시됩니다. 다른 버튼을 클릭하면 해당 버튼이 해당 액션을 수행하지만 그 후에는 다른 버튼을 클릭하기 전에 강조 표시된 상태로 남아 있습니다. 제발 도와주세요! 문제가 여기에서 왔다고 생각하면서 주 수업을 포함하는 코드를 게시했습니다.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); 

     } 
    }); 
} 

} 
+2

1) * "코드를 제공하지 못할 수도 있습니다."* "코드"가 아닌 [SSCCE] (http://sscce.org/) 코드를 게시하십시오. 그 문제를 보여줍니다. 2) BTW는 Swing, AWT, SWT, web-app입니다., JavaFX ..? 3) '다른 버튼'이 장기간 실행되는 작업입니까? –

+0

음, 여기가 새로운데! @AndrewThompson은 그네입니다. 다른 단추는 장기 실행 작업을 트리거하지 않습니다. – Shonu93

+0

1)'share |를 읽을 수있는 질문 아래에있는 링크 행을 찾으십시오. 편집 | .. '편집'을 클릭하십시오. 2) 스윙 태그가 추가되었습니다. 3) 확인해 주셔서 감사합니다. 그것은 하나의 가능성을 배제합니다. –

답변

2

클릭 한 후 "강조 표시된"(초점을 맞춘) 효과를 제거하려면 버튼을 초점을 맞출 수없는 것으로 설정하십시오.

button.setFocusable(false); 

이렇게하면 구성 요소를 전환하는 "탭"기능이 제거됩니다.

+0

정말 고마워요! 그것은 효과가있다! :) – Shonu93

+0

도움이 된 것을 기쁘게 생각합니다. 승인 된 것으로 표시하는 것을 잊지 마세요! – Vulcan

관련 문제