2017-07-12 5 views
0

JDesktopPaneの右下に複数行テキスト(3-4行はOK)を書いていますが、どうすればいいですか?
テキストは固定されていません。スイングアプリケーションを起動するたびに変更できますが、アプリケーションが開始されると同じままです。アプリケーションから更新する必要はありません。JDeskopPaneにテキストを書く

私の最初の考えは、イメージを作成し、それをJDesktopPaneのバックグラウンドとして配置してから書き込むことでしたが、単純な解決策ではありません。

ありがとうございました。

+0

私は(それを記述することなく)JDeskopPaneの背景として画像を入れてみましたが、画像はスケールしないと、私は上にそれを「リピート」する方法がわからない@JudeNiroshan背景 – res1

+0

'JLabel'を使用しますか?コンポーネントの 'paintComponent'メソッドをオーバーライドしますか? – MadProgrammer

答えて

2
import java.awt.AlphaComposite; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import javax.swing.JDesktopPane; 

public class MyDesktopPane extends JDesktopPane { 
    Image img; 
    public MyDesktopPane() { 
     super(); 
    } 

@Override 
public void paintComponent(Graphics g) { 
    int width = this.getWidth(); 
    int height = this.getHeight(); 
    int infoWidth = 150; 
    int infoHeight = 100; 
    super.paintComponent(g); 

    // alpha 
    final Float alpha = new Float(0.9); 
    final Graphics2D g2d = (Graphics2D) g; 
    g2d.setComposite(makeComposite(alpha.floatValue())); 

    // draw bacground image is set 
    if (img != null) { 
     g.drawImage(img, 0, 0, width, height, this); 
    } 

    //draw 3 line text in red reound rectangle 
    g.setColor(Color.RED); 
    g.fillRoundRect(width - infoWidth, height - infoHeight, infoWidth, infoHeight, 5, 5); 
    g.setColor(Color.BLACK); 

    g.drawString("Line 1", width - infoWidth + 5, height - infoHeight + 20); 
    g.drawString("Line 2", width - infoWidth + 5, height - infoHeight + 40); 
    g.drawString("Line 3", width - infoWidth + 5, height - infoHeight + 60); 

} 

public void setBackGroundImage(String path) { 
    try { 
     boolean file = new File(path).isFile(); 
     if (file) { 
      img = javax.imageio.ImageIO.read(new FileInputStream(path)); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    this.repaint(); 
} 

private AlphaComposite makeComposite(final float alpha) { 
    final int type = AlphaComposite.SRC_OVER; 
    return (AlphaComposite.getInstance(type, alpha)); 
} 
} 

、以下右下のテキストの複数行、JDesktopPaneの右隅を正当化するためにFontMetricsを用いて示して変動にprint()方法。

image

import java.awt.*; 
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 

/** @see http://stackoverflow.com/a/45055215/230513 */ 
public class JDPTest extends JDesktopPane { 

    private MyFrame one = new MyFrame("One", 100, 100); 

    public JDPTest() { 
     this.setPreferredSize(new Dimension(640, 480)); 
     this.add(one); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setColor(Color.lightGray); 
     g2d.fillRect(0, 0, getWidth(), getHeight()); 
     g2d.setColor(Color.BLACK); 
     g2d.setFont(new Font(Font.SERIF, Font.BOLD, 16)); 
     print(g2d, 3, "Hello, world!"); 
     print(g2d, 2, "This is a test."); 
     print(g2d, 1, "This is another test."); 
     print(g2d, 0, "This is still another test."); 
    } 

    private void print(Graphics2D g2d, int line, String s) { 
     FontMetrics fm = g2d.getFontMetrics(); 
     int x = this.getWidth() - fm.stringWidth(s) - 5; 
     int y = this.getHeight() - fm.getDescent() 
      - line * (fm.getHeight() + fm.getLeading()); 
     g2d.drawString(s, x, y); 
    } 

    private final class MyFrame extends JInternalFrame { 

     MyFrame(String name, int x, int y) { 
      super(name, true, true, true, true); 
      this.setSize(320, 240); 
      this.setLocation(x, y); 
      this.setVisible(true); 
     } 
    } 

    private void display() { 
     JFrame f = new JFrame("JDPTest"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(this); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new JDPTest().display(); 
      } 
     }); 
    } 
} 
+0

'paintComponent(..)'で 'super.paintComponent(g);'を呼び出す必要はありませんか? – res1

+0

@ res1:これは[_opaque property_](http://www.oracle.com/technetwork/java/painting-140037.html#props)を尊重する方法の1つです。 'fillRect()'を使うもう一つの方法は、 "四角形の範囲内に含まれるすべてのビットをペイントする"ことです。 – trashgod

2

このサンプルクラスを使用してください。 設定されている場合、背景が拡大縮小されます。 herehere見た例を組み合わせる

関連する問題