public class Test {
static JTextField tf;
public static void main(String args[]) {
JFrame j = new JFrame("窗口");
j.setVisible(true);
j.setSize(50, 100);
tf = new JTextField("无敌是多么的寂寞");
JButton b1 = new JButton("红");
JButton b2 = new JButton("黄");
j.setLayout(new FlowLayout());
j.add(b1);
j.add(b2);
j.add(tf);
//忘记了有什么好的刷新方式
j.setVisible(false);
j.setVisible(true);
my ji = new my();
b1.addMouseListener(ji);
b2.addMouseListener(ji);
}
}
class my implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
JButton j = (JButton) e.getSource();
if (j.getText().equals("红")) {
Test.tf.setBackground(Color.red);
} else {
Test.tf.setBackground(Color.yellow);
}
}
//省略其他方法
}