我開發一個Swing應用程序有一個橢圓形的按鈕,其輸出如下所示,代碼幫助如下: -需要理解Swing代碼
代碼: -
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class AlphaCompositeDemo extends JFrame{
AlphaCompositeDemo()
{
super("AlphaComposite Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setLayout(new FlowLayout());
setBackground(new Color(0.2f,0.7f,0.1f,0.4f));
comp c=new comp();
add(c);
add(new JButton("Click"));
setVisible(true);
}
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
SwingUtilities.invokeLater(new Runnable(){public void run(){new AlphaCompositeDemo();}});
}
}
class comp extends JComponent
{
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g.create();
g2.setComposite(AlphaComposite.SrcOver);
g2.setColor(Color.RED);
g2.fillOval(50, 50, 220, 120);
}
public Dimension getPreferredSize()
{
return new Dimension(200,200);
}
}
現在我有以下問題:
- 如果我指定的x,y座標爲橢圓形,那麼爲什麼當窗口調整大小時它會從它的位置移開? (雖然我知道由於FlowLayout它在中心對齊,但它違反了必須固定的屬性,因爲我已指定座標x,y)。
- 第二,如果輸出是顯而易見的(我並不期待),那麼我指定的x,y座標是w.r.t哪個角?
另請參閱本[示例](https://sites.google.com/site/drjohnbmatthews/composite)。 – trashgod