アップレットに小さい子ウィンドウ(JPanel)・Javaの小技、サンプル、ニュースなどを紹介していきます。みんなで参考にしてください。

Ads by Google


上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

アップレットに小さい子ウィンドウ(JPanel)



アップレットに小さい子ウィンドウを貼り付けるには以下のように実装します。
参考にしてください。




public class MsgPanel extends JPanel{
/**
* 親コンポーネント
*/
Component parent;
/**
* パネルを閉じるラベル
*/
public JLabel closeLabel;
/**
* パネルを閉じるボタン
*/
public JPanel closePanel;
/**
* メッセージテキスト
*/
public JTextArea msgText;
/**
* 現在のパネルの開閉状態
*/
public boolean bOpen;
/**
* 推奨サイズ
*/
protected Dimension prefSize = new Dimension(200, 70);
/**
* 閉じる直前の座標値
*/
protected Point location;
/**
* 移動のときに使用
*/
Point offset = null;

/**
* コンストラクタ
*/
public MsgPanel(Component c)
{
super(new BorderLayout());
setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));

parent = c;

createComponents();

bOpen = true;
}

/**
* コンポーネントを作成します。
*/
protected void createComponents()
{
JPanel pnl = new JPanel(new BorderLayout());
//閉じるボタンをラベルで作成する。
closePanel = new JPanel(new BorderLayout());
closeLabel = new JLabel("×");

closeLabel.setBorder(BorderFactory.createEtchedBorder());

//ヘルプパネルの開閉
closeLabel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
//setVisible(false);
if (bOpen) {
location = getLocation();
bOpen = false;
closeLabel.setText(null);
Icon icon = new ImageIcon(getClass().getResource("images/help.gif"));
closeLabel.setIcon(icon);
Dimension dim = new Dimension(icon.getIconWidth()+2, icon.getIconHeight());
closeLabel.setPreferredSize(dim);
closePanel.setPreferredSize(dim);
remove(msgText);
setSize(new Dimension(dim.width+1, dim.height+1));
Dimension gdim = parent.getSize();
setLocation(gdim.width-dim.width-2, gdim.height-dim.height-2);
}
else {
bOpen = true;
closeLabel.setText("×");
closeLabel.setIcon(null);
closeLabel.setPreferredSize(new Dimension(12, 12));
closePanel.setPreferredSize(new Dimension(12, 12));
add(msgText, BorderLayout.CENTER);
setSize(prefSize);
setLocation(location.x, location.y);
}
}
});

closePanel.add(closeLabel, BorderLayout.WEST);
//pnl.setBounds(0, 0, 12, 12);
closeLabel.setPreferredSize(new Dimension(12, 12));
closePanel.setPreferredSize(new Dimension(12, 12));
pnl.add(closePanel, BorderLayout.WEST);

//ヘルプ文字のコンポーネント
msgText = new JTextArea(3, 10);
msgText.setWrapStyleWord(true);
msgText.setLineWrap(true);
msgText.setBackground(new Color(255, 255, 180));
msgText.update(msgText.getGraphics());
add(msgText, BorderLayout.CENTER);

setPreferredSize(prefSize);

//移動バー
JPanel bar = new JPanel();
bar.setBorder(BorderFactory.createEtchedBorder());
pnl.add(bar, BorderLayout.CENTER);
add(pnl, BorderLayout.NORTH);

// ヘルプパネルの移動
bar.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
offset = e.getPoint();
offset.x += getX();
offset.y += getY();
}
});
bar.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Point current = e.getPoint();
current.x += getX();
current.y += getY();
Point move = new Point(getX() + (current.x-offset.x),
getY() + (current.y-offset.y));
if (move.x < 0) move.x = 0;
if (move.y < 0) move.y = 0;
if (move.x > parent.getWidth()-getWidth())
move.x = parent.getWidth()-getWidth();
if (move.y > parent.getHeight()-getHeight())
move.y = parent.getHeight()-getHeight();

setLocation(move);

int mrg = 3;
if (current.x < 0) current.x = 0;
if (current.y < 0) current.y = 0;
if (current.x > parent.getWidth()-mrg)
current.x = parent.getWidth()-mrg;
if (current.y > parent.getHeight()-getHeight())
current.y = parent.getHeight()-getHeight();

offset = current;
}
});
}

/**
* メッセージ文字の設定
*/
public void setText(String txt)
{
msgText.setText(txt);
msgText.paintImmediately(msgText.getVisibleRect());
}
}