单击一个JButton如何弹出一个新的JDialog
监听一个JButton,如何在单击的时候弹出一个JDialog?JButtonoval=newJButton("椭圆");oval.addActionListener(n...
监听一个JButton,如何在单击的时候弹出一个JDialog?
JButton oval=new JButton("椭圆");
oval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JDialog dialogOval=new JDialog(PadControl.this,"Draw Oval");
JDialog.setDefaultLookAndFeelDecorated(true);
JLabel mOval=new JLabel("Please input height and width(not 0!):");
dialogOval.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
JTextField rheight=new JTextField();
h=Integer.parseInt(rheight.getText().trim());
JTextField rwidth=new JTextField();
w=Integer.parseInt(rwidth.getText().trim());
JButton dOval=new JButton("Draw");
dOval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// drawOval();
}
});
JButton fOval=new JButton("Fill");
fOval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// fillOval();
}
});
Container cOval=getContentPane();
cOval.add(mOval);
cOval.add(rheight);
cOval.add(rwidth);
cOval.add(dOval);
cOval.add(fOval);
dialogOval.setVisible(true);
}
});
这是我写的……但是JDialog不能弹出来……
请各位高手指教~~~谢谢了! 展开
JButton oval=new JButton("椭圆");
oval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JDialog dialogOval=new JDialog(PadControl.this,"Draw Oval");
JDialog.setDefaultLookAndFeelDecorated(true);
JLabel mOval=new JLabel("Please input height and width(not 0!):");
dialogOval.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
JTextField rheight=new JTextField();
h=Integer.parseInt(rheight.getText().trim());
JTextField rwidth=new JTextField();
w=Integer.parseInt(rwidth.getText().trim());
JButton dOval=new JButton("Draw");
dOval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// drawOval();
}
});
JButton fOval=new JButton("Fill");
fOval.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
// fillOval();
}
});
Container cOval=getContentPane();
cOval.add(mOval);
cOval.add(rheight);
cOval.add(rwidth);
cOval.add(dOval);
cOval.add(fOval);
dialogOval.setVisible(true);
}
});
这是我写的……但是JDialog不能弹出来……
请各位高手指教~~~谢谢了! 展开
1个回答
展开全部
你贴的代码编译不过,我只看出一个问题,剩下的太乱我没看,用你的代码改了一下随便写了个新的
dialogOval.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
这句话得改了,子窗口的关闭操作不能是这个,最上层窗口才可以,改成
dialogOval.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
你和我的代码比较下不同
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JobFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static int width = 480;
private static int height = 300;
private JButton oval = new JButton("椭圆");
public JobFrame() {
super("aa");
this.setLayout(new GridLayout(4, 1));
this.setBounds(new Rectangle(200, 200, width, height));
JPanel p = new JPanel();
oval.addActionListener(this);
p.add(oval);
this.add(p);
JPanel p1 = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JobFrame();
}
public void actionPerformed(ActionEvent e) {
JDialog dialogOval = new JDialog(this, "Draw Oval");
JDialog.setDefaultLookAndFeelDecorated(true);
dialogOval.setBounds(new Rectangle(200, 200, 200, 200));
JLabel mOval = new JLabel("Please input height and width(not 0!):");
dialogOval.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
JTextField rheight = new JTextField();
int h = 100;
JTextField rwidth = new JTextField();
int w = 200;
JButton dOval = new JButton("Draw");
dOval.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// drawOval();
}
});
JButton fOval = new JButton("Fill");
fOval.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// fillOval();
}
});
Container cOval = getContentPane();
cOval.add(mOval);
cOval.add(rheight);
cOval.add(rwidth);
cOval.add(dOval);
cOval.add(fOval);
dialogOval.setVisible(true);
}
}
dialogOval.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
这句话得改了,子窗口的关闭操作不能是这个,最上层窗口才可以,改成
dialogOval.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
你和我的代码比较下不同
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class JobFrame extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static int width = 480;
private static int height = 300;
private JButton oval = new JButton("椭圆");
public JobFrame() {
super("aa");
this.setLayout(new GridLayout(4, 1));
this.setBounds(new Rectangle(200, 200, width, height));
JPanel p = new JPanel();
oval.addActionListener(this);
p.add(oval);
this.add(p);
JPanel p1 = new JPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new JobFrame();
}
public void actionPerformed(ActionEvent e) {
JDialog dialogOval = new JDialog(this, "Draw Oval");
JDialog.setDefaultLookAndFeelDecorated(true);
dialogOval.setBounds(new Rectangle(200, 200, 200, 200));
JLabel mOval = new JLabel("Please input height and width(not 0!):");
dialogOval.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
JTextField rheight = new JTextField();
int h = 100;
JTextField rwidth = new JTextField();
int w = 200;
JButton dOval = new JButton("Draw");
dOval.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// drawOval();
}
});
JButton fOval = new JButton("Fill");
fOval.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// fillOval();
}
});
Container cOval = getContentPane();
cOval.add(mOval);
cOval.add(rheight);
cOval.add(rwidth);
cOval.add(dOval);
cOval.add(fOval);
dialogOval.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询