如何使用GUI intelliJ

时间:2016-07-25 07:53:00

标签: java user-interface intellij-idea frame

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

以下是我的GUI组件的图片:

enter image description here

以下是我在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);

        }
    });

}
}

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

所以我设法让它运行并展示一切。我没有扩展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);
        }
    });

}
}

答案 2 :(得分:0)

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

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