【发布时间】:2014-09-14 09:42:32
【问题描述】:
我正在尝试使用 MVC 结构创建一个小 GUI,现在我希望能够从 Listener 类在我的 GUI 中设置 JTextField 的背景:
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);
}
}
所以我不能只写在 Listener 类中:
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 类中解析它? 感谢您的帮助!
【问题讨论】:
-
"String as Parameter an Parameter an parse it in the GUI Class?"传递
Color,如果它应该代表它! -
我做了一些提示错误...刚刚更新了它..
-
你想过
JColorChooserHow to Use Color Choosers吗? -
再次,我会在第一时间将
String解析为Color,并特别同意@DavidPostill 的建议——没有比选择颜色更好的(Swing)组件了JColorChooser. -
好吧,我必须看到我听说 JColorChooser 对于我的小 gui 来说有点太大了^^
标签: java swing colors background jtextfield