1

嗨,我正在做一个摇摆项目。我想在数据库中的 JTable 中显示我的数据。我
创建了 JSplitpane。在面板 1 中为来自 Databse 的 sect 数据创建了搜索按钮、jLabel 和 JTextBox ...在面板 2 中,我想在 JTable.my 数据库查询中显示搜索结果,并且一切正常......但我无法在面板 2 中显示 JTable .. .任何人都可以帮我解决这个问题。

public JFrame TimesheetReport(){ //创建第一个 JFrame

    final JFrame employeeFrame = new JFrame("Report");



    employeeFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        employeeFrame.setResizable(true)
    employeeFrame.setLocation(100,50);
    employeeFrame.setVisible(true);
            employeeFrame.setResizable(false);
    panel.setLayout(new FlowLayout());
    panel1.setLayout(new BorderLayout());
     employeeFrame.pack();
            final JLabel label1;
    final JLabel label2;
    final JTextField  text1;
    final JTextField text2;
    final JButton ADD;


    {

    label1 = new JLabel("From Date");
    text1 = new JTextField(12);

    label2 = new JLabel("End Date");
    text2 = new JTextField(12);
    JButton button2 = new JButton("calendar");

        ADD=new JButton("Search");
    ADD.setText("Search");
    ADD.addActionListener(new java.awt.event.ActionListener() {  

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) { 

                DbUtility TimesheetReport=new DbUtility();
                TimesheetReport. loadDriver();
                TimesheetReport.connect();

                String value=(text1.getText());
                String value2=(text2.getText());
                ResultSet rs=     TimesheetReport.executeSelectQuery14(value,value2);
                String    Activityid =null, EmployeeName = null,StartDate = null,EndDate=null,Activity=null,Project=null,Day1=null,Day2=null,Day3=null,Day4=null,Day5=null ;

            try {
            while (rs.next()) {
                Activityid  = rs.getString("Activityid");
                EmployeeName = rs.getString("EmployeeName");
                StartDate = rs.getString("StartDate");
                EndDate = rs.getString("EndDate");
                Activity = rs.getString("Activity");
                Project= rs.getString("Project");
                Day1 = rs.getString("Day1");
                Day2 = rs.getString("Day2");
                Day3 = rs.getString("Day3");
                Day4 = rs.getString("Day4");
                Day5 = rs.getString("Day5");

                String[] columnNames = {"Activityid","EmployeeName", "StatrDate","EndDate", "Activity","Project","Day1","Day2","Day3","Day3","Day4","Day5"};
                String data[][] = {{Activityid,EmployeeName, StartDate,EndDate,Activity,Project,Day1,Day2,Day3,Day4,Day5}};
                System.out.println(Activityid+" "+EmployeeName+" "+StartDate+"" +EndDate+""+Activity+" "+Project+""+Day1+""+Day2+""+Day3+""+Day4+""+Day5);
                final DefaultTableModel model = new DefaultTableModel(data, columnNames);
                table2= new JTable(model);
                table2.setRowHeight( 70 );
                table2.setFillsViewportHeight(true);
                JTextField field = new JTextField();
                field.setForeground(Color.RED);
                field.setBackground(Color.RED);
                table2.getTableHeader().setPreferredSize(new Dimension(50,50));
                table2.getTableHeader().setBackground(Color.blue);
                table2.getTableHeader().setForeground(Color.white);
                table2.getTableHeader().setFont(new Font("Serif", Font.BOLD, 15)); 
                table2.getTableHeader().setOpaque(true);
                table2.getModel();
                table2.setForeground(Color.blue);
                table2.setBackground(Color.white);
                table2.setGridColor(Color.MAGENTA);
                table2.setShowGrid(true);
                    JScrollPane pane = new JScrollPane(table2);
                    panel1.add(pane);




        }
            } catch (SQLException e1) {

                e1.printStackTrace();
                                }  

                 }
                     });


        JSplitPane splitPane = new      JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel,panel1 );
        splitPane.setDividerLocation(300);
        Dimension minimumSize = new Dimension(250, 0);
        panel.setMinimumSize(minimumSize);
        employeeFrame.add(splitPane);
         pane.add(panel1);
         panel.add(label1);

         label1.setForeground(Color.white);
         panel.add(text1);

         panel.add(label2);

         label2.setForeground(Color.white);
         panel.add( text2);

         panel.add(ADD);

          }



      return employeeFrame;

       }
4

2 回答 2

3

但我无法在 panel2 中显示 JTable ...任何人都可以帮我解决这个问题。

您的代码不是SSCCE格式...它不会在简单的复制和粘贴上编译,并且存在一个错误,因为table2未在给定代码的方法范围内声明,也没有任何引用,panel2但我认为您的意思是panel1。 ..

解决方案:

1)不要使用null/ Absolute LayoutManager

阅读:

2)为什么需要添加JTablepanel1或问题中引用的panel2)?如果有的话,我们会将其添加JScrollPaneJTablepanel1只需添加table2JScrollPane并添加JScrollPaneJSplitPane即:

JScrollPane pane = new JScrollPane(table2);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, pane);

或者添加JTableJScrollPane和添加JScrollPanepanel1BorderLayout 所以JPanel将拉伸到容器大小:

JPanel panel1=new JPanel(new BorderLayout());
....
JScrollPane pane = new JScrollPane(table2);
panel1.add(pane);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, panel1);

