如何防止我的JPanel重新调整

时间:2015-04-15 16:28:18

标签: java swing

我正在制作一个模拟RGB工作方式的java程序,3个滑块和3个相应的画布,它们会变为0到255之间的颜色值。还有一个额外的画布显示三种颜色的组合。我的问题是,当我开始增加滑块上的值时,我的整个面板开始伸展。我现在一直在查看代码一段时间没有运气。我需要更改什么才能阻止面板调整或使空间更大,以便不需要调整?

package javaapplication1;

/**
 *
 * @author Latez Bradley
 */

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RGBSlider extends JFrame {
  public RGBSlider() {
    getContentPane().add(new TColor());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1400, 1000);
    setVisible(true);
  }



  public static void main(String arg[]) {
    new RGBSlider();
  }


}

class TColor extends JPanel {
  int n;
  DrawingCanvas canvas = new DrawingCanvas();
  DrawingCanvas rc = new DrawingCanvas();
  DrawingCanvas gc = new DrawingCanvas();
  DrawingCanvas bc = new DrawingCanvas();
  JLabel rgbValue = new JLabel("");
  JLabel red = new JLabel("<html>Red<br>Hex Value: "+n+"<br>Bin: "+n+"<html><br>Dec:"+n+"</html>");
  JLabel green =  new JLabel("<html>Green<br>Hex Value: "+n+"<br>Bin: "+n+"<html><br>Decimal: </html>"+n);
  JLabel blue = new JLabel("<html>Blue<br>Hex Value: "+n+"<br>Bin: "+n+"<html><br>Decimal: </html>"+n);



  JSlider sliderR, sliderG, sliderB;

  public TColor() {

    sliderR = getSlider(0, 255, 5, 255, 5);
    sliderR.setOrientation(1);
    sliderG = getSlider(0, 255, 0, 255, 5);
    sliderG.setOrientation(1);
    sliderB = getSlider(0, 255, 0, 255, 5);
    sliderB.setOrientation(1);


    JPanel panel = new JPanel();
    GridLayout myLayout = new GridLayout(4,3,200,0);
    panel.setLayout((myLayout));


    //panel.add(new JLabel("R-G-B Sliders (0 - 255)"));



    panel.add(sliderR);
    panel.add(red);
    rc.setSize(50, 50);
    panel.add(rc);

    panel.add(sliderG);
    panel.add(green);
    gc.setSize(50,50);
    panel.add(gc);

    panel.add(sliderB);
    panel.add(blue);
    bc.setSize(50,50);
    panel.add(bc);

    panel.add(new JLabel("RGB Value: ", JLabel.RIGHT));

    //rgbValue.setBackground(Color.white);
    //rgbValue.setForeground(Color.black);
    rgbValue.setOpaque(true);
    panel.add(rgbValue);


    canvas.setSize(300,300);
    add(canvas);
    add(panel);


  }

  public JSlider getSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) {
    JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init);
    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(mjrTkSp);
    slider.setMinorTickSpacing(mnrTkSp);
    slider.setPaintLabels(true);
    slider.addChangeListener(new SliderListener());
    return slider;
  }

  class DrawingCanvas extends Canvas {
    Color color;
    Color rColor;
    Color gColor;
    Color bColor;
    int redValue, greenValue, blueValue;



    public DrawingCanvas() {
      setSize(350, 350);
      color = new Color(0, 0, 0);

      setBackgroundColor();


    }

    public void setBackgroundColor() {
      color = new Color(redValue, greenValue, blueValue);
      setBackground(color);

      //red.setBackground(Color.white);
      //green.setBackground(gColor);
      //blue.setBackground(bColor);
    }
  }

  class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
      JSlider slider = (JSlider) e.getSource();

      if (slider == sliderR) {
        canvas.redValue = slider.getValue();
        rc.redValue=slider.getValue();
        displayRGBColor();
        //red.setBackground(ColorR);
      } else if (slider == sliderG) {
        canvas.greenValue = slider.getValue();
        gc.greenValue = slider.getValue();
        displayRGBColor();
      } else if (slider == sliderB) {
        canvas.blueValue = slider.getValue();
        bc.blueValue=slider.getValue();
        displayRGBColor();
      }

      rc.repaint();
      red.setIgnoreRepaint(true);
      gc.repaint();
      bc.repaint();
      canvas.repaint();
    }

      public String toBin(int x){
      String temp="";
      while(x!=0){
              temp = (x%2)+temp;
              x/=2;
      }
      return temp;
    }



    public void displayRGBColor() {
      canvas.setBackgroundColor();
      rc.setBackgroundColor();
      gc.setBackgroundColor();
      bc.setBackgroundColor();

      //rgbValue.setText(Integer.toString(canvas.color.getRGB() & 0xffffff, 16));
      rgbValue.setText("<html> Hex Value: "+Integer.toString(canvas.color.getRGB() & 0xfffffff, 16)+"<br>Bin: "+toBin(sliderR.getValue())+""+toBin(sliderG.getValue())+""+toBin(sliderB.getValue()));
      red.setText("<html>Red<br>Hex Value: "+Integer.toString(canvas.color.getRed()  & 0xffffff, 16)+"<br>Bin: "+" "+"<html><br>Dec:"+sliderR.getValue()+"</html>");
      blue.setText("<html>Blue<br>Hex Value: "+Integer.toString(canvas.color.getBlue()  & 0xffffff, 16)+"<br>Bin: "+toBin(sliderB.getValue())+"<html><br>Dec:"+sliderB.getValue()+"</html>");
      green.setText("<html>Green<br>Hex Value: "+Integer.toString(canvas.color.getGreen()  & 0xffffff, 16)+"<br>Bin: "+toBin(sliderG.getValue())+"<html><br>Dec:"+sliderG.getValue()+"</html>");
    }

  }
}

1 个答案:

答案 0 :(得分:3)

  1. 不要设置尺寸或首选尺寸。而是让组件和布局管理器为您设置大小。
  2. 在添加组件之后和设置可见之前,在JFrame上调用pack()
  3. 此处不要使用Canvas或其他AWT组件。使用JPanels或JLabel。