///////////这个是程序的其余部分
private Canvas colorPool = new Canvas();//show the current chosen color
private JButton okButton = new JButton("OK");
private static JFrame noFrame = null;
public PaletteDialog()
{
super(noFrame, "Choose Your Favourite Color", true);
this.setResizable(true);//???should be flase
Container cp = getContentPane();
colorPool.setBackground(INIT_COLOR);//set default foreground Color
okButton.addActionListener(okL);
okButton.setSize(50,30);
middlePanel.setLayout(new GridLayout(2, 1));
middlePanel.add(colorPool);
middlePanel.add(okButton);
rightPanel.setLayout(new GridLayout(6, 2));
rText.setHorizontalAlignment(JTextField.CENTER);
rText.addActionListener(textL);
rText.setText(Integer.toString(255));
rightPanel.add(rLabel);
rightPanel.add(rText);
gText.setHorizontalAlignment(JTextField.CENTER);
gText.addActionListener(textL);
gText.setText(Integer.toString(255));
rightPanel.add(gLabel);
rightPanel.add(gText);
bText.setHorizontalAlignment(JTextField.CENTER);
bText.addActionListener(textL);
bText.setText(Integer.toString(255));
rightPanel.add(bLabel);
rightPanel.add(bText); //float[] temp = Color.RGBtoHSB(0xff, 0xff, 0xff, null);//get the corresponding HSB array
rightPanel.add(hLabel);
hNumLabel.setText(Float.toString((float)0.0));
rightPanel.add(hNumLabel);
rightPanel.add(sLabel);
sNumLabel.setText(Float.toString((float)0.0));
rightPanel.add(sNumLabel);
rightPanel.add(brLabel);
bNumLabel.setText(Float.toString((float)0.8));
rightPanel.add(bNumLabel); brightness.addChangeListener(new ChangeListener()// listener for the slider
{
public void stateChanged(ChangeEvent e)
{
int value = ((JSlider)e.getSource()).getValue();
bNumLabel.setText(Float.toString((float)value/200));
Color temp = Color.getHSBColor(Float.parseFloat(hNumLabel.getText()),
Float.parseFloat(sNumLabel.getText()),
(float)value/200); changeColor(temp);
}
});
showAllColors.setSize(200,200);
brightness.setValue(160);
cp.setLayout(new GridLayout(1, 4));
cp.add(showAllColors);
cp.add(brightness);
cp.add(middlePanel);
cp.add(rightPanel);
//resize the current Dialog to proper size
int dialogWidth = d.width, dialogHeight = d.height;
setSize(dialogWidth*5/8, dialogHeight/3);
setLocation((dialogWidth - getSize().width) / 2, (dialogHeight - getSize().height) / 2);//let the whole dialog window located in the center }//constructor
public static void main(String[] args)
{
new PaletteDialog().show();
} private class ShowAllColors extends Canvas implements MouseListener, MouseMotionListener
{
public static final float DEFAULT_BRIGHTNESS = (float)0.8;
private float h, s, b;//hue, saturation, brightness
public void paint(Graphics g)
{
//whole 200*200 pixel while each box 2*2
for(h = 0; h < 1.0; h += 0.01)//draw ff*ff colors
{
for(s = 0; s < 1.0; s += 0.01)//draw the upper part of the palette
{
g.setColor( Color.getHSBColor(h, s, (float)DEFAULT_BRIGHTNESS));
g.fillRect((int)(h * 200), (int)(s * 200), 2, 2); //draw the upper part of the palette
}
}
}//paint public ShowAllColors()
{
setSize(200,200);
addMouseListener(this);
addMouseMotionListener(this);
}//constructor
public void mouseReleased(MouseEvent e)
{
handling(e);
}
public  void mouseDragged(MouseEvent e)
{
handling(e);
}
private void handling(MouseEvent e)// listener for ShowAll colors
{
buttonFlag = e.getButton();
Point temp = e.getPoint();
float b = (float)brightness.getValue()/200;
float h = (float)temp.x/200;
float s = (float)temp.y/200;
if(b >= 1 || s >= 1 || h >= 1)//illegal value
  return;
if(b <0 || s < 0 || h < 0)
  return;
Color tempColor = Color.getHSBColor(h, s, b);
changeColor(tempColor); bNumLabel.setText(Float.toString(b));
hNumLabel.setText(Float.toString(h));
sNumLabel.setText(Float.toString(s));
}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e)
{
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseExited(MouseEvent e)
{
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void mousePressed(MouseEvent e) {} }//private class Show_All_Colors
private void changeColor(Color changeColor)//for AllColors and brightness only
{
colorPool.setBackground(changeColor);
//privateColor = changeColor;???no need?
rText.setText(Integer.toString(changeColor.getRed()));
gText.setText(Integer.toString(changeColor.getGreen()));
bText.setText(Integer.toString(changeColor.getBlue()));
}
}

解决方案 »

  1.   

    用setPrefererSize(new Dimension(200, 200)试
      

  2.   

    问题已经解决,不过你说的办法我试了,也是不行,不过受你的建议的启发,我意识到我的JPanel被压扁的原因在于整个JDialog的宽度太小了,GridLayout布局管理器在宽度不够的情况下自动把JPanel压扁了.
    不过再请高手回答一个小问题,就马上结贴:Java中有没有类似于C++中默认函数参数的语法?public void function(int i = 3, int j = 2)
    {
    }
    只需要回答 有还是没有,这个问题困扰了我们很长时间了,谢谢.
      

  3.   

    问题已经解决,不过你说的办法我试了,也是不行,不过受你的建议的启发,我意识到我的JPanel被压扁的原因在于整个JDialog的宽度太小了,GridLayout布局管理器在宽度不够的情况下自动把JPanel压扁了.
    不过再请高手回答一个小问题,就马上结贴:Java中有没有类似于C++中默认函数参数的语法?public void function(int i = 3, int j = 2)
    {
    }
    只需要回答 有还是没有,这个问题困扰了我们很长时间了,谢谢.