【问题标题】:How to display a frame with GUI intelliJ如何使用 GUI intelliJ 显示框架
【发布时间】:2016-11-28 11:27:03
【问题描述】:

我正在使用 IntelliJ GUI 功能,但我正在努力显示框架。

这是我的 GUI 组件的图片:

这是我在 IDE 中用来配置它的代码:

public class SftpDoc extends JPanel{

private JRadioButton radioButton1;
private JRadioButton radioButton2;
private JRadioButton radioButton3;
private JButton button1;
private JComboBox comboBox1;
private JTextField textField1;
private JButton browseButton;
private JButton button2;
private final static JPanel panel = new JPanel();

public SftpDoc(){

    panel.add(button1);
    panel.add(comboBox1);
    panel.add(radioButton1);
    panel.add(textField1);
    panel.add(browseButton);
    panel.add(radioButton2);
    panel.add(radioButton3);
    panel.add(button2);

}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.getContentPane().add(panel);
            frame.setVisible(true);

        }
    });

}
}

当我运行该类时,框架是唯一出现的组件:

【问题讨论】:

    标签: java user-interface intellij-idea frame


    【解决方案1】:

    尝试最大化窗口或在代码中设置框架大小——frame.setSize()

    【讨论】:

    • 如果你使用frame.pack(),它会根据里面组件的大小自动改变框架的大小。
    • 您好,感谢您的评论。我尝试添加它,但它仍然显示框架内没有任何内容。
    【解决方案2】:

    所以我设法让它运行并显示所有内容。我没有扩展 JPanel,而是使用了 JFrame 并调用了要设置为可见的类。代码如下:

    public class SftpDoc extends JFrame {
    
    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    private JRadioButton radioButton3;
    private JButton button1;
    private JComboBox comboBox1;
    private JTextField textField1;
    private JButton browseButton;
    private JButton button2;
    private final static JPanel panel = new JPanel();
    
    public SftpDoc() {
    
        panel.add(button1);
        panel.add(comboBox1);
        panel.add(radioButton1);
        panel.add(textField1);
        panel.add(browseButton);
        panel.add(radioButton2);
        panel.add(radioButton3);
        panel.add(button2);
        panel.revalidate();
        add(panel);
        pack();
    }
    
    public static void main(String[] args) {
    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new SftpDoc().setVisible(true);
            }
        });
    
    }
    }
    

    【讨论】:

      【解决方案3】:
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
      

      您好,您必须添加 frame.pack();在 setVisible() 方法之前。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多