package color2;import java.awt.BorderLayout;import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
public class ColorFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton(); public ColorFrame() {
try { jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jButton2.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jButton3.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jButton2.setBounds(new Rectangle(62, 130, 83, 25));
jButton2.setText("绿");
jButton3.setBounds(new Rectangle(99, 215, 83, 25));
jButton3.setText("蓝");
jButton1.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(jButton3);
jButton1.setBounds(new Rectangle(142, 76, 83, 25));
jButton1.setText("红");
jPanel1.setLayout(null);
this.setSize(500, 400);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setVisible(true);
} public void jButton1_actionPerformed(ActionEvent e) {
ColorFrame frm = new ColorFrame();
String command = e.getActionCommand();
if (command.equals("红")) {
frm.getContentPane().setBackground(new Color(255, 0, 0));
} else if (command.equals("绿")) {
frm.getContentPane().setBackground(new Color(0, 255, 0));
} else {
frm.getContentPane().setBackground(new Color(0, 0, 255));
} }
class ColorFrame_jButton1_actionAdapter implements ActionListener {
private ColorFrame adaptee;
ColorFrame_jButton1_actionAdapter(ColorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
} }}
不知道怎么回事,这个监听根本不起作用~~~
我是一开始做了三个
可是想合成一个用,然后在那里面判断一下就可以了,谁知道怎么都做不好,请大家帮帮我吧,太感谢了~
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
public class ColorFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton(); public ColorFrame() {
try { jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
jButton2.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jButton3.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jButton2.setBounds(new Rectangle(62, 130, 83, 25));
jButton2.setText("绿");
jButton3.setBounds(new Rectangle(99, 215, 83, 25));
jButton3.setText("蓝");
jButton1.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(jButton3);
jButton1.setBounds(new Rectangle(142, 76, 83, 25));
jButton1.setText("红");
jPanel1.setLayout(null);
this.setSize(500, 400);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setVisible(true);
} public void jButton1_actionPerformed(ActionEvent e) {
ColorFrame frm = new ColorFrame();
String command = e.getActionCommand();
if (command.equals("红")) {
frm.getContentPane().setBackground(new Color(255, 0, 0));
} else if (command.equals("绿")) {
frm.getContentPane().setBackground(new Color(0, 255, 0));
} else {
frm.getContentPane().setBackground(new Color(0, 0, 255));
} }
class ColorFrame_jButton1_actionAdapter implements ActionListener {
private ColorFrame adaptee;
ColorFrame_jButton1_actionAdapter(ColorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
} }}
不知道怎么回事,这个监听根本不起作用~~~
我是一开始做了三个
可是想合成一个用,然后在那里面判断一下就可以了,谁知道怎么都做不好,请大家帮帮我吧,太感谢了~
解决方案 »
- 为什么基本类型给可以赋值给对象
- 在Java中用SQL语句删除access数据库的记录,为什么用access打开时记录还在?在线等!
- java 的正则表达式的使用问题
- 暴菜的问题:日期+时间应该 使用什么数据类型的变量
- 如何将一个数据库中的表显示在程序的窗口中呢
- List l1 = new LinkedList();报错
- 谁能提供几个关于java的好的新闻组服务器地址阿
- 大家给点意见,来者有分!!!
- 如何将JAVA程序从Jbuilder中导出为可执行文件?100分
- ie5都不支持swing,怎么办?只能用awt?
- 经典作品--Java解惑谜题连载⑤
- 可以 把多个 jtextfield 的内容储存到 一个 arraylist 里面吗 。。
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class ColorFrame extends JFrame
{
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton(); public ColorFrame()
{
try
{ jbInit();
}
catch (Exception exception)
{
exception.printStackTrace();
}
} private void jbInit() throws Exception
{
getContentPane().setLayout(borderLayout1);
jButton2.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jButton3.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jButton2.setBounds(new Rectangle(62, 130, 83, 25));
jButton2.setText("绿");
jButton3.setBounds(new Rectangle(99, 215, 83, 25));
jButton3.setText("蓝");
jButton1.addActionListener(new ColorFrame_jButton1_actionAdapter(this));
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(jButton3);
jButton1.setBounds(new Rectangle(142, 76, 83, 25));
jButton1.setText("红");
jPanel1.setLayout(null);
this.setSize(500, 400);
this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
this.setVisible(true);
} public void jButton1_actionPerformed(ActionEvent e)
{
// ColorFrame frm = new ColorFrame();
String command = e.getActionCommand();
if (command.equals("红"))
{
jPanel1.setBackground(new Color(255, 0, 0));
}
else if (command.equals("绿"))
{
jPanel1.setBackground(new Color(0, 255, 0));
}
else
{
jPanel1.setBackground(new Color(0, 0, 255));
} } class ColorFrame_jButton1_actionAdapter implements ActionListener
{
private ColorFrame adaptee; ColorFrame_jButton1_actionAdapter(ColorFrame adaptee)
{
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e)
{
adaptee.jButton1_actionPerformed(e);
} }
public static void main(String[] args)
{
new ColorFrame();
}}
你设置frm.getContentPane()
当然看不到颜色变化