一个java swing的问题,button数组不能用
这样能很正常地添加importjava.awt.*;importjavax.swing.*;publicclasswb{publicfinalintWhiteNum=52...
这样能很正常地添加
import java.awt.*;
import javax.swing.*;
public class wb {
public final int WhiteNum = 52; //白键数
public final int BlackNum = 51; //黑键数
public final int WW = 15; //白键键宽
public final int BW = 12; //黑键键宽
public final int BH = 45; //黑键长
public final int WH = 65; //白键长
public wb() {
JFrame frame = new JFrame();
JLayeredPane pane = new JLayeredPane();
int[] noBlack= {1,4,8,11,15,18,22,25,29,32,36,39,43,46,50};//标记没有黑键的位置
int flag = 0;
JButton[] white = new JButton[WhiteNum];
JButton[] black = new JButton[BlackNum];
for(int i=0;i<BlackNum;i++)
{ //添加黑块
flag = 0;
for(int j = 0; j < 15;j++)
//把标记数组中的每个值和现在所判断的黑键所在位置比较,相同则让flag=1,即不添加这个黑键
if(noBlack[j] == i)
flag = 1;
if(flag == 1) continue;
JButton button = new JButton();
button.setBackground(Color.BLACK);
button.setBounds(WW*(i+1)-BW/2+18,320, BW, BH);
pane.add(button);
pane.setLayer(button,1); //设置黑键层次(较上面)
}
for(int i=0;i<WhiteNum;i++)
{ //添加白块
JButton button = new JButton();
button.setBackground(Color.WHITE);
button.setBounds(WW*i+18,320, WW, WH);
pane.add(button);
pane.setLayer(button,0); //设置白键层次(较底下)
}
frame.add(pane);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main (String[] args)
{
new wb();
}
}
用了JButton数组就不行了
import java.awt.*;
import javax.swing.*;
public class wb {
public final int WhiteNum = 52; //白键数
public final int BlackNum = 51; //黑键数
public final int WW = 15; //白键键宽
public final int BW = 12; //黑键键宽
public final int BH = 45; //黑键长
public final int WH = 65; //白键长
public wb() {
JFrame frame = new JFrame();
JLayeredPane pane = new JLayeredPane();
int[] noBlack= {1,4,8,11,15,18,22,25,29,32,36,39,43,46,50};//标记没有黑键的位置
int flag = 0;
JButton[] white = new JButton[WhiteNum];
JButton[] black = new JButton[BlackNum];
for(int i=0;i<BlackNum;i++)
{ //添加黑块
flag = 0;
for(int j = 0; j < 15;j++)
//把标记数组中的每个值和现在所判断的黑键所在位置比较,相同则让flag=1,即不添加这个黑键
if(noBlack[j] == i)
flag = 1;
if(flag == 1) continue;
white[i].setBackground(Color.BLACK); //这里运行时报错
white[i].setBounds(WW*(i+1)-BW/2+18,320, BW, BH);
pane.add(white[i]);
pane.setLayer(white[i],1); //设置黑键层次(较上面)
}
for(int i=0;i<WhiteNum;i++)
{ //添加白块
white[i].setBackground(Color.WHITE);
white[i].setBounds(WW*i+18,320, WW, WH);
pane.add(white[i]);
pane.setLayer(white[i],0); //设置白键层次(较底下)
}
frame.add(pane);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main (String[] args)
{
new wb();
}
} 展开
import java.awt.*;
import javax.swing.*;
public class wb {
public final int WhiteNum = 52; //白键数
public final int BlackNum = 51; //黑键数
public final int WW = 15; //白键键宽
public final int BW = 12; //黑键键宽
public final int BH = 45; //黑键长
public final int WH = 65; //白键长
public wb() {
JFrame frame = new JFrame();
JLayeredPane pane = new JLayeredPane();
int[] noBlack= {1,4,8,11,15,18,22,25,29,32,36,39,43,46,50};//标记没有黑键的位置
int flag = 0;
JButton[] white = new JButton[WhiteNum];
JButton[] black = new JButton[BlackNum];
for(int i=0;i<BlackNum;i++)
{ //添加黑块
flag = 0;
for(int j = 0; j < 15;j++)
//把标记数组中的每个值和现在所判断的黑键所在位置比较,相同则让flag=1,即不添加这个黑键
if(noBlack[j] == i)
flag = 1;
if(flag == 1) continue;
JButton button = new JButton();
button.setBackground(Color.BLACK);
button.setBounds(WW*(i+1)-BW/2+18,320, BW, BH);
pane.add(button);
pane.setLayer(button,1); //设置黑键层次(较上面)
}
for(int i=0;i<WhiteNum;i++)
{ //添加白块
JButton button = new JButton();
button.setBackground(Color.WHITE);
button.setBounds(WW*i+18,320, WW, WH);
pane.add(button);
pane.setLayer(button,0); //设置白键层次(较底下)
}
frame.add(pane);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main (String[] args)
{
new wb();
}
}
用了JButton数组就不行了
import java.awt.*;
import javax.swing.*;
public class wb {
public final int WhiteNum = 52; //白键数
public final int BlackNum = 51; //黑键数
public final int WW = 15; //白键键宽
public final int BW = 12; //黑键键宽
public final int BH = 45; //黑键长
public final int WH = 65; //白键长
public wb() {
JFrame frame = new JFrame();
JLayeredPane pane = new JLayeredPane();
int[] noBlack= {1,4,8,11,15,18,22,25,29,32,36,39,43,46,50};//标记没有黑键的位置
int flag = 0;
JButton[] white = new JButton[WhiteNum];
JButton[] black = new JButton[BlackNum];
for(int i=0;i<BlackNum;i++)
{ //添加黑块
flag = 0;
for(int j = 0; j < 15;j++)
//把标记数组中的每个值和现在所判断的黑键所在位置比较,相同则让flag=1,即不添加这个黑键
if(noBlack[j] == i)
flag = 1;
if(flag == 1) continue;
white[i].setBackground(Color.BLACK); //这里运行时报错
white[i].setBounds(WW*(i+1)-BW/2+18,320, BW, BH);
pane.add(white[i]);
pane.setLayer(white[i],1); //设置黑键层次(较上面)
}
for(int i=0;i<WhiteNum;i++)
{ //添加白块
white[i].setBackground(Color.WHITE);
white[i].setBounds(WW*i+18,320, WW, WH);
pane.add(white[i]);
pane.setLayer(white[i],0); //设置白键层次(较底下)
}
frame.add(pane);
frame.setSize(800, 600);
frame.setVisible(true);
}
public static void main (String[] args)
{
new wb();
}
} 展开
2016-06-20
展开全部
数组是有new 了,但里面第一个元素,都要赋值 。。。。。。。。。
black[i]=new JButton();; //////之后,才能使用 。。。。。。。。
black[i]=new JButton();; //////之后,才能使用 。。。。。。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询