作者AI3767 (星泉幼子)
看板java
标题Re: [问题] showMessageDialog的显示位置
时间Thu Oct 15 03:29:06 2009
※ 引述《faithpiano (恩)》之铭言:
: 想要请问大家
: JOptionPane.showMessageDialog如何改变他的显示位置呢?
: 就是要自己指定位置
: 好像并不能使用setBounds这种东西..
: 谢谢罗^^
如同 s 大用的, 可以用 JOption 的 JDialog 来达到
先 new 一个专用的 JOptionPane
以一些method做设定, 像是
.setMessage() .setMessageType()
.setOptions() .setOptionType()
.setIcon()
依需求使用
然後用 .createDialog() 来拿到 JDialog
之後对拿到的 JDialog 做位置设定 .setLocation()
最後把它 show 出来, 完工!
以下为测试用原始码 J.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class J extends JFrame implements ActionListener {
private JTextField xJTF = new JTextField("0", 4);
private JTextField yJTF = new JTextField("0", 4);
private JButton showBtn = new JButton("Show");
private JOptionPane jop;
public static void main(String[] args) {
new J().setVisible(true);
} //{END main}
J() {
setSize(400,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel cp = (JPanel)getContentPane();
JPanel locPane = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
locPane.add(new JLabel("X: "));
locPane.add(xJTF);
locPane.add(new JLabel("Y: "));
locPane.add(yJTF);
cp.add(locPane, BorderLayout.CENTER);
showBtn.addActionListener(this);
cp.add(showBtn, BorderLayout.SOUTH);
jop = new JOptionPane();
} //{END constructor}
public void actionPerformed(ActionEvent ae) {
int x=0, y=0;
try { x=Integer.parseInt(xJTF.getText());
} catch(NumberFormatException nfe) { xJTF.setText("0"); }
try { y=Integer.parseInt(yJTF.getText());
} catch(NumberFormatException nfe) { yJTF.setText("0"); }
jop.setMessage("Test Message");
JDialog jd = jop.createDialog(this, "My Title");
jd.setLocation(x, y);
jd.setVisible(true);
} //{END} actionPerformed()
} //{END class} J
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.227.185.103
1F:推 faithpiano:总之还是要使用JDialog罗 因为之前有点赶 才想直接改 10/15 13:28
2F:推 faithpiano:谢谢喔 10/15 13:42