import javax.swing.*
import java.awt.*
import java.awt.event.*
import java.awt.geom.*
import java.util.*
final class FontColor extends JFrame {
final static private Dimension SIZE = new Dimension(800, 600)
public String getTitle() {return "FontColor"}
public Dimension getPreferredSize() {return SIZE}
public Dimension getMinimumSize() {return SIZE}
public Dimension getMaximumSize() {return SIZE}
public Dimension getSize() {return SIZE}
private Canvas canvas
private Brush brush
FontColor() throws HeadlessException {
init()
attachListeners()
doLay()
setVisible(true)
}
private void init() {
brush = new StringBoundBrush()
canvas = new Canvas(brush)
}
private void attachListeners() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1)
}
})
}
private void doLay() {
Container container = getContentPane()
container.add(canvas, BorderLayout.CENTER)
pack()
}
public static void main(String... args) {
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
SwingUtilities.invokeLater(FontColor::new)
}
private interface Brush extends Observer {
/**
* do paint action
* @param g the graphics context in you application
*/
void paint(Graphics g)
/**
* The component that this brush hold
* @return component instance
*/
Component getOwner()
void setOwner(Component owner)
}
private static final class StringBoundBrush implements Brush {
private Component owner
private final static AffineTransform AFFINE_TRANSFORM = AffineTransform.getRotateInstance(45f/180f*Math.PI)
private final static String CHAR="你好啊!"
public StringBoundBrush() {
AFFINE_TRANSFORM.preConcatenate(AffineTransform.getTranslateInstance(100, 100))
}
public Component getOwner() {
return owner
}
public void setOwner(Component owner) {
this.owner = owner
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create()
g2.setTransform(AFFINE_TRANSFORM)
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON)
g2.setColor(Color.PINK)
g2.setFont(g2.getFont().deriveFont(34f))
g2.fill( g2.getFontMetrics().getStringBounds(CHAR,g2) )
g2.setColor(Color.BLUE)
g2.drawString(CHAR,0,0)
g2.dispose()
}
public void update(Observable o, Object arg) {
if (owner != null) owner.repaint()
}
}
/**
* you canvas
*/
private class Canvas extends JComponent {
private Brush brush
private Canvas(Brush brush) {
super()
this.brush = brush
brush.setOwner(this)
}
protected void paintComponent(Graphics g) {
super.paintComponent(g)
if (brush == null) return
brush.paint(g)
}
}
}
JFrame的默认布局管理器是BorderLayout,BorderLayout将容器(也就是JFrame)分成东
西
南
北
中
五个部分,向JFrame添加控件时BorderLayout默认将控件的位置设置为中间位置,所以你的设置不起作用
看了你的问题是想通过坐标控制控件,所以要将JFrame的布局管理器设置为绝度布局,即
mainFrame.setLayout(null)将布局管理器设置为null.
向其中动态添加文字有两种实现方式
1、是重写JPanel的paintComponent(Graphics
g)方法2、是动态添加JLabel
ImageIcon icon=new ImageIcon(getClass.getResource(url))//url 图片的名字,该图片的路径必须和你swing界面在同一包下。然后你再 JLabel label=new JLabel(icon)//把图标放入标签上,最后再把Label放入JPanel上。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)