我无法让我的GUI显示在我的程序中。我在IntelliJ IDE中使用JFormDesigner并且无法使其工作。它运行成功,但GUI表格根本没有出现。这是我的代码,感觉我错过了一些简单的东西:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
* Created by JFormDesigner on Thu Apr 12 15:41:25 SAST 2018
*/
/**
* @author Jordan
*/
public class hashTable extends JPanel {
public hashTable() {
initComponents();
}
public static void main(String [] args) {
hashTable ht = new hashTable();
ht.setVisible(true);
}
private void button1ActionPerformed(ActionEvent e) {
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - Jordan Taschner
label2 = new JLabel();
label3 = new JLabel();
textField1 = new JTextField();
button1 = new JButton();
textField2 = new JTextField();
//======== this ========
// JFormDesigner evaluation mark
setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), getBorder())); addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
setLayout(new MigLayout(
"hidemode 3",
// columns
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]" +
"[fill]",
// rows
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]" +
"[]"));
//---- label2 ----
label2.setText("VOTER APPLICATION");
label2.setFont(label2.getFont().deriveFont(Font.BOLD, label2.getFont().getSize() + 3f));
add(label2, "cell 3 0 7 1");
//---- label3 ----
label3.setText("CSC2 Assignment 3");
label3.setForeground(new Color(229, 218, 218));
label3.setFont(new Font("Menlo", Font.ITALIC, 13));
add(label3, "cell 4 1");
//---- textField1 ----
textField1.setText("(enter ID number)");
textField1.setFont(textField1.getFont().deriveFont(textField1.getFont().getStyle() | Font.ITALIC));
add(textField1, "cell 4 4 2 1");
//---- button1 ----
button1.setText("Find Name");
button1.setFont(new Font("Menlo", Font.PLAIN, 13));
button1.addActionListener(e -> button1ActionPerformed(e));
add(button1, "cell 4 6");
add(textField2, "cell 4 7 2 1");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - Jordan Taschner
private JLabel label2;
private JLabel label3;
private JTextField textField1;
private JButton button1;
private JTextField textField2;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
任何帮助将不胜感激。我认为将其设置为可见或打包存在问题。我是java的新手,所以我不太确定。
答案 0 :(得分:0)
你不能只显示JPanel
;您需要创建JFrame
并使用setContentPane()
来显示JFrame中的面板:
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new hashTable());
frame.pack();
frame.setVisible(true);