2017-07-06 1 views
0

Ich versuche, eine bestimmte GUI zu erreichen, ABER es ist mir nicht gelungen, ein JPanel in meine Jframe, die in einer anderen Funktion initialisiert wird, eine Idee, wie dies zu tun?Greifen Sie jframe aus einer anderen Funktion und fügen Sie jpanel hinein - Java Swing

hier ist mein Code:

public class CDRTable { 
    int totalRecords; 
    private final String[] columnNames = { "Year", "String", "Comment" }; 
    private final DefaultTableModel model = new DefaultTableModel(null, columnNames) { 
     @Override 
     public Class<?> getColumnClass(int column) { 
      return (column == 0) ? Integer.class : Object.class; 
     } 
    }; 
    private final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); 
    private final JTable table = new JTable(model); 
    private final JButton first = new JButton(new AbstractAction("|<") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex = 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton prev = new JButton(new AbstractAction("<") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex -= 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton next = new JButton(new AbstractAction(">") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex += 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton last = new JButton(new AbstractAction(">|") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex = maxPageIndex; 
      initFilterAndButton(); 
     } 
    }); 
    private final JTextField field = new JTextField(2); 
    private final JLabel label = new JLabel(); 

    public JComponent makeUI() throws ClassNotFoundException, SQLException { 

     table.setFillsViewportHeight(true); 
     table.setRowSorter(sorter); 

     Class.forName("org.h2.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa"); 

     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery("SELECT * FROM cdr LIMIT 1000"); 

     totalRecords = 1000; 
     for (int i = 0; i < totalRecords; i++) { 
      model.addRow(new Object[] { 0,0,0 }); 
     } 
     table.setModel(DbUtils.resultSetToTableModel(rs)); 

     JPanel po = new JPanel(); 
     po.add(field); 
     po.add(label); 
     JPanel box = new JPanel(new GridLayout(1, 4, 2, 2)); 
     for (JComponent r : Arrays.asList(first, prev, po, next, last)) { 
      box.add(r); 
     } 

     int rowCount = model.getRowCount(); 
     int v = rowCount % itemsPerPage == 0 ? 0 : 1; 
     maxPageIndex = rowCount/itemsPerPage + v; 
     initFilterAndButton(); 
     label.setText(String.format("/ %d", maxPageIndex)); 
     KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 
     field.getInputMap(JComponent.WHEN_FOCUSED).put(enter, "Enter"); 
     field.getActionMap().put("Enter", new AbstractAction() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        int v = Integer.parseInt(field.getText()); 
        if (v > 0 && v <= maxPageIndex) { 
         currentPageIndex = v; 
        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       initFilterAndButton(); 
      } 
     }); 

     JPanel p = new JPanel(new BorderLayout()); 
     p.add(box, BorderLayout.NORTH); 
     p.add(new JScrollPane(table)); 
     return p; 
    } 

    private final int itemsPerPage = 100; 
    private int maxPageIndex; 
    private int currentPageIndex = 1; 

    private void initFilterAndButton() { 
     sorter.setRowFilter(new RowFilter<TableModel, Integer>() { 
      @Override 
      public boolean include(Entry<? extends TableModel, ? extends Integer> entry) { 
       int ti = currentPageIndex - 1; 
       int ei = entry.getIdentifier(); 
       return ti * itemsPerPage <= ei && ei < ti * itemsPerPage + itemsPerPage; 
      } 
     }); 
     first.setEnabled(currentPageIndex > 1); 
     prev.setEnabled(currentPageIndex > 1); 
     next.setEnabled(currentPageIndex < maxPageIndex); 
     last.setEnabled(currentPageIndex < maxPageIndex); 
     field.setText(Integer.toString(currentPageIndex)); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        CDRTable obj = new CDRTable(); 
        obj.createAndShowGUI(); 
       } catch (ClassNotFoundException e) { 
        e.printStackTrace(); 
       } catch (SQLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public void createAndShowGUI() throws ClassNotFoundException, SQLException { 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new CDRTable().makeUI()); 
     f.setBounds(30, 50, 1300, 600); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 


} 

Wenn ich versuche, die JFrame in createAndShowGUI() von public JComponent makeUI() initialisiert den Zugriff auf einen Fehler zeigt

f nicht

aufgelöst werden kann ich bin sehr neu zu JAVA, also bitte ignorieren, wenn Sie sich durch meine Frage irritiert fühlen :)

+1

Ich sehe 'f' nicht in Ihrem makeUI. Sie sollten in der createAndShowGui() -Methode wahrscheinlich ein Feld anstelle einer lokalen Variablen machen. – matt

+1

Sie definieren JFrame f nur in Ihrer statischen createAndShowGUI-Methode. Es ist nicht zugänglich außerhalb! Lesen Sie etwas über die Grundlagen der Sichtbarkeit und Objektorientierung in Java – Luftbaum

+0

Ich habe versucht zu erklären und zu definieren 'JFrame f = neue JFrame (" CDR TABLE "); 'in meiner Hauptklasse, aber dann zwang es mich, es statisch zu machen, die den gesamten Code vollständig ändern. Weiß nicht, was ich tun soll –

Antwort

0

Irgendwie habe ich erreicht, was ich wollte, also dachte ich, ich sollte mir die Antwort hier geben.

