2017-03-18 54 views
-1

所以我在嘗試更改按鍵的背景顏色時遇到了問題,並且我不完全確定如何去解決它。 它可以作爲一個鍵盤,我唯一的問題是要改變背景顏色的方法,當任何按鍵輸入如何動態更改JButtom背景?

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.GridLayout; 
import java.awt.Panel; 
import java.awt.TextArea; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.AbstractAction; 

public abstract class OnScreenKeyboard extends JFrame implements KeyListener 

{ 
    String firstRow[] = {"~","1","2","3","4","5","6","7","8","9","0","-","+","Back\nSpace"}; 
    String secondRow[] = {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","|"}; 
    String thirdRow[] = {"Caps\nLock","A","S","D","F","G","H","J","K","L",";","'","Enter"}; 
    String fourthRow[] = {"Shift","Z","X","C","V","B","N","M",",",".","?","Space"}; 
    JButton first[] = new JButton[14]; 
    JButton second[] = new JButton[14]; 
    JButton third[] = new JButton[13]; 
    JButton fourth[] = new JButton[12]; 
    Panel keys = new Panel(); 
    Panel text = new Panel(); 
    TextArea textArea = new TextArea(); 
    String strText = ""; 
    private JLabel label1; 
    private JLabel label2; 
    private JTextField textField; 
    public OnScreenKeyboard() 

{ 
    super("Typing Application"); 

    label1 = new JLabel("Type some text using your keyboard. The keys you press will be " 
      + "highlighed and the text will be displayed"); 
    add(label1); 
    label2 = new JLabel("Note: clicking the buttons with your mouse will not perform any action"); 
    add(label2); 
    textField = new JTextField(80); 
    textField.setEditable(true); 
    TextFieldHandler handler = new TextFieldHandler(); 
    this.setLayout(new BorderLayout(1,1)); 
    keys.setLayout(new GridLayout(4,14)); 
    text.setLayout(new BorderLayout(1,1)); 
    text.add(textArea); 



    for(int i=0; i<14; i++) 
    { 
    first[i] = new JButton(firstRow[i]); 
    first[i].setBackground(Color.white); 
    keys.add(first[i]); 
    first[i].addKeyListener(this); 
    } 


    for(int i=0; i<14; i++) 
    { 
    second[i] = new JButton(secondRow[i]); 
    second[i].setBackground(Color.white); 
    keys.add(second[i]); 
    second[i].addKeyListener(this); 
    } 


    for(int i=0; i<13; i++) 
    { 
    third[i] = new JButton(thirdRow[i]); 
    third[i].setBackground(Color.white); 
    keys.add(third[i]); 
    third[i].addKeyListener(this); 
    } 

    for(int i=0; i<12; i++) 
    { 
    fourth[i] = new JButton(fourthRow[i]); 
    fourth[i].setBackground(Color.white); 
    keys.add(fourth[i]); 
    fourth[i].addKeyListener(this); 
    } 

    add(text, BorderLayout.NORTH); 
    add(keys,BorderLayout.CENTER); 
} 


    private class TextAreaHandler implements ActionListener 
    { 

     public void actionPerformed(ActionEvent event) 
     { 
     String string = ""; // declare string to display 
     if (event.getSource() == textField) 
      string = String.format("%s", 
       event.getActionCommand()); 

    } 
    } 

     public void actionPerformed(ActionEvent event) { 
      if (event.getSource() instanceof JButton) { 
       System.out.println((((JButton) event.getSource()).getActionCommand())); 
       ((JButton) event.getSource()).setBackground(Color.BLUE); 
       ((JButton) event.getSource()).setContentAreaFilled(false); 
       ((JButton) event.getSource()).setOpaque(true); 
      } 
     } 


    @Override 
    public void keyTyped(KeyEvent event) 
    { 
     int keyCode = event.getKeyCode(); 
     strText = String.format("%s", event.getKeyCode()); 
    } 



    private class TextFieldHandler implements ActionListener 
    { 
     public void actionPerformed(ActionEvent event) 
     { 
     String string = ""; // declare string to display 
       // user pressed Enter in JTextField textField1 
       if (event.getSource() == textField) 
      string = String.format("%s", event.getActionCommand()); 

     } 

    } 


} 
+0

歡迎來到SO。 TL; DR。這是最短的代碼來證明問題嗎?請發佈一個[MCVE] – c0der

+0

我對java或編碼不是很有經驗,這是我可能想到的最短代碼。如果有更短的路,請讓我知道!我非常渴望從錯誤中學習,並學習不同的編碼方式來高效地編寫代碼。 – ThePlum

+0

請參閱:http://stackoverflow.com/help/someone-answers – c0der

回答

1

它不帶任何的Java知識,知道,你並不需要4x14按鈕和標貼和textfilelds來演示一個JButton中背景顏色的變化。 這個MCVE不能證明它嗎?

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

public class OnScreenKeyboard extends JFrame{ 

    public OnScreenKeyboard() 
    { 
     super(); 
     JButton button = new JButton("T"); 
     add(button,BorderLayout.CENTER); 
     pack();//"size to fit the preferred size and layouts of its subcomponents" 
     setVisible(true);//make Jframe show 
    } 

    public void actionPerformed(ActionEvent event) { 
     if (event.getSource() instanceof JButton) { 
      System.out.println((((JButton) event.getSource()).getActionCommand())); 
      ((JButton) event.getSource()).setBackground(Color.BLUE); 
      ((JButton) event.getSource()).setContentAreaFilled(false); 
      ((JButton) event.getSource()).setOpaque(true); 
     } 
    } 

    //a main is needed to make your code runnable (google MCVE) 
    public static void main(String[] args) { 

     new OnScreenKeyboard(); 
    } 
} 

當它簡短且簡潔時,它也可以幫助您調試問題。
提示:你寫了一個ActionListener但你永遠不會使用它。
如果您需要更多幫助,請不要猶豫,問問。

+0

keybindings和/或get/putClientProperty – mKorbel