java JPanel 中添加文本的问题。
importjava.awt.*;importjavax.swing.*;classFirstPanelextendsJPanel{publicvoidpaintComp...
import java.awt.*;
import javax.swing.*;
class FirstPanel extends JPanel{
public void paintComponent(Graphics q){
q.setColor(Color.red);
q.setFont(new Font("Times New Roman",Font.BOLD,16));
q.drawString("go away!", 120, 90);
}
}
class FirstFrame extends JFrame{
public FirstFrame(){
setTitle("this is title");
setSize(316,216);
FirstPanel fp=new FirstPanel();
add(fp);
fp.setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
public class windows {
public static void main(String[] args){
JFrame frame=new FirstFrame();
//设置用户窗体上执行close的时候使用system.exit方法退出。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //默认的窗口是不可见的。
}
}import java.awt.*;
import javax.swing.*;
class FirstPanel extends JPanel{
public void paintComponent(Graphics q){
q.setColor(Color.red);
q.setFont(new Font("Times New Roman",Font.BOLD,16));
q.drawString("go away!", 120, 90);
}
}
class FirstFrame extends JFrame{
public FirstFrame(){
setTitle("this is title");
setSize(316,216);
FirstPanel fp=new FirstPanel();
add(fp);
fp.setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
public class windows {
public static void main(String[] args){
JFrame frame=new FirstFrame();
//设置用户窗体上执行close的时候使用system.exit方法退出。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //默认的窗口是不可见的。
}
}
这是我的代码,我想让“go away!” 在窗口中居中显示,有没有什么呢方法可以调用的,而不是计算数值,这样肯定不是很准确的居中吧!! 展开
import javax.swing.*;
class FirstPanel extends JPanel{
public void paintComponent(Graphics q){
q.setColor(Color.red);
q.setFont(new Font("Times New Roman",Font.BOLD,16));
q.drawString("go away!", 120, 90);
}
}
class FirstFrame extends JFrame{
public FirstFrame(){
setTitle("this is title");
setSize(316,216);
FirstPanel fp=new FirstPanel();
add(fp);
fp.setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
public class windows {
public static void main(String[] args){
JFrame frame=new FirstFrame();
//设置用户窗体上执行close的时候使用system.exit方法退出。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //默认的窗口是不可见的。
}
}import java.awt.*;
import javax.swing.*;
class FirstPanel extends JPanel{
public void paintComponent(Graphics q){
q.setColor(Color.red);
q.setFont(new Font("Times New Roman",Font.BOLD,16));
q.drawString("go away!", 120, 90);
}
}
class FirstFrame extends JFrame{
public FirstFrame(){
setTitle("this is title");
setSize(316,216);
FirstPanel fp=new FirstPanel();
add(fp);
fp.setLayout(new FlowLayout(FlowLayout.CENTER));
}
}
public class windows {
public static void main(String[] args){
JFrame frame=new FirstFrame();
//设置用户窗体上执行close的时候使用system.exit方法退出。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //默认的窗口是不可见的。
}
}
这是我的代码,我想让“go away!” 在窗口中居中显示,有没有什么呢方法可以调用的,而不是计算数值,这样肯定不是很准确的居中吧!! 展开
展开全部
package needl;
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询