swing初学者,大家帮帮我的忙~~~

blackboy_my 2007-06-10 08:10:54
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);
}

}

}
不知道怎么回事,这个监听根本不起作用~~~
我是一开始做了三个
可是想合成一个用,然后在那里面判断一下就可以了,谁知道怎么都做不好,请大家帮帮我吧,太感谢了~
...全文
320 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackboy_my 2007-07-12
  • 打赏
  • 举报
回复
多谢啊,兄弟,,,
insiku 2007-06-10
  • 打赏
  • 举报
回复
jPanel1 盖在 frm.getContentPane() 上面
你设置frm.getContentPane()
当然看不到颜色变化
insiku 2007-06-10
  • 打赏
  • 举报
回复
package test;

import java.awt.BorderLayout;
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();
}

}

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