class AllPanels extends JPanel implements ActionListener
{
AllSwings frame;
JButton b1=new JButton("White");
JButton b2=new JButton("Black");
JButton b3=new JButton("Green");
JButton b4=new JButton("Blue");
AllPanels()
{
setLayout(new GridLayout(2,2,5,5));
add(b1); b1.addActionListener(this);
add(b2); b2.addActionListener(this);
add(b3); b3.addActionListener(this);
add(b4); b4.addActionListener(this); }
public void actionPerformed(ActionEvent evt)
{
AllSwings als=new AllSwings();
Object source=evt.getSource();
if(source==b1)
{
als.swatch.setBackground(Color.white);
}
else if(source==b2)
{
als.swatch.setBackground(Color.black);
}
else if(source==b3)
{
als.swatch.setBackground(Color.green);
}
else if(source==b4)
{
als.swatch.setBackground(Color.blue);
}
repaint();
}
}
{
AllSwings frame;
JButton b1=new JButton("White");
JButton b2=new JButton("Black");
JButton b3=new JButton("Green");
JButton b4=new JButton("Blue");
AllPanels()
{
setLayout(new GridLayout(2,2,5,5));
add(b1); b1.addActionListener(this);
add(b2); b2.addActionListener(this);
add(b3); b3.addActionListener(this);
add(b4); b4.addActionListener(this); }
public void actionPerformed(ActionEvent evt)
{
AllSwings als=new AllSwings();
Object source=evt.getSource();
if(source==b1)
{
als.swatch.setBackground(Color.white);
}
else if(source==b2)
{
als.swatch.setBackground(Color.black);
}
else if(source==b3)
{
als.swatch.setBackground(Color.green);
}
else if(source==b4)
{
als.swatch.setBackground(Color.blue);
}
repaint();
}
}
解决方案 »
- java程序如何做成可执行程序
- 关于异常
- (急)JAVA的程序,请大家指教,编译通不过~~
- 如何在JAVA程序的调试中获得当前oracle 10g数据库的游标打开数?
- 请帮忙改错,谢谢
- api的使用方法
- 大家帮忙解决JavaMail读取HTML等复杂邮件!!!
- 菜鸟求救:我的Applet为什么在浏览器中无法运行?
- 用fileinputstream读入文件的路径问题
- 用URL和URLConnection对象如何向Web服务器上的文件中写入信息?
- 对象数组排序问题;高手看看!问题出在哪?
- 请问:Exception in thread 'main java.lang.NoClassDefFoundError:News(wrong name:xml bean/News是什么错误
import java.awt.event.*;
import javax.swing.*;
public class AllSwings extends JFrame
{ class AllPanels extends JPanel implements ActionListener
{
//AllSwings frame;
JButton b1=new JButton("White");
JButton b2=new JButton("Black");
JButton b3=new JButton("Green");
JButton b4=new JButton("Blue");
AllPanels()
{
setLayout(new GridLayout(2,2,5,5));
add(b1); b1.addActionListener(this);
add(b2); b2.addActionListener(this);
add(b3); b3.addActionListener(this);
add(b4); b4.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
//AllSwings als=new AllSwings();
Object source=evt.getSource();
if(source==b1)
{
swatch.setBackground(Color.white);
}
else if(source==b2)
{
swatch.setBackground(Color.black);
}
else if(source==b3)
{
swatch.setBackground(Color.green);
}
else if(source==b4)
{
swatch.setBackground(Color.blue);
}
repaint();
}
} AllPanels pls;
JPanel swatch;
AllSwings()
{
setTitle("AllSwings");
setSize(300,100);
JPanel pane=new JPanel();
pane.setLayout(new GridLayout(1,2,5,10));
swatch=new JPanel();
swatch.setBackground(Color.black);
pls=new AllPanels();
pane.add(swatch);
pane.add(pls);
setContentPane(pane);
}
public static void main(String[] args)
{
AllSwings as=new AllSwings();
as.setResizable(false);
as.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as.show();
}
}
把AllPanels变成内部类倒是挺简单的,但是方法不太好,呵呵!