0
我試圖創建一個MVC架構一個小的GUI程序,現在我希望能夠設置一個JTextField的背景,我的GUI從監聽器類:setBackgroundcolor與參數
package gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
public class Gui extends JFrame {
private JTextField jtf;
private JLabel lblStatus;
public Gui() {
new JFrame();
jtf = new JTextField();
//jtf.addKeyListener(this);
lblStatus = new JLabel("Zeit:");
setSize(700, 60);
setTitle("Tippmaster V1.0");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 2));
add(jtf);
add(lblStatus);
setVisible(true);
}
public void addKeyListener(KeyListener kl){
jtf.addKeyListener(kl);
public void setStatus(String status){
lblStatus.setText(status);
}
/**
*
* @param color should involve the information of the color for example GREEN
*/
public void setBackgroundcolor(Color color){
jtf.setBackground(Color.color);
}
}
所以我不能只在監聽器類寫:
package controller;
import gui.Gui;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* Created by tq67 on 23.07.2014.
*/
public class CharListener implements KeyListener {
private Gui gui;
public CharListener(Gui gui) {
this.gui = gui;
}
@Override
public void keyPressed(KeyEvent arg0) { /* Nothing to do */ }
@Override
public void keyReleased(KeyEvent arg0) {
//Here I want to set the parameter sth like this or maybe only Green Blue etc..
gui.setBackground(Color.GREEN);
}
@Override
public void keyTyped(KeyEvent e) { /* Nothing to do */ }
}
那是可能的,或者我應該給一個字符串作爲參數,並在GUI類解析它? Thx爲您提供幫助!
*「?String作爲參數在GUI類的解析它」 *如果這是它應該代表的那麼傳遞一個'Color'! –
我做了一些tippmistakes ...只是更新了它.. – Synoon
你有沒有想過'JColorChooser' [如何使用顏色選擇器](http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html )? – DavidPostill