好的,所以我有一个程序在JPanel上显示一个字符串,它绘制了Ascents所在的行,它在Descent和中间画了一条线,并且介于两者之间。我有一个组合框来更改字符串的字体和大小,我有一个JTextField来更改文本本身。我使用actionListeners来更新String。每当我通过JTextField更新String的文本时,程序就会出现故障,并在JFrame的右上角显示JTextField图像的副本。
代码:
package com.Patel.RichClients;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
//a class to demonstrate
public class StringAscent extends JFrame {
private static Font font;
private int fontSize = 50;
private StringPanel panel;
private JComboBox fontOptions;
private JComboBox fontSizeOptions;
private JTextField text;
// constructor
public StringAscent() {
// set the initial font to Times new Roman
font = new Font("Agency FB", Font.PLAIN, fontSize);
setVisible(true);
setSize(520, 170);
setTitle("String Ascents");
setLayout(new BorderLayout());
//set up the components
GraphicsEnvironment ge= GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = ge.getAvailableFontFamilyNames();
fontOptions = new JComboBox(fontNames);
text = new JTextField(22);
text.setText("Swing");
String[] sizeOptions = new String[50];
for (int i = 0; i < sizeOptions.length; i ++){
sizeOptions[i] = Integer.toString(i + 1);
}
fontSizeOptions = new JComboBox(sizeOptions);
panel = new StringPanel();
//set up actionListeners
fontOptions.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JComboBox ref = (JComboBox) e.getSource();
font = new Font(fontNames[ref.getSelectedIndex()], Font.PLAIN, fontSize);
panel.repaint();
}
});
fontSizeOptions.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JComboBox ref = (JComboBox) e.getSource();
fontSize = Integer.parseInt(sizeOptions[ref.getSelectedIndex()]);
font = new Font(font.getName(), Font.PLAIN, fontSize);
panel.repaint();
}
});
text.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
});
//add components
add(panel, BorderLayout.NORTH);
add(fontOptions, BorderLayout.WEST);
add(text, BorderLayout.CENTER);
add(fontSizeOptions, BorderLayout.EAST);
}
public static void main(String[] args) {
Runnable showGui = new Runnable() {
public void run() {
StringAscent gui = new StringAscent();
}
};
SwingUtilities.invokeLater(showGui);
}
// inner JPanel class
private class StringPanel extends JPanel {
// constructor
public StringPanel() {
}
public Dimension getPreferredSize() {
return new Dimension(400, 100);
}
public void paintComponent(Graphics g) {
g.setColor(Color.black);
// set up the string
g.setFont(font);
// FontMetric is an object that holds information relevant to the
// rendering of the font
FontMetrics metric = g.getFontMetrics();
int ascent = metric.getAscent();
String string = text.getText();
g.drawString(string, 100, 50);
int x = 50;
// draw Ascent line
g.drawLine(x, 50 - ascent, x + 180, 50 - ascent);
// draw Ascent/2 line
g.drawLine(x, 50 - (ascent / 2), x + 180, 50 - (ascent / 2));
// draw Ascent/4 line
g.drawLine(x, 50 - (ascent / 4), x + 180, 50 - (ascent / 4));
// draw baseline line
g.drawLine(x, 50, x + 180, 50);
// draw Descent line
g.drawLine(x, 50 + metric.getDescent(), x + 180, 50 + metric.getDescent());
}
}
}
答案 0 :(得分:2)
将super.paintComponent(g)
添加到paintComponent
方法的开头
public void paintComponent(Graphics g) {
super.paintComponent(g)
g.setColor(Color.black);
//...
基本上,paintComponent
的一个作业是准备Graphics
上下文来绘制当前组件。在Swing中,Graphics
是一个共享资源,在绘制时窗口中的所有组件都使用它,因此在绘制它之前确保正确准备上下文非常重要
请查看Performing Custom Painting和Painting in AWT and Swing,详细了解绘画的工作原理