3)也因为我看到你最大化JFrame通过:

JFrame#setExtendedState(JFrame.MAXIMIZED_BOTH);

明智的做法是LayoutManager在 JFrame 大小发生更改时使用自动调整组件大小。即BorderLayoutGridLayout并且GridBagLayout是一些浮现在脑海中的。

4)在添加所有组件之前也不要设置JFrame可见。

5)通过块在Event Dispatch Thread上创建 Swing 组件。SwingUtilities.invokeLater(Runnable r)

6)不要忘记pack()JFrame设置可见之前但在添加组件之后调用。

7)我也觉得这个评论很可疑://Creating 1st JFrame阅读The Use of Multiple JFrames, Good/Bad Practice? . 而是使用CardLayoutor JDialog

8) 我看不到需要setLocation完全扩展的JFrame.

这是您的修复代码(我只使用了所有组件 default LayoutManagers(除了panel1使用BorderLayoutsoJPanel将拉伸到容器大小):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Test {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }

    public Test() {
        createAndShowGui();
    }

    public void createAndShowGui() {

        final JFrame employeeFrame = new JFrame("Report");
        employeeFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JPanel panel = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(250, 0);
            }
        };
        final JTable table2 = new JTable(new String[][]{
                    {"one", "two", "three"},
                    {"one", "two", "three"},
                    {"one", "two", "three"}
                }, new String[]{"col1", "col2", "col3"});

        JPanel panel1 = new JPanel(new BorderLayout());//panel which holds jtable
        final JLabel label1;
        final JLabel label2;
        final JTextField text1;
        final JTextField text2;
        final JButton ADD;

        label1 = new JLabel("From Date");
        text1 = new JTextField(12);
        label2 = new JLabel("End Date");
        text2 = new JTextField(12);
        JButton button2 = new JButton("calendar");
        ADD = new JButton("Search");
        ADD.setText("Search");
        ADD.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                /*
                 DbUtility createAndShowGui = new DbUtility();
                 createAndShowGui.loadDriver();
                 createAndShowGui.connect();

                 String value = (text1.getText());
                 String value2 = (text2.getText());
                 ResultSet rs = createAndShowGui.executeSelectQuery14(value, value2);
                 String Activityid = null, EmployeeName = null, StartDate = null, EndDate = null, Activity = null, Project = null, Day1 = null, Day2 = null, Day3 = null, Day4 = null, Day5 = null;

                 try {
                 while (rs.next()) {
                 Activityid = rs.getString("Activityid");
                 EmployeeName = rs.getString("EmployeeName");
                 StartDate = rs.getString("StartDate");
                 EndDate = rs.getString("EndDate");
                 Activity = rs.getString("Activity");
                 Project = rs.getString("Project");
                 Day1 = rs.getString("Day1");
                 Day2 = rs.getString("Day2");
                 Day3 = rs.getString("Day3");
                 Day4 = rs.getString("Day4");
                 Day5 = rs.getString("Day5");

                 String[] columnNames = {"Activityid", "EmployeeName", "StatrDate", "EndDate", "Activity", "Project", "Day1", "Day2", "Day3", "Day3", "Day4", "Day5"};
                 String data[][] = {{Activityid, EmployeeName, StartDate, EndDate, Activity, Project, Day1, Day2, Day3, Day4, Day5}};
                 System.out.println(Activityid + " " + EmployeeName + " " + StartDate + "" + EndDate + "" + Activity + " " + Project + "" + Day1 + "" + Day2 + "" + Day3 + "" + Day4 + "" + Day5);
                 final DefaultTableModel model = new DefaultTableModel(data, columnNames);
                 table2 = new JTable(model);
                 table2.setRowHeight(70);
                 table2.setFillsViewportHeight(true);
                 JTextField field = new JTextField();
                 field.setForeground(Color.RED);
                 field.setBackground(Color.RED);
                 table2.getTableHeader().setPreferredSize(new Dimension(50, 50));
                 table2.getTableHeader().setBackground(Color.blue);
                 table2.getTableHeader().setForeground(Color.white);
                 table2.getTableHeader().setFont(new Font("Serif", Font.BOLD, 15));
                 table2.getTableHeader().setOpaque(true);
                 table2.getModel();
                 table2.setForeground(Color.blue);
                 table2.setBackground(Color.white);
                 table2.setGridColor(Color.MAGENTA);
                 table2.setShowGrid(true);

                 }
                 } catch (SQLException e1) {
                 e1.printStackTrace();
                 }
                 */
            }
        });

        panel.add(label1);
        label1.setForeground(Color.white);
        panel.add(text1);
        panel.add(label2);
        label2.setForeground(Color.white);
        panel.add(text2);
        panel.add(ADD);

        JScrollPane pane = new JScrollPane(table2);
        panel1.add(pane);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, panel1);
        splitPane.setDividerLocation(300);

        employeeFrame.add(splitPane);

        employeeFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
        //employeeFrame.setLocation(100, 50);
        employeeFrame.pack();

        employeeFrame.setVisible(true);
        employeeFrame.setResizable(false);//or else it wont work nicely withn JFrame#setExtendedState
    }
}
于 2013-02-15T12:41:13.940 回答
0

通过查看您的代码,我找不到您将表格放置在您实际添加到 SplitPane 中的面板或窗格中的位置。

于 2013-02-15T12:43:43.167 回答