Soo用户可以选择文本"Welcome to Java Programming"文本是斜体、粗体还是居中。这些选项是复选框。我不知道如何制作,这样当用户选择中心时,它就会在中心。
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRoundRect(75,90,324,120,10,10);
g.drawLine(183,90,183,210);
g.setColor(currentC);
g.setFont(new Font(currentFont, intBold + intItalic, 24));
g.drawString("Welcome to Java Programming",30,70);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == checkBoxBold) {
if(e.getStateChange() == ItemEvent.SELECTED)
intBold = Font.BOLD;
if(e.getStateChange() == ItemEvent.DESELECTED)
intBold = Font.PLAIN;
}
if(e.getSource() == checkBoxItalics) {
if(e.getStateChange() == ItemEvent.SELECTED)
intItalic = Font.ITALIC;
if(e.getStateChange() == ItemEvent.DESELECTED)
intItalic = Font.PLAIN;
}
if(e.getSource() == checkBoxCenter) {
if(e.getStateChange() == ItemEvent.SELECTED)
//PROBLEM RIGHT HERE
if(e.getStateChange() == ItemEvent.DESELECTED)
}
if(e.getSource() == radioRed)
currentC = Color.red;
else if(e.getSource() == radioGreen)
currentC = Color.green;
else if(e.getSource() == radioBlue)
currentC = Color.blue;
if(e.getSource() == fontChoice)
currentFont = fontNames[fontChoice.getSelectedIndex()];
repaint();
}
使用FontMetrics:
FontMetrics fm = g.getFontMetrics();
要获得文本宽度,请使用:
fm.stringWidth(text)
然后在上输出您的文本
width / 2 - fm.stringWidth(text) / 2
其中width是输出区域的宽度。