gpt4 book ai didi

java - 无法在 Java swing 小程序中添加文本或中心方 block

转载 作者:行者123 更新时间:2023-11-30 09:31:11 24 4
gpt4 key购买 nike

当单击“输入消息”按钮时,我无法显示字符串,而且出于某种原因我无法将框居中。当我点击 Draw square 时,它​​将绘制正方形,如果我单击“Draw it in color”,它将以彩色绘制。但是无论我做什么,文本都不会出现。这是我的代码:

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;
}


}

建议?

最佳答案

如图here , drawString() 方法需要 baseline relative坐标。这example可能有助于理解几何。

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

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

关于java - 无法在 Java swing 小程序中添加文本或中心方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12978716/

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