gpt4 book ai didi

java - 如何防止我的 JPanel 重新调整

转载 作者:行者123 更新时间:2023-11-29 05:08:17 25 4
gpt4 key购买 nike

我正在制作一个模拟 RGB 工作原理的 java 程序,3 个 slider 和 3 个相应的 Canvas ,颜色值在 0 到 255 之间变化。还有一个额外的 Canvas 显示三种颜色的组合。我的问题是,当我开始增加 slider 上的值时,我的整个面板开始拉伸(stretch)。我一直在看代码,但没有运气。我需要更改什么以防止面板调整或使空间更大而无需调整?

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. 不要设置尺寸或首选尺寸。而是让组件和布局管理器为您设置尺寸。
  2. 在添加组件之后和设置可见之前调用 JFrame 上的 pack()
  3. 不要在此处使用 Canvas 或其他 AWT 组件。使用 JPanel 或 JLabel。

关于java - 如何防止我的 JPanel 重新调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29655751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com