public class CDRTable { 
    //JFrame f; 
    int totalRecords; 
    private final String[] columnNames = { "ANUMBER", "BNUMBER", "DATETIME" }; 
    private final DefaultTableModel model = new DefaultTableModel(null, columnNames) { 
     @Override 
     public Class<?> getColumnClass(int column) { 
      return (column == 0) ? Integer.class : Object.class; 
     } 
    }; 
    private final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); 
    private final JTable table = new JTable(model); 
    private final JButton first = new JButton(new AbstractAction("|<") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex = 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton prev = new JButton(new AbstractAction("<") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex -= 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton next = new JButton(new AbstractAction(">") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex += 1; 
      initFilterAndButton(); 
     } 
    }); 
    private final JButton last = new JButton(new AbstractAction(">|") { 
     public void actionPerformed(ActionEvent e) { 
      currentPageIndex = maxPageIndex; 
      initFilterAndButton(); 
     } 
    }); 
    private final JTextField field = new JTextField(2); 
    private final JLabel label = new JLabel(); 

    public JComponent makeUI(Container f) throws ClassNotFoundException, SQLException { 

     table.setFillsViewportHeight(true); 
     table.setRowSorter(sorter); 

     Class.forName("org.h2.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa"); 

     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery("SELECT * FROM cdr LIMIT 1000"); 

     totalRecords = 1000; 
     for (int i = 0; i < totalRecords; i++) { 
      model.addRow(new Object[] { 0,0,0 }); 
     } 
     table.setModel(DbUtils.resultSetToTableModel(rs)); 

     JPanel po = new JPanel(); 
     po.add(field); 
     po.add(label); 
     JPanel box = new JPanel(new GridLayout(1, 4, 2, 2)); 
     for (JComponent r : Arrays.asList(first, prev, po, next, last)) { 
      box.add(r); 
     } 
     int rowCount = model.getRowCount(); 
     int v = rowCount % itemsPerPage == 0 ? 0 : 1; 
     maxPageIndex = rowCount/itemsPerPage + v; 
     initFilterAndButton(); 
     label.setText(String.format("/ %d", maxPageIndex)); 
     KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); 
     field.getInputMap(JComponent.WHEN_FOCUSED).put(enter, "Enter"); 
     field.getActionMap().put("Enter", new AbstractAction() { 
      public void actionPerformed(ActionEvent e) { 
       try { 
        int v = Integer.parseInt(field.getText()); 
        if (v > 0 && v <= maxPageIndex) { 
         currentPageIndex = v; 
        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       initFilterAndButton(); 
      } 
     }); 

     UIManager.put("TabbedPane.selected", Color.lightGray); 
     JTabbedPane tabbedPane = new JTabbedPane(); 
     f.add(tabbedPane); 

     JPanel panel = new JPanel(); 
     tabbedPane.addTab("TABLE", null, panel, null); 
     panel.setBackground(Color.white); 

     JPanel panel_1 = new JPanel(); 
     tabbedPane.addTab("GRAPH", null, panel_1, null); 
     panel_1.setBackground(Color.white); 

     JScrollPane scrollpane = new JScrollPane(); 
     panel.add(scrollpane, BorderLayout.SOUTH); 
     scrollpane.setViewportView(table); 

     table.getTableHeader().setBackground(new Color(26,82,118)); 
     table.getTableHeader().setForeground(Color.white); 
     table.getTableHeader().setPreferredSize(new Dimension(100, 40)); 
     table.setRowHeight(30); 

     first.setBackground(new Color(84, 153, 199)); 
     prev.setBackground(new Color(84, 153, 199)); 
     next.setBackground(new Color(84, 153, 199)); 
     last.setBackground(new Color(84, 153, 199)); 

     first.setForeground(Color.white); 
     prev.setForeground(Color.white); 
     next.setForeground(Color.white); 
     last.setForeground(Color.white); 

     panel.add(box); 
     panel.add(new JScrollPane(table)); 
     return scrollpane; 

    } 

    private final int itemsPerPage = 100; 
    private int maxPageIndex; 
    private int currentPageIndex = 1; 

    private void initFilterAndButton() { 
     sorter.setRowFilter(new RowFilter<TableModel, Integer>() { 
      @Override 
      public boolean include(Entry<? extends TableModel, ? extends Integer> entry) { 
       int ti = currentPageIndex - 1; 
       int ei = entry.getIdentifier(); 
       return ti * itemsPerPage <= ei && ei < ti * itemsPerPage + itemsPerPage; 
      } 
     }); 
     first.setEnabled(currentPageIndex > 1); 
     prev.setEnabled(currentPageIndex > 1); 
     next.setEnabled(currentPageIndex < maxPageIndex); 
     last.setEnabled(currentPageIndex < maxPageIndex); 
     field.setText(Integer.toString(currentPageIndex)); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        CDRTable obj = new CDRTable(); 
        obj.createAndShowGUI(); 
       } catch (ClassNotFoundException e) { 
        e.printStackTrace(); 
       } catch (SQLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public void createAndShowGUI() throws ClassNotFoundException, SQLException { 
     JFrame f = new JFrame("CDR TABLE"); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new CDRTable().makeUI(f)); 
     f.setBounds(30, 50, 1300, 600); 
     f.setLayout(new GridLayout(0, 2)); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 


} 
+0

einfachste Weg ist, Ihre jframe als öffentlich zugängliches Feld zu deklarieren, nachdem Sie den Namen Ihrer Klasse deklarieren, und instanziieren Sie es in Ihrer Hauptmethode, auf diese Weise kann Ihre JFrame-Variable in allen Ihren Methoden zugänglich sein –

Verwandte Themen