简体   繁体   English

如何在Java中的Jpanel中添加3个矩形?

[英]How to add 3 rectangles to Jpanel in java?

I'm trying to add three rectangles to the center of BorderLayout and I'm completely lost. 我试图将三个矩形添加到BorderLayout的中心,我完全迷路了。 My finished program needs to increase the height of the rectangle as the sliders move but I'm trying to figure out how to intitally draw these three rectangles to the jpanel. 我完成的程序需要随着滑块的移动来增加矩形的高度,但是我试图弄清楚如何将这三个矩形绘制到jpanel上。 I'm so lost. 我很迷路。 My code is below. 我的代码如下。

 import java.awt.*;
 import javax.swing.*;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;

 public class ShowColors extends JPanel
 {

public static void main(String args[])
{
    JFrame frame = new JFrame();

    JPanel main = new JPanel(new BorderLayout());
    main.setSize(2000, 1000);
    frame.setContentPane(main);

    JPanel jp1 = new JPanel(new GridLayout(0, 1));
    JPanel jp2 = new JPanel(new GridLayout(2,3));
    JPanel jp3 = new JPanel(new GridLayout(1, 3));

    jp1.setPreferredSize(new Dimension(90, 800));
    jp2.setPreferredSize(new Dimension(1000, 150));
    jp3.setPreferredSize(new Dimension(800, 600));

    JRadioButton rb1 = new JRadioButton("Decimal", true);
    JRadioButton rb2 = new JRadioButton("Binary");
    JRadioButton rb3 = new JRadioButton("Hex");
    JRadioButton rb4 = new JRadioButton("Octal");
    JButton jb1 = new JButton("RESET");

    ButtonGroup group = new ButtonGroup();
    group.add(rb1);
    group.add(rb2);
    group.add(rb3);
    group.add(rb4);

    JSlider jRed = new JSlider(0,255);
    JSlider jGreen = new JSlider(0,255);
    JSlider jBlue = new JSlider(0,255);

    jRed.setPaintLabels(true);
    jRed.setPaintTicks(true);
    jRed.setMinorTickSpacing(5);
    jRed.setMajorTickSpacing(50);
    jRed.setValue(0);

    jGreen.setPaintLabels(true);
    jGreen.setPaintTicks(true);
    jGreen.setMinorTickSpacing(5);
    jGreen.setMajorTickSpacing(50);
    jGreen.setValue(0);

    jBlue.setPaintLabels(true);
    jBlue.setPaintTicks(true);
    jBlue.setMinorTickSpacing(5);
    jBlue.setMajorTickSpacing(50);
    jBlue.setValue(0);

    JLabel labelR = new JLabel("Red", JLabel.CENTER);
    JLabel labelG = new JLabel("Green", JLabel.CENTER);
    JLabel lableB = new JLabel("Blue", JLabel.CENTER);

    jp1.add(rb1);
    jp1.add(rb2);
    jp1.add(rb3);
    jp1.add(rb4);
    jp1.add(jb1);

    jp2.add(labelR);
    jp2.add(labelG);
    jp2.add(lableB);
    jp2.add(jRed);
    jp2.add(jGreen);
    jp2.add(jBlue);

    main.add(jp1, BorderLayout.WEST);
    main.add(jp2, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void paint(Graphics g)
{
    super.paint(g);
    g.drawRect(0, 0, 10, 20);
    g.setColor(Color.RED);
    g.fillRect(0, 0, 10, 20);

    g.drawRect(10, 0, 10, 20);
    g.setColor(Color.GREEN);
    g.fillRect(10, 0, 10, 20);

    g.drawRect(20, 0, 10, 20);
    g.setColor(Color.BLUE);
    g.fillRect(20, 0, 10, 20);
}

}

here is my layout and i want the rectangles in the center. 这是我的布局,我想在中心放置矩形。 样本输出

  1. Don't override paint , painting in Swing is achieved by a delicate and complicated chain of methods, which is easily broken. 不要覆盖paint ,画在Swing是由方法一个微妙而复杂的链条,这是很容易折断实现。 Instead, override it's paintComponent method instead. 而是改写它的paintComponent方法。 See Painting in AWT and Swing for more details 有关更多详细信息,请参见在AWT和Swing中绘画
  2. Having said that, don't add all your components to the same panel onto which you want to draw, you'll end up painting underneth all the components. 话虽如此,不要将所有组件都添加到要在其上绘制的同一面板上,您最终将在所有组件下绘画。 Instead, create a separate JPanel which acts as the paint surface and another JPanel which acts as the controller (containing the controls and the paint surface). 而是创建一个单独的JPanel作为油漆表面,另一个JPanel作为控制器(包含控件和油漆表面)。 Use setters and getters to change the state of the paint surface. 使用安装员和吸气剂改变油漆表面的状态。 See Performing Custom Painting for more details. 有关更多详细信息,请参见执行自定义绘画
  3. Create some kind of "drawable" object, which knows how to paint itself and what it should use to paint itself (the color) 创建某种“可绘制”对象,该对象知道如何绘制自身以及绘制自己应使用的对象(颜色)
  4. Create some kind of List (in the paint surface class) which can hold the objects which you want to paint. 创建某种类型的List (在绘画表面类中),该列表可以容纳要绘画的对象。 Within in it's paintComponent you will will loop through the list and request that each object paint itself, passing it the Graphics context. 在它的paintComponent您将遍历列表并请求每个对象自我绘制,并向其传递Graphics上下文。 Take a look at 2D Graphics for more details 查看2D图形以了解更多详细信息

This previous answer may also help 先前的答案可能也有帮助

I think you just need a simple subclass of JPanel and override paintComponent() . 我认为您只需要一个JPanel的简单子类并重写paintComponent() Something like this should get you going: 这样的事情应该可以帮助您:

import javax.swing.*;
import java.awt.*;

 public class Canvas extends JPanel {

   // TODO member variables for rectangle size/color

   public void paintComponent(Graphics g) {
     super.paintComponent(g);
     g.fillRect(10,10,100,50);
     g.drawRect(10,80,100,50);
  }
}

EDIT : Actually, I guess you really don't need a "Canvas" class, you can just use a plain JPanel as @MadProgrammer suggests. 编辑 :实际上,我想您真的不需要“ Canvas”类,您可以按照@MadProgrammer的建议使用普通的JPanel What you do need is a class that will encapsulate the Rectangle behavior, which can just be a simple JComponent that gets added to the JPanel that holds your three rectangles. 您需要做的是一个封装Rectangle行为的类,该类可以只是添加到保存三个矩形的JPanel的简单JComponent

Here's a working solution, imports excluded for brevity: 这是一个可行的解决方案,为简洁起见,不包括进口产品:

public class ShowColors {

    class Rectangle extends JComponent implements ChangeListener {
        private JSlider slider; 
        private Color color;

        public Rectangle(JSlider slider, Color color) {
            this.slider = slider;
            this.color = color;
            this.setPreferredSize(new Dimension(250, 250));
            slider.addChangeListener(this);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            int value = slider.getValue();
            g.setColor(color);
            g.fillRect(10,10,100,value);

        }

        @Override
        public void stateChanged(ChangeEvent arg0) {
            this.repaint();
        }
    }

    public ShowColors() {
        JFrame frame = new JFrame();

        JPanel main = new JPanel(new BorderLayout());
        main.setSize(2000, 1000);
        frame.setContentPane(main);

        JPanel jp1 = new JPanel(new GridLayout(0, 1));
        JPanel jp2 = new JPanel(new GridLayout(2, 3));

        jp1.setPreferredSize(new Dimension(90, 800));
        jp2.setPreferredSize(new Dimension(1000, 150));

        JRadioButton rb1 = new JRadioButton("Decimal", true);
        JRadioButton rb2 = new JRadioButton("Binary");
        JRadioButton rb3 = new JRadioButton("Hex");
        JRadioButton rb4 = new JRadioButton("Octal");
        JButton jb1 = new JButton("RESET");

        ButtonGroup group = new ButtonGroup();
        group.add(rb1);
        group.add(rb2);
        group.add(rb3);
        group.add(rb4);

        JSlider jRed = buildSlider();
        JSlider jGreen = buildSlider();
        JSlider jBlue = buildSlider();

        JLabel labelR = new JLabel("Red", JLabel.CENTER);
        JLabel labelG = new JLabel("Green", JLabel.CENTER);
        JLabel lableB = new JLabel("Blue", JLabel.CENTER);

        jp1.add(rb1);
        jp1.add(rb2);
        jp1.add(rb3);
        jp1.add(rb4);
        jp1.add(jb1);

        jp2.add(labelR);
        jp2.add(labelG);
        jp2.add(lableB);
        jp2.add(jRed);
        jp2.add(jGreen);
        jp2.add(jBlue);

        JPanel canvas = new JPanel();
        canvas.setLayout(new FlowLayout());
        canvas.setPreferredSize(new Dimension(800, 600));
        canvas.add(new Rectangle(jRed, Color.RED));
        canvas.add(new Rectangle(jGreen, Color.GREEN));
        canvas.add(new Rectangle(jBlue, Color.BLUE));

        main.add(jp1, BorderLayout.WEST);
        main.add(jp2, BorderLayout.SOUTH);
        main.add(canvas, BorderLayout.EAST);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static JSlider buildSlider() {
        JSlider slider = new JSlider(0, 255);
        slider.setPaintLabels(true);
        slider.setPaintTicks(true);
        slider.setMinorTickSpacing(5);
        slider.setMajorTickSpacing(50);
        slider.setValue(50);
        return slider;
    }

    public static void main(String args[]) {
        new ShowColors();
    }

}

And here's what it looks like: 这是它的样子:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM