2

单击“输入消息”按钮时,我无法显示字符串,并且由于某种原因我无法使框居中。当我单击绘制方块时,它将绘制方块,如果我单击“以颜色绘制”,它将以彩色绘制。但是,无论我做什么,文本都不会出现。这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;


public class Lab9 extends JApplet implements ActionListener, ItemListener {

 private JRadioButton square;
 private JRadioButton message;
 private JTextField messageField;
 private String messageEntered = "";
 private JComboBox location;
 private JPanel top;
 private JPanel bottom;
 private JCheckBox drawColor;
 private JButton draw;
 private int newWidth;
 private int newHeight;
 private static final String locations[] = {"Centered", "Random location"};
 private boolean drawSquare;
 private Color color;

@Override
public void init() {
    setLayout(new FlowLayout());
    top = new JPanel();
    bottom = new JPanel();

    square = new JRadioButton("Draw square");
    square.addItemListener(this);
    top.add(square);

    message = new JRadioButton("Enter your message:");
    message.addItemListener(this);
    top.add(message);

    messageField = new JTextField(20);
    messageField.addActionListener(this);
    top.add(messageField);

    //bottom panel
    bottom.add(new JLabel("Select where to draw:"));
    location = new JComboBox(locations);
    bottom.add(location);

    drawColor = new JCheckBox("Draw in color");
    drawColor.addItemListener(this);
    bottom.add(drawColor);

    draw = new JButton("Draw it!");
    draw.addActionListener(this);
    bottom.add(draw);

    add(top);
    add(bottom);

}

 @Override 
 public void paint(Graphics g) {
     super.paint(g); 

     g.setColor(color);

     if(drawSquare){

         g.fillRect(newWidth, newHeight, 200, 200);
     }

     else {
         g.setColor(color);
         g.drawString(messageEntered, newWidth, newHeight); // print out entered name 
     }
 }

@Override
public void actionPerformed(ActionEvent e) {

    repaint();
}

@Override
public void itemStateChanged(ItemEvent e) {
    int width = getWidth();
    int height = getHeight();
    Random rand = new Random();

    if(e.getSource() == location && location.getSelectedIndex() == 1) {

         newWidth = rand.nextInt(width) + 20;
         newHeight = rand.nextInt(height) + 20;
    }

    if (e.getSource() == location && location.getSelectedIndex() == 0) {
        newWidth = width/2-100;
        newHeight = height/2-100;
    }

    if(e.getSource() == square && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = true;
    }

    if(e.getSource() == message && e.getStateChange() == ItemEvent.SELECTED) {
        drawSquare = false;
        messageEntered = messageField.getText();
    }

    if (e.getSource() == drawColor && e.getStateChange() == ItemEvent.SELECTED) {
        color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
    }

    if(e.getSource() == drawColor && e.getStateChange() == ItemEvent.DESELECTED) {
        color = Color.BLACK;
    }


}

建议?

4

1 回答 1

2

如此处所示,该drawString()方法需要基线相对坐标。这个例子可能有助于理解几何。

FontMetrics fm = g.getFontMetrics();
g.drawString(messageEntered, newWidth, newHeight + fm.getAscent());

顺便说一句,考虑覆盖paintComponent()aJPanel并将其添加到您的小程序中。

于 2012-10-19T17:28:05.473 回答