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 :)
Ich sehe 'f' nicht in Ihrem makeUI. Sie sollten in der createAndShowGui() -Methode wahrscheinlich ein Feld anstelle einer lokalen Variablen machen. – matt
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
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 –