我对java的所有东西都很新。我只是想创建一个包含多个表单的简单应用程序。我创建了一个摇摆形式,它将成为所有其他形式的基础。然后,我试图实例化通用的swing表单,并添加特定于我正在创建的任何形式的组件。
我在将其中一个表单上的JTable添加到布局时遇到了特别的麻烦。
这是我的最小代码示例:
模板表格:
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class FormLayout extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public FormLayout() throws SQLException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 850, 800);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup())
);
contentPane.setLayout(gl_contentPane);
//gl_contentPane.add(new JScrollPane(table), BorderLayout.CENTER);
}
}
通话类:
import java.awt.EventQueue;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JTable;
public class Main extends FormLayout {
private static final long serialVersionUID = 1L;
public Main() throws SQLException{
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FormLayout frame = new FormLayout();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void MainForm() throws SQLException{
String str = "select * from tblEndoscopy";
ResultSet rs = Bquery.resultQuery(str);
JTable table = new JTable(Bquery.buildTableModel(rs));
////HOW DO I ADD THIS JTABLE TO THE TEMPLATE FORM i.e. TO THE GROUPED PANEL ???
}
}