Swing 问题 1,主窗体中有十四个Button,产生一个随数,根据随机数,改变Button的背景颜色2,在主窗体中如可显示图片。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 还有,就是我在主窗体中加一个JLabel 可总是加不上背景图片 import java.awt.*;import java.awt.event.*;import javax.swing.*;public class TestComponent extends JComponent{ public TestComponent() { this.setLayout(new FlowLayout(5)); for (int i = 0; i < 14; i++) { JButton btn = new JButton("按钮" + (i + 1)); btn.setBackground(new Color((float) Math.random(), (float) Math.random(), (float) Math.random())); add(btn); } } public void paintComponent(Graphics g) { ImageIcon icon = new ImageIcon("background.jpg"); int width = this.getWidth(); int height = this.getHeight(); g.drawImage(icon.getImage(), 0, 0, width, height, null); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.add(new TestComponent()); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }} 好谢谢了,不是那种效果。我是一个点名系统,当我点 点名 时,主窗体中的十四个lable 的颜色 ,随着随机数而变化,也就是随机哪个数,哪个数对应的 lable就变成红色。实分感谢。 我表示不是很明白,你的意思是说每个数对应一种颜色,随机到某个数时这14个button就显示这个数对应的颜色吗?也就是说button的颜色一直是在变吗? 双缓冲 保存String到文本文件怎么写,网上查了感觉还是不清楚 java中的数组小问题 招聘2年以上Java, .NET, C++软件工程师。 有对ClassLoader熟悉的朋友没?请教一个问题 有一道题,看看吧 怎样用JAVA操作注册表? 关于 JSR-80 操作usb VC++和java起头并进可以吗? package & import 的一个奇怪问题 java paint repaint update方法的调用问题 mac下用swing做出来的界面显示异常TAT
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestComponent extends JComponent
{
public TestComponent()
{
this.setLayout(new FlowLayout(5));
for (int i = 0; i < 14; i++)
{
JButton btn = new JButton("按钮" + (i + 1));
btn.setBackground(new Color((float) Math.random(), (float) Math.random(), (float) Math.random()));
add(btn);
}
}
public void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("background.jpg");
int width = this.getWidth();
int height = this.getHeight();
g.drawImage(icon.getImage(), 0, 0, width, height, null);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new TestComponent());
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我表示不是很明白,你的意思是说每个数对应一种颜色,随机到某个数时这14个button就显示这个数对应的颜色吗?也就是说button的颜色一直是在变吗?