根据动态圆的大小在文本框中显示大小

时间:2015-03-22 15:37:17

标签: java swing jtextarea

我正在尝试创建一个绘制圆形的Java程序,滑块可以调整圆形大小,另外3个滑块控制RGB设置。问题是我无法获得要在JTextBox中显示的统计数据(直径,面积和周长)。请帮助它让我疯了!!!

谢谢!

CircleModifier.java

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class CircleModifier extends JFrame implements ChangeListener {
   public DrawingPanel drawingPanel;

   public InfoPanel infoPanel;
   public JPanel sizePanel;

   private JPanel sliderPanel;
   private JSlider sizeSlider, redColorSlider, greenColorSlider,
         blueColorSlider;
   private JLabel sizeLabel, redLabel, greenLabel, blueLabel;

   public CircleModifier() {
      super("Circle Modifier Application");
      setLayout(new BorderLayout());
      drawingPanel = new DrawingPanel();
      add(drawingPanel, BorderLayout.CENTER);
      sliderPanel = new JPanel();
      add(sliderPanel, BorderLayout.EAST);
      sliderPanel.setBackground(Color.WHITE);

      infoPanel = new InfoPanel();
      add(infoPanel, BorderLayout.SOUTH);
      sliderPanel.setLayout(new GridLayout(2, 4, 0, 1));
      sizeLabel = new JLabel("-Size-");
      sizeLabel.setForeground(Color.BLACK);
      setSizeSlider(new JSlider(JSlider.VERTICAL, 0, 350, 10));
      getSizeSlider().setMajorTickSpacing(50);
      getSizeSlider().setMinorTickSpacing(25);
      getSizeSlider().setPaintTicks(true);
      getSizeSlider().setPaintLabels(true);
      getSizeSlider().setBackground(Color.WHITE);
      getSizeSlider().addChangeListener(this);
      redColorSlider = new JSlider(JSlider.VERTICAL, 0, 255, 0);
      redColorSlider.setMajorTickSpacing(20);
      redColorSlider.setMinorTickSpacing(5);
      redColorSlider.setPaintTicks(true);
      redColorSlider.setPaintLabels(true);
      redColorSlider.setBackground(Color.WHITE);
      redColorSlider.addChangeListener(this);
      redLabel = new JLabel("-Red-");
      redLabel.setForeground(Color.RED);
      greenColorSlider = new JSlider(JSlider.VERTICAL, 0, 255, 0);
      greenColorSlider.setMajorTickSpacing(20);
      greenColorSlider.setMinorTickSpacing(5);
      greenColorSlider.setPaintTicks(true);
      greenColorSlider.setPaintLabels(true);
      greenColorSlider.setBackground(Color.WHITE);
      greenColorSlider.addChangeListener(this);
      greenLabel = new JLabel("-Green-");
      greenLabel.setForeground(Color.GREEN);
      blueColorSlider = new JSlider(JSlider.VERTICAL, 0, 255, 0);
      blueColorSlider.setMajorTickSpacing(20);
      blueColorSlider.setMinorTickSpacing(5);
      blueColorSlider.setPaintTicks(true);
      blueColorSlider.setPaintLabels(true);
      blueColorSlider.setBackground(Color.WHITE);
      blueColorSlider.addChangeListener(this);
      blueLabel = new JLabel("-Blue-");
      blueLabel.setForeground(Color.BLUE);

      sliderPanel.add(sizeLabel);

      sliderPanel.add(redLabel);
      sliderPanel.add(greenLabel);
      sliderPanel.add(blueLabel);

      sliderPanel.add(getSizeSlider());
      sliderPanel.add(redColorSlider);
      sliderPanel.add(greenColorSlider);
      sliderPanel.add(blueColorSlider);
      setSize(800, 500);

      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
   }

   public void stateChanged(ChangeEvent e) {
      int size = getSizeSlider().getValue();
      drawingPanel.setDiameter(size);
      sizeLabel.setText("-Size-");
      int red = redColorSlider.getValue();
      int green = greenColorSlider.getValue();
      int blue = blueColorSlider.getValue();
      drawingPanel.setNewCircleColor(red, green, blue);
      redLabel.setText("-Red-");
      greenLabel.setText("-Green-");
      blueLabel.setText("-Blue-");
   }

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

   public JSlider getSizeSlider() {
      return sizeSlider;
   }

   public void setSizeSlider(JSlider sizeSlider) {
      this.sizeSlider = sizeSlider;
   }
}

InfoPane.java

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

@SuppressWarnings("serial")
public class InfoPanel extends JPanel {
   JTextArea textarea;
   JLabel label;
   private JTextArea display;

   public InfoPanel() {
      setLayout(new FlowLayout());
      label = new JLabel("Information");
      add(label);
      display = new JTextArea(5, 30);
      display.setText("The Radius is: " + "\nThe Diameter is: "
            + "Dynamic diameter to display here!" + "\nThe Area is: "
            + "\nThe Circumference is: ");
      add(display);
   }
}

DrawingPanel.java

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

@SuppressWarnings("serial")
public class DrawingPanel extends JPanel {

   public static String size;
   int diameter = 1;
   int red = 255, green = 255, blue = 255;
   Color newCircleColor = new Color(red, green, blue);

   public void paintComponent(Graphics g) {
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, this.getWidth(), this.getHeight());
      g.setColor(newCircleColor);
      g.fillOval(10, 10, diameter, diameter);
   }

   public void setDiameter(int newSize) {
      diameter = newSize;
      repaint();
   }

   public void setNewCircleColor(int red, int green, int blue) {
      newCircleColor = new Color(red, green, blue);
      repaint();
   }

}

1 个答案:

答案 0 :(得分:1)

为InfoPanel提供一个公共方法,

public void textareaSetText(String text) {
    textarea.setText(text);
}

在此内部,设置JTextArea变量的文本。

然后在CircleModifier的stateChanged(...)方法中调用此方法。如果要附加文本,还可以为InfoPanel提供类似的textareaAppendText(String text)方法。