单击一个JButton如何弹出一个新的JDialog

发布网友 发布时间:2024-10-23 21:53

我来回答

1个回答

热心网友 时间:2024-10-26 03:59

你贴的代码编译不过,我只看出一个问题,剩下的太乱我没看,用你的代码改了一下随便写了个新的
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);
}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com