|
Antwort |
(CodeLib-Manager)
Registriert seit: 5. Aug 2004 Ort: München 1.062 Beiträge |
#1
Hi Leute
Ich hab ein klitzekleines Problem, das mir meinen Verstand und meine Nerven kostet: ich habe folgenden Java-Code...
Code:
...und folgendes Problem:
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; class cResources implements KeyListener { public LinkedList Resource; public JPanel MainPanel; private LinkedList Panels; private JLabel Caption; public int fCount; private void Init(String fCaption, int Count) { fCount = 0; MainPanel = new JPanel(); Caption = new JLabel(fCaption); Panels = new LinkedList(); Resource = new LinkedList(); Panels.add(new JPanel()); ((JPanel)Panels.get(0)).setLayout(new BorderLayout()); ((JPanel)Panels.get(0)).add(Caption, BorderLayout.CENTER); setCount(Count); } public cResources(String fCaption, int Count) { Init(fCaption, Count); } public cResources() { fCount = 0; Init("", 3); } public int getData(int Index) { return Integer.parseInt(((JTextField)Resource.get(Index)).getText()); } public void setCount(int Count) { //Reset everything MainPanel.setLayout(new GridLayout(Count + 1, 1)); MainPanel.add(((JPanel)Panels.get(0))); //Add old ones // System.out.println("-----------------------------"); for (int i = 1; i <= ((fCount > Count)?(Count-1):fCount); i++) { MainPanel.add(((JPanel)Panels.get(i))); // System.out.println(i + " of " + Count); } for (int i = fCount; i < Count; i++) { Panels.add(new JPanel()); Resource.add(new JTextField()); ((JPanel)Panels.get(i + 1)).setLayout(new GridLayout(1, 1)); ((JPanel)Panels.get(i + 1)).add((JTextField)Resource.get(i)); MainPanel.add((JPanel)Panels.get(i + 1)); // System.out.println((i + 1) + " of " + Count); } for (int i = Count; i < fCount; i--) { Resource.remove(i); Panels.remove(i + 1); } fCount = Count; } public void keyPressed(KeyEvent event) { switch (event.getKeyChar()) { case '0': break; case '1': break; case '2': break; case '3': break; case '4': break; case '5': break; case '6': break; case '7': break; case '8': break; case '9': break; default: ; } } public void keyReleased(KeyEvent event) { } public void keyTyped(KeyEvent event) { } } class cProcess { public int fCount; public cResources Devices[]; //ß = Allocated; 1 = Max; 2 = Needed public cProcess() { Devices = new cResources[3]; for (int i = 0; i < 3; i++) switch (i) { case 0: Devices[i] = new cResources("Allocated", 0); break; case 1: Devices[i] = new cResources("Max", 0); break; case 2: Devices[i] = new cResources("Needed", 0); break; } } public void setCount(int Count) { for (int i = 0; i < 3; i++) Devices[i].setCount(Count); } } class cPC implements ActionListener/*MouseListener, KeyListener, ActionListener*/ { public Container ContentPane; public Container ResultPanel; public JList res; private JButton bOk; public Container MainPanel; private JPanel ProcessList; public JList Processes; public JPanel Buttons; public JButton Add; public JButton Delete; public JButton AddRec; public JButton DelRec; public JButton bShowProc; public JButton WorkAlgo; private JPanel Rest; private JPanel RestSized; private JPanel Resources; private JPanel DevList; private Container DevInfos; private JPanel Available; public cResources DevAvailable; public cResources DevCaptions; public LinkedList pList; private cProcess l; private int c; private int h; private int temp; private int fCount; public JFrame Parent; public void SetSize() { Parent.setSize(Parent.getWidth() + 1, Parent.getHeight() + 1); Parent.setSize(Parent.getWidth() - 1, Parent.getHeight() - 1); } public int ProcCount() { return Processes.getSelectedIndex(); } public cPC(JFrame Parent) { this.Parent = Parent; h = 0; c = 0; temp = 0; ContentPane = new Container(); { ResultPanel = new Container(); ResultPanel.setLayout(new BorderLayout()); res = new JList(); bOk = new JButton("Ok"); bOk.addActionListener(this); ResultPanel.add(res, BorderLayout.CENTER); ResultPanel.add(bOk, BorderLayout.SOUTH); //ResultPanel.setVisible(false); } { pList = new LinkedList(); MainPanel = new Container(); MainPanel.setLayout(new BorderLayout()); } { ProcessList = new JPanel(); Rest = new JPanel(); } { Processes = new JList(new DefaultListModel()); Processes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); } { Add = new JButton("Add Process"); Add.addActionListener(this); Delete = new JButton("Delete Process"); Delete.addActionListener(this); AddRec = new JButton("Add Resource"); AddRec.addActionListener(this); DelRec = new JButton("Delete Resource"); DelRec.addActionListener(this); bShowProc = new JButton("Show Process"); bShowProc.addActionListener(this); WorkAlgo = new JButton("Work Algo"); WorkAlgo.addActionListener(this); } { Buttons = new JPanel(); Buttons.setLayout(new GridLayout(6, 1)); Buttons.add(Add); Buttons.add(Delete); Buttons.add(AddRec); Buttons.add(DelRec); Buttons.add(bShowProc); Buttons.add(WorkAlgo); } { ProcessList.setLayout(new BorderLayout()); ProcessList.add(Buttons, BorderLayout.SOUTH); ProcessList.add(Processes, BorderLayout.CENTER); } { DevCaptions = new cResources("Captions", 0); DevAvailable = new cResources("Available", 0); } { MainPanel.add(ProcessList, BorderLayout.EAST); ProcessList.setSize(300, 500); MainPanel.add(Rest, BorderLayout.CENTER); Rest.setLayout(new BorderLayout()); } RestSized = new JPanel(); MainPanel.setVisible(true); ContentPane.setLayout(new BorderLayout()); ContentPane.add(MainPanel, BorderLayout.CENTER); ContentPane.add(ResultPanel, BorderLayout.EAST); ResultPanel.setVisible(false); MainPanel.setSize(ContentPane.getWidth(), ContentPane.getHeight()); Processes = new JList(new DefaultListModel()); Processes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); SetSize(); AddProcess(); SetDevCount(5); Rest.add(RestSized, BorderLayout.NORTH); } public void SetDevCount(int Count) { RestSized.setSize(RestSized.getWidth(), 45 + 18 * Count); //Height of 18 Pixels for each Edit, plus height for Title and border DevCaptions.setCount(Count); DevAvailable.setCount(Count); for (int i = 0; i < pList.size(); i++) ((cProcess)pList.get(i)).setCount(Count); fCount = Count; //Nachdem Java so freundlich ist, uns nichts anzuzeigen, müssen wir es erst dazu bringen. Zwar nicht elegent, aber na ja.... RestSized.setVisible(false); RestSized.setVisible(true); // ShowProc(ProcCount()); // RestSized.setSize(Rest.Height); } public void ShowProc(int Ind) { if ((Ind < 0) | (Ind >= pList.size())) return ; RestSized.setLayout(new GridLayout(1, 5)); RestSized.setBorder(BorderFactory.createTitledBorder((String)Processes.getSelectedValue())); if (h > 0) { RestSized.remove(DevCaptions.MainPanel); RestSized.remove(l.Devices[0].MainPanel); RestSized.remove(l.Devices[1].MainPanel); RestSized.remove(l.Devices[2].MainPanel); RestSized.remove(DevAvailable.MainPanel); } h = 1; l = (cProcess)pList.get(Ind); RestSized.add(DevCaptions.MainPanel); RestSized.add(l.Devices[0].MainPanel); RestSized.add(l.Devices[1].MainPanel); RestSized.add(l.Devices[2].MainPanel); RestSized.add(DevAvailable.MainPanel); } public void AddProcess() { pList.add(new cProcess()); ((DefaultListModel)(Processes.getModel())).addElement("Process" + (c++)); Processes.setSelectedIndex(c - 1); ((cProcess)pList.get(pList.size() - 1)).setCount(fCount); ShowProc(c - 1); } public void DeleteProcess(int Ind) { if ((Ind < 0) | (Ind >= pList.size())) return ; pList.remove(Ind+1); ((DefaultListModel)(Processes.getModel())).removeElementAt(Ind); } public void pWorkAlgo() { //Algo - Doesnt work yet /* { int pCount2Do = pList.size(); int resCount = DevAvailable.fCount; int[] available = new int[resCount]; int[][][] processes = new int[pCount2Do][2][resCount]; int i, j, k; return Integer.parseInt(((JTextField)Resource.get(Index)).getText()); for (i = 0; i < resCount; i++) available[i] = Integer.parseInt((JTextField)(DevAvailable.Resource.get(i)).getText()); for (i = 0; i < pCount2Do; i++) for (j = 0; j < resCount; j++) { processes[i][0][j] = Integer.parseInt(((JTextField)((cProcess)(pList.get(i)).Devices[0].get(i)).getText())); processes[i][1][j] = Integer.parseInt(((JTextField)((cProcess)(pList.get(i)).Devices[1].get(i)).getText())); } } } */ MainPanel.setSize(1, ContentPane.getHeight()); MainPanel.setVisible(false); ResultPanel.setSize(ContentPane.getWidth(), ContentPane.getHeight()); ResultPanel.setVisible(true); SetSize(); } public void actionPerformed(ActionEvent event) { if (event.getActionCommand().equals("Add Process")) AddProcess(); if (event.getActionCommand().equals("Delete Process")) DeleteProcess(ProcCount()); if (event.getActionCommand().equals("Add Resource")) SetDevCount(fCount + 1); if (event.getActionCommand().equals("Delete Resource")) if (fCount < 0) SetDevCount(fCount - 1); if (event.getActionCommand().equals("Show Process")) ShowProc(ProcCount()); if (event.getActionCommand().equals("Work Algo")) pWorkAlgo(); if (event.getActionCommand().equals("Ok")) { MainPanel.setVisible(true); ResultPanel.setVisible(false); MainPanel.setSize(ContentPane.getWidth(), ContentPane.getHeight()); SetSize(); } } } public class BankerAlgo extends JFrame { int RecCount; cPC PC; public static void main(String[] args) { BankerAlgo wnd = new BankerAlgo(); wnd.setSize(600, 500); wnd.setLocation(100, 100); wnd.setVisible(true); wnd.RecCount = 3; } public BankerAlgo() { super("Banker-Algo"); // addWindowListener(new WindowClosingAdapter(true)); System.out.println("\n\nJava ist so langsam, dass ein Käfer im Vergleich dazu mit gezogener Handbremse im Rückwärtsgang abgeht wie eine Rakete!\n\n"); PC = new cPC(this); getContentPane().add(PC.ContentPane); } } Wenn ich in der Methode cPC.WorkAlgo() die Kommentarzeichen (/* und */) wegtu, bekomm ich ne Exception [equote="'Highspeed'-Java-Interpreter klagt nach 5h interpretieren:"] java.exe error - Exception in thread "main" java.lang.NoClassDefFoundEr [/equote] Zudem (Ich stell die Frage mal hier rein, weil alles am selben Quellcode hängt, mach aber wenns jemanden stört nen neuen Thread auf): Ich setz manchmal die Größe von MainPanel und ResultPanel, welche beide im ContentPane sind (Immer in der klasse cPC). Dabei verändert sich aber die Größe kein bisschen. Die Änderung des Visible-statuses seh ich auch erst, nachdem ich das Forum resize (siehe SetSize()-Methode). bzw. is mir allgemein aufgefallen, dass sich das ganze manchmal wehrt, mir das anzuzeigen, was ich will (siehe auch wo setVisible(false); setVisible(true); is...) Kann man das irgendwie ändern? Hängt das mit dem ersten Problem irgendwie zusammen? Oder ist das gar nicht meine Schuld, sondern ist Java einfach nur zu dumm dafür? PS: Das ganze passierte unter Red Hat auf nem PC in meiner Schule, werds also erst Montag wieder probieren können
Mike
Passion is no replacement for reason |
Zitat |
Ansicht |
Linear-Darstellung |
Zur Hybrid-Darstellung wechseln |
Zur Baum-Darstellung wechseln |
ForumregelnEs ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.
BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus. Trackbacks are an
Pingbacks are an
Refbacks are aus
|
|
Nützliche Links |
Heutige Beiträge |
Sitemap |
Suchen |
Code-Library |
Wer ist online |
Alle Foren als gelesen markieren |
Gehe zu... |
LinkBack |
LinkBack URL |
About LinkBacks |