当我调用我的jdialog类时,我一次又一次地遇到这个问题,它没有显示任何内容这是jdialog接口的代码:
public JMovie() {
JFrame f = new JFrame();
JDialog jmovie = new JDialog(f,"JMovie Dialog");
jmovie.setLayout(new BorderLayout());
okbutton = new JButton("Ok");
cancelbutton = new JButton("Cancel");
title = new JLabel("Title", SwingConstants.RIGHT);
date = new JLabel("Date made on ", SwingConstants.RIGHT);
price = new JLabel("Price", SwingConstants.RIGHT);
inputdate = new JLabel("dd/mm/yyyy");
Ttitle = new JTextField(null, 15);
TMadeon = new JTextField(null, 10);
TPrice = new JTextField(null, 6);
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jb = new JPanel();
jl = new JPanel();
okbutton.addActionListener(this);
cancelbutton.addActionListener(this);
Ttitle.setName("Title");
TMadeon.setName("Date Made on");
TPrice.setName("Price");
jb.add(okbutton);
jb.add(cancelbutton);
title.setAlignmentX(Component.RIGHT_ALIGNMENT);
date.setAlignmentX(Component.RIGHT_ALIGNMENT);
price.setAlignmentX(Component.RIGHT_ALIGNMENT);
JPanel south = new JPanel(new FlowLayout());
south.add(jb, BorderLayout.SOUTH);
JPanel west = new JPanel(new GridLayout(3,1));
west.add(title,BorderLayout.WEST);
west.add(date,BorderLayout.WEST);
west.add(price,BorderLayout.WEST);
JPanel center = new JPanel(new GridLayout(3,1));
center.add(jp3,FlowLayout.LEFT);
center.add(jp2,FlowLayout.LEFT);
center.add(jp1,FlowLayout.LEFT);
inputdate.setEnabled(false);
jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);
jmovie.add(south);
jmovie.add(west);
jmovie.add(center);
}
这是接口jmovie对话框的代码,我在这里用另一个类调用它。
public void actionPerformed(ActionEvent event) {
if(event.getSource().equals(btnMAdd))
{
JMovie ne = new JMovie();
ne.setVisible(true);
}
}
当我运行时,它显示:
1条答案
按热度按时间3okqufwl1#
首先,变量名不应该以大写字符开头。有些变量是正确的,有些则不是。遵循java惯例并保持一致!
在对话框可见之前,应将组件添加到对话框中。
所以代码应该是:
你应该使用
pack()
而不是setvisible(…)。这将允许所有组件以其首选大小显示。另外,不应该创建jframe。要传递给jdialog的参数是当前可见的jframe。
因此,在actionlistener中,可以使用如下逻辑获取当前帧:
现在创建对话框时,将窗口作为参数传递:
使用此值指定对话框的父窗口。