【问题标题】:JDialog is not showing upJDialog 没有出现
【发布时间】:2020-12-21 16:29:41
【问题描述】:

当我调用我的 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 DIAlog 的代码,我在另一个类中调用它。

public void actionPerformed(ActionEvent event) {
        if(event.getSource().equals(btnMAdd))
        {
            JMovie ne = new JMovie();
            ne.setVisible(true);
        }
    }

当我运行它时显示:

【问题讨论】:

标签: java jdialog


【解决方案1】:

首先,变量名不应以大写字符开头。有些变量是正确的,有些则不是。遵循 Java 约定并保持一致!

jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);

jmovie.add(south);
jmovie.add(west);
jmovie.add(center);

应在对话框可见之前将组件添加到对话框中。

所以代码应该是:

jmovie.add(south);
jmovie.add(west);
jmovie.add(center);

jmovie.setSize(350, 150);
jmovie.setLocation(300,300);
jmovie.setVisible(true);

您应该使用pack() 而不是 setVisible(...)。这将允许所有组件以其首选大小显示。

JFrame f = new JFrame();

另外,您不应该创建 JFrame。您要传递给 JDialog 的参数是当前可见的 JFrame。

因此,在您的 ActionListener 中,您可以使用以下逻辑获取当前帧:

Window window = SwingUtilities.windowForComponent( btnMAdd );

现在,当您创建对话框时,您将窗口作为参数传递:

JMovie ne = new JMovie(window);

您使用此值来指定对话框的父窗口。

【讨论】:

  • 当我只按下按钮时,JMovie 对话框中再次没有显示任何内容。 JMovie 对话框的解释是这样的
  • 布局说明 - 整个对话框使用 BorderLayout - 南部是使用 FlowLayout 的 JPanel,包含两个按钮 - 西部是使用 GridLayout (3x1) 的 JPanel。此面板包括 3 个使用右对齐的 JLabel。 - 中心是使用 GrdLayout (3x1) 的 JPanel。每个位置都包含一个使用 FlowLayout 并左对齐的 JPanel,每个 JPanel 都包含上面看到的三个 JTextField 之一(标题使用 15 宽,日期使用 10 宽,价格使用 6 宽)。该日期的 JPanel 还包括一个显示输入模式的禁用 JLabel
  • 我的意思是我做了他要求的一切,但又没有显示任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多