![]() |
| | Themen-Optionen | Thema durchsuchen |
| | Nach oben #1 |
| Neuer Benutzer Registriert seit: 23.08.2005
Beiträge: 9
|
Habe schon eine weile nichts mehr Programmiert. Kaum fange ich wieder an beginnt die Verzweiflung. Ich habe ein Programm geschrieben, dass in einer Tabelle Werte anzeigen soll, sobald ich auf das Menü Neu starten klicke soll statt gar nichts nur 1 angezeigt werden. Ich habe die Tabelle in einem JPanel realisiert, und glaube auch, dass es genau daran liegt, warum mein Listener nicht greift. Hier ist mein derzeitiger Code. Wäre klasse, wenn mir da jemand bei diesem Problem helfen könnte. Code: import javax.swing.*;
import java.awt.*;
public class Hauptprogramm extends JFrame {
private static final long serialVersionUID = 1L;
private Container fensterinhalt;
private Spielbereich spielbereich;
public Menueleiste menueleiste;
Hauptprogramm(){
spielbereich = new Spielbereich(false);
fensterinhalt = this.getContentPane();
fensterinhalt.add(spielbereich, BorderLayout.CENTER);
menueleiste = new Menueleiste(this);
this.setResizable(false);
this.setTitle("Sudoku Version 1.0");
this.pack();
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setVisible( true );
}
public static void main(String[] args) {
new Hauptprogramm();
}
}
mport java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.*;
public class Spielbereich extends JPanel {
private static final long serialVersionUID = 1L;
public String[][] tabelleninhalt = new String[9][9];
public String[] ueberschrift = new String [9];
public JTable tabelle;
public Tabelleneigenschaften model;
Spielbereich(boolean status){
if (status){
tabelle = new JTable(Tabelleninhalt_ermitteln(), Ueberschrift_ermitteln());
System.out.print("mplll");
} else {
tabelle = new JTable(Tabelleninhalt_ermitteln2(), Ueberschrift_ermitteln());
}
tabelle.setShowGrid(true);
tabelle.setRowHeight(60);
tabelle.getTableHeader().setReorderingAllowed( false );
tabelle.getTableHeader().setResizingAllowed( false );
tabelle.setColumnSelectionAllowed( true );
tabelle.setRowSelectionAllowed( true );
tabelle.setFocusable(true);
tabelle.setRequestFocusEnabled(false);
tabelle.setDefaultRenderer( Object.class, new Tabelleneigenschaften(tabelle, tabelleninhalt));
this.add(tabelle);
}
public String[][]Tabelleninhalt_ermitteln(){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
tabelleninhalt[i][j] = "0";
}
}
return tabelleninhalt;
}
public String[][]Tabelleninhalt_ermitteln2(){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
tabelleninhalt[i][j] = "1";
}
}
return tabelleninhalt;
}
public String[] Ueberschrift_ermitteln(){
for (int i = 0; i < 9; i++){
ueberschrift[i] = "";
}
return ueberschrift;
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class Tabelleneigenschaften extends AbstractTableModel implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public Font schrift = new Font("Serif", Font.ITALIC, 30);
public JTextField text;
public Spielbereich spielbereich;
public int[][] sudokufeld = new int[9][9];
Tabelleneigenschaften(JTable anzeigetabelle, String[][] feld){
Sudokufeld_ermitteln(feld);
anzeigetabelle.setModel(this);
}
public void Sudokufeld_ermitteln(String[][] wertuebergabe){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
sudokufeld[i][j] = Integer.parseInt(wertuebergabe[i][j]);
}
}
}
public int getRowCount() {
// TODO Auto-generated method stub
return 9;
}
public int getColumnCount() {
// TODO Auto-generated method stub
return 9;
}
public Object getValueAt(int arg0, int arg1) {
String rueckgabewert;
//rueckgabewert = new String(String.valueOf(zuweisen.sudokufeld[arg0][arg1]));
rueckgabewert = new String(String.valueOf(sudokufeld[arg0][arg1]));
return rueckgabewert;
}
public boolean isCellEditable(int arg0, int arg1){
boolean rueckgabewert;
if ((this.sudokufeld[arg0][arg1]) == 0){
rueckgabewert = true;
} else {
rueckgabewert = false;
}
return rueckgabewert;
}
public void setValueAt(Object value, int row, int column){
this.sudokufeld[row][column] = Integer.parseInt((String)value);
}
public Component getTableCellRendererComponent( JTable tabelle, Object wert,
boolean isSelected, boolean hasFocus, int spalte, int reihe) {
//Textfeld erzeugen
text = new JTextField((String)wert);
text.setOpaque(true);
text.setFont(tabelle.getFont());
text.setFont(schrift);
text.setForeground(tabelle.getForeground());
text.setBackground(tabelle.getBackground());
text.setHorizontalAlignment(JTextField.CENTER);
if (hasFocus) {
text.setBackground(new Color (225,225,225));
}
else if(isSelected)
text.setBackground(new Color (225,225,225));
else if(reihe == 0 && spalte == 0)
text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else {}
setValueAt(wert, tabelle, spalte, reihe);
return text;
}
public void setValueAt(Object wert, JTable tabelle, int spalte, int reihe){
spalte = tabelle.convertColumnIndexToModel(spalte);
reihe = tabelle.convertColumnIndexToModel(reihe);
if (reihe < 9 && spalte < 9) {
int numpages = Integer.parseInt((String)wert);
if (numpages == 0) {
text.setText("");
} else {
}
}
}
public boolean editCellAt(int row, int column){
text.setText("hallo");
return true;
}
}
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Menueleiste {
public JMenuBar menue = new JMenuBar();
public JMenu spiel = new JMenu("Spiel");
public JMenuItem starten = new JMenuItem("Neu Starten");
public Menueverarbeitung ereignis = new Menueverarbeitung();
public Menueleiste(JFrame leiste){
menue.add(spiel);
spiel.add(starten);
starten.addActionListener(ereignis);
leiste.setJMenuBar(menue);
}
}
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class Menueverarbeitung implements ActionListener{
public JTable tabelle;
public Tabelleneigenschaften eigenschaften;
public String[][] spielfeld_neu = new String[9][9];
public String[] ueberschrift = new String[9];
String befehl;
public void actionPerformed(ActionEvent evt) {
befehl = evt.getActionCommand();
if (befehl.equals("Neu Starten")){
//eigenschaften.Sudokufeld_ermitteln(Neues_Spiel_Feld());
new Spielbereich(true);
System.out.print("hallo");
}
}
public String[][] Neues_Spielfeld(){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
spielfeld_neu[i][j] = "1";
//System.out.print(wertuebergabe[i][j]);
}
}
return spielfeld_neu;
}
}
|
| | |
| | Nach oben #2 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.133
|
Du solltest lieber das vorhandene TableModel ändern, anstatt ne neue Tabelle auf die Halde zu werfen... Dazu sollte jenes TableModel entweder ein Singleton sein (googlen), oder du übergibst es den anderen Klassen (speziell der "Menueverarbeitung") als Parameter - entweder im Konstruktor, oder via Setter. Ist dir nun dein Problem klar geworden? Edit: Diese Zeile hier Code: //eigenschaften.Sudokufeld_ermitteln(Neues_Spiel_Feld()); Geändert von pago (08.08.2006 um 15:54 Uhr) |
| | |
| | Nach oben #3 |
| Neuer Benutzer Registriert seit: 23.08.2005
Beiträge: 9
|
Danke für den Tipp Durch debuggen konnte ich feststellen, das die Variable "Sudokufeld" nun die Werte hat, die sie braucht. Nur leider werden sie in der Tabelle nicht angezeigt. Gibt es da eine Funktion in JTable die die Zellen aktualisiert. Habe es mit fireTableCellUpdated( row, column ); probiert und mit editCellAt() und auch mir dem Aufruf der getValueAt Methode versucht, keine Erfolge. Hier der abgeänderte Code in der Klasse Menueverarbeitung Code: if (befehl.equals("Neu Starten")){
eigenschaften = new Tabelleneigenschaften(tabelleaendern,Neues_Spielfeld(), false);
eigenschaften.Sudokufeld_ermitteln(Neues_Spielfeld());
System.out.print("hallo");
}
Code: Tabelleneigenschaften(JTable anzeigetabelle, String[][] feld, boolean status){
menueverarbeitung = new Menueverarbeitung(anzeigetabelle);
Sudokufeld_ermitteln(feld);
if (status)
anzeigetabelle.setModel(this);
}
|
| | |
| | Nach oben #4 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.133
| java Code:
Müsstest du vielleicht so ändern: java Code:
|
| | |
| | Nach oben #5 |
| Neuer Benutzer Registriert seit: 23.08.2005
Beiträge: 9
|
Tja irgendwie hat dies nicht funktioniert, ich weiß nicht, wo ich sonst noch ansetzen soll. Ich habe so das Gefühl das die Menuverarbeitung irgendetwas nicht bekommt, was sie dringen benötigt. Bin schon seit Stunden an diesem Problem und bin kurz vor dem aufgeben. |
| | |
| | Nach oben #6 |
| Benjamin Klaile Registriert seit: 02.12.2004 Ort: Remagen
Beiträge: 4.516
|
Dieses Thema wurde verschoben aufgrund von .. Postings im falschen Forum. Bemerkung: Es fällt mir jetzt erst auf, aber es geht hier ja eindeutig um was Grafisches, demnach verschiebe ich das mal ins entsprechende Forum. |
| | |
| | Nach oben #8 |
| Oliver O. Registriert seit: 17.08.2005
Beiträge: 427
|
Zwar hat das nicht direkt mir dem thema zu tun, aber muss das hier sein: Code: else if(reihe == 0 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 0 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 1 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 2 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 3 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 4 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 3) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 4) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 5 && spalte == 5) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 0) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 1) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 2) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 6 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 7 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 6) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 7) text.setBackground(Color.LIGHT_GRAY);
else if(reihe == 8 && spalte == 8) text.setBackground(Color.LIGHT_GRAY);
else {}
zb: Code: Color[][] c = new Color[9][9];
public void Sudokufeld_ermitteln(String[][] wertuebergabe){
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
sudokufeld[i][j] = Integer.parseInt(wertuebergabe[i][j]);
c[i][j] = Color.LIGHT_GRAY;
}
}
public Component getTableCellRendererComponent([...]){
[...]
text.setBackground(c[reihe][spalte]);
|
| | |
![]() |
| Lesezeichen |
| Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
| Themen-Optionen | Thema durchsuchen |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Microsoft reagiert auf die neuerliche Verzögerung von Vista | sparrow | Nachrichten | 0 | 27.03.2006 22:24 |
| [PHP] Aktuell gespielten Titel von XMMS auf einer Webseite in einer Grafik anzeigen | Corvin | Tutorials | 0 | 28.11.2005 16:26 |
| Login auf mehreren Domains | Homepagespeicher | PHP-Programmierung | 17 | 21.08.2005 16:54 |
| GESUCHT:simples beispiel zum thema resultset, tablemodel => in einer JTable ausgeben! | bamboocha | Desktop-Applikationen und Grafik | 2 | 06.06.2005 21:34 |
| JTable setValueAt festlegen auf einzelne Rows | McSnoop | Allgemeine Java-Programmierung | 3 | 04.02.2005 14:50 |