Hallo zusammen,
ich probiere jetzt schon eine ganze Weile rum und kriegs nicht gebacken. Ich habe eine GUI programmiert mit einem JSplitPane. Im linken Bereich wird eine HTML-Seite angezeigt, die allgemeine Hinweise zum Programm enthält. Rechts erscheint ein JTabbedPane mit verschiedenen Inhalten. Soweit so gut.
Wenn ich aber jetzt im Menü etwas auswähle und darauf hin der Inhalt des rechten Bereichs aktualisiert, also das JTabbedPane neu gezeichnet wird, wird die Breite des linken Bereichs automatisch mit verändert und auf eine Größe kleinere Größe gesetzt.
Hier ist ma der komplettel Code, weil ich wirklich nicht weiß, wo da der Fehler steckt:
Hat da jemand eine Idee, woher das seltsame Verhalten des Programms kommt - bin für jeden Tip echt dankbar!
ich probiere jetzt schon eine ganze Weile rum und kriegs nicht gebacken. Ich habe eine GUI programmiert mit einem JSplitPane. Im linken Bereich wird eine HTML-Seite angezeigt, die allgemeine Hinweise zum Programm enthält. Rechts erscheint ein JTabbedPane mit verschiedenen Inhalten. Soweit so gut.
Wenn ich aber jetzt im Menü etwas auswähle und darauf hin der Inhalt des rechten Bereichs aktualisiert, also das JTabbedPane neu gezeichnet wird, wird die Breite des linken Bereichs automatisch mit verändert und auf eine Größe kleinere Größe gesetzt.
Hier ist ma der komplettel Code, weil ich wirklich nicht weiß, wo da der Fehler steckt:
Code:
/*
* Created on 29.04.2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package gui;
//Benoetigte Packete:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author Thorsten
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class GUI extends JFrame implements ActionListener {
// Name der Anwendung:
protected final static String lblAppName = "Programmieren & Modellieren 2 - Framework";
// Abstand des Anwendungsfensters vom Bildschirmrand festlegen:
protected final static int inset = 65;
// Desktop einrichten:
protected static JDesktopPane desktop = new JDesktopPane();
// Elemente der GUI:
protected static JMenuBar menuBar;
protected static JSplitPane splitPane;
protected static JPanel leftPanel, rightPanel;
// Voreingestellte Groesse des linken Bildschirmbereichs:
protected final static int defaultDividerLocation = 250;
// Rubrik, in dem der aktuelle Workshop steht:
protected static String strMainItem = "J2EE Grundlagen";
// Ausgewaehlter Menuepunkt:
protected static Object objSelectedItem = null;
protected static String strSelectedItem = "Java Beans";
// Menueauswahl, bei der das Programm beendet wird:
protected final static String lblQuit = "Programm beenden";
/**
* Standardkonstruktor
*/
public GUI() {
// Konstruktor der Basisklasse aufrufen
// (Anwendungsname setzen):
super(lblAppName);
// Eigenschaften des Anwendungsfensters setzen:
this.setWindowProperties();
//Make dragging a little faster but perhaps uglier.
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
// Look & Feel verbessern:
setDefaultLookAndFeelDecorated(true);
// Zum Beenden der Anwendung:
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// GUI anzeigen:
//this.show();
}
/**
* Fenstereigenschaften festlegen:
*
*/
private void setWindowProperties() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(inset, inset, screenSize.width - inset*2, screenSize.height - inset*2);
}
/**
* Anwendung schliessen:
*/
protected void quit() {
System.exit(0);
}
/**
* Menueleiste generieren:
*/
protected void createMenuBar() {
menuBar = new JMenuBar();
JMenu menu;
JMenuItem item;
// Datei:
menu = new JMenu("Datei");
menu.setMnemonic('D');
item = new JMenuItem("Programm beenden");
item.setMnemonic('e');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
// J2EE Grundlagen:
menu = new JMenu("J2EE Grundlagen");
menu.setMnemonic('G');
item = new JMenuItem("Java Beans");
item.setMnemonic('J');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Swing");
item.setMnemonic('S');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Datenbankanbindung mit JDBC");
item.setMnemonic('J');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Remote Objects und RMI");
item.setMnemonic('R');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
// J2EE EJB:
menu = new JMenu("J2EE EJB");
menu.setMnemonic('E');
item = new JMenuItem("Session Beans");
item.setMnemonic('S');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Entity Beans");
item.setMnemonic('n');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Message Driven Beans");
item.setMnemonic('M');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Transaktionsverwaltung");
item.setMnemonic('T');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
// J2EE Web-Development:
menu = new JMenu("J2EE Web-Development");
menu.setMnemonic('W');
item = new JMenuItem("Java Servlets");
item.setMnemonic('S');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Java Server Pages");
item.setMnemonic('P');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
// Birds & Feathers:
menu = new JMenu("Birds & Feathers");
menu.setMnemonic('B');
item = new JMenuItem("Sicherheit");
item.setMnemonic('S');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("J2ME");
item.setMnemonic('J');
item.addActionListener(this);
menu.add(item);
item = new JMenuItem("Web Services");
item.setMnemonic('W');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
// Hilfe:
menu = new JMenu("Hilfe");
menu.setMnemonic('H');
item = new JMenuItem("Über das Framework...");
item.setMnemonic('Ü');
item.addActionListener(this);
menu.add(item);
menuBar.add(menu);
}
/**
* Menueleiste hinzufuegen
*/
protected void addMenuBar() {
this.createMenuBar();
this.setJMenuBar(menuBar);
}
/**
* SplitPane generieren
*
* @param left -linke Komponente
* @param right rechte Komponente
*
* @return JSplitPane
*/
protected void createSplitPane() {
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
//splitPane.setPreferredSize(new Dimension(250, 350));
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(defaultDividerLocation);
}
/**
* Neues skrollbares TextPane erzeugen
*
* @return JScrollPane
*/
protected JScrollPane createScrollTextPane(final String url) {
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setEditable(false);
textPane.setMargin(new Insets(5, 5, 5, 5));
String text = readFile(url);
textPane.setText(text);
JScrollPane scrollPane = new JScrollPane(textPane);
return scrollPane;
}
/**
* Einlesen einer Datei
* aus dem Verzeichnis 'strFileRoot' mit dem Namen 'strFileName'
*
* @param strFileName
* @return
* @throws IOException
*/
protected static String readFile(final String strFileName) {
String strNewLine = System.getProperty("line.separator");
StringBuffer strFile = new StringBuffer();
String strLine = new String();
try {
FileReader frInput = new FileReader(strFileName);
BufferedReader brInput = new BufferedReader(frInput);
int z=0;
while((strLine = brInput.readLine()) != null) {
if(z>0)
strFile.append(strNewLine);
strFile.append(strLine);
z++;
}
} catch(IOException e) {
System.err.println(e);
}
return strFile.toString();
}
/**
* Register-Panel mit Text fuellen
*
* @param intIndex
* @param pane
* @return
*/
protected static JTextPane setPaneText(final int intIndex, JTextPane pane) {
String strFile = new String();
switch(intIndex) {
case 0: // Aufgabenstellung
// Menue 'J2EE Grundlagen':
if(strSelectedItem == "Java Beans")
strFile = readFile("src/gui/files/java_beans_lesson.html");
else if(strSelectedItem == "Swing")
strFile = readFile("src/gui/files/swing_lesson.html");
else if(strSelectedItem == "Datenbankanbindung mit JDBC")
strFile = readFile("src/gui/files/jdbc_lesson.html");
else if(strSelectedItem == "Remote Objects und RMI")
strFile = readFile("src/gui/files/rmi_lesson.html");
// Menue 'J2EE EJB':
else if(strSelectedItem == "Session Beans")
strFile = readFile("src/gui/files/session_beans_lesson.html");
else if(strSelectedItem == "Entity Beans")
strFile = readFile("src/gui/files/entity_beans_lesson.html");
else if(strSelectedItem == "Message Driven Beans")
strFile = readFile("src/gui/files/message_driven_beans_lesson.html");
else if(strSelectedItem == "Transaktionsverwaltung")
strFile = readFile("src/gui/files/transactions_lesson.html");
// Menue 'J2EE Web-Development':
else if(strSelectedItem == "Java Servlets")
strFile = readFile("src/gui/files/servlets_lesson.html");
else if(strSelectedItem == "Java Server Pages")
strFile = readFile("src/gui/files/jsp_lesson.html");
// Menue 'Birds & Feathers':
else if(strSelectedItem == "Sicherheit")
strFile = readFile("src/gui/files/security_lesson.html");
else if(strSelectedItem == "J2ME")
strFile = readFile("src/gui/files/j2me_lesson.html");
else if(strSelectedItem == "Web Services")
strFile = readFile("src/gui/files/web_services_lesson.html");
break;
case 1: // Loesungshinweise
// Menue 'J2EE Grundlagen':
if(strSelectedItem == "Java Beans")
strFile = readFile("src/gui/files/java_beans_hints.html");
else if(strSelectedItem == "Swing")
strFile = readFile("src/gui/files/swing_hints.html");
else if(strSelectedItem == "Datenbankanbindung mit JDBC")
strFile = readFile("src/gui/files/jdbc_hints.html");
else if(strSelectedItem == "Remote Objects und RMI")
strFile = readFile("src/gui/files/rmi_hints.html");
// Menue 'J2EE EJB':
else if(strSelectedItem == "Session Beans")
strFile = readFile("src/gui/files/session_beans_hints.html");
else if(strSelectedItem == "Entity Beans")
strFile = readFile("src/gui/files/entity_beans_hints.html");
else if(strSelectedItem == "Message Driven Beans")
strFile = readFile("src/gui/files/message_driven_beans_hints.html");
else if(strSelectedItem == "Transaktionsverwaltung")
strFile = readFile("src/gui/files/transactions_hints.html");
// Menue 'J2EE Web-Development':
else if(strSelectedItem == "Java Servlets")
strFile = readFile("src/gui/files/servlets_hints.html");
else if(strSelectedItem == "Java Server Pages")
strFile = readFile("src/gui/files/jsp_hints.html");
// Menue 'Birds & Feathers':
else if(strSelectedItem == "Sicherheit")
strFile = readFile("src/gui/files/security_hints.html");
else if(strSelectedItem == "J2ME")
strFile = readFile("src/gui/files/j2me_hints.html");
else if(strSelectedItem == "Web Services")
strFile = readFile("src/gui/files/web_services_hints.html");
break;
case 2: // Programmbeispiel
// Menue 'J2EE Grundlagen':
if(strSelectedItem == "Java Beans")
strFile = readFile("src/gui/files/java_beans_example.html");
else if(strSelectedItem == "Swing")
strFile = readFile("src/gui/files/swing_example.html");
else if(strSelectedItem == "Datenbankanbindung mit JDBC")
strFile = readFile("src/gui/files/jdbc_example.html");
else if(strSelectedItem == "Remote Objects und RMI")
strFile = readFile("src/gui/files/rmi_example.html");
// Menue 'J2EE EJB':
else if(strSelectedItem == "Session Beans")
strFile = readFile("src/gui/files/session_beans_example.html");
else if(strSelectedItem == "Entity Beans")
strFile = readFile("src/gui/files/entity_beans_example.html");
else if(strSelectedItem == "Message Driven Beans")
strFile = readFile("src/gui/files/message_driven_beans_example.html");
else if(strSelectedItem == "Transaktionsverwaltung")
strFile = readFile("src/gui/files/transactions_example.html");
// Menue 'J2EE Web-Development':
else if(strSelectedItem == "Java Servlets")
strFile = readFile("src/gui/files/servlets_example.html");
else if(strSelectedItem == "Java Server Pages")
strFile = readFile("src/gui/files/jsp_example.html");
// Menue 'Birds & Feathers':
else if(strSelectedItem == "Sicherheit")
strFile = readFile("src/gui/files/security_example.html");
else if(strSelectedItem == "J2ME")
strFile = readFile("src/gui/files/j2me_example.html");
else if(strSelectedItem == "Web Services")
strFile = readFile("src/gui/files/web_services_example.html");
break;
case 3: // Loesung
// Menue 'J2EE Grundlagen':
if(strSelectedItem == "Java Beans")
strFile = readFile("src/gui/files/java_beans_complete.html");
else if(strSelectedItem == "Swing")
strFile = readFile("src/gui/files/swing_complete.html");
else if(strSelectedItem == "Datenbankanbindung mit JDBC")
strFile = readFile("src/gui/files/jdbc_complete.html");
else if(strSelectedItem == "Remote Objects und RMI")
strFile = readFile("src/gui/files/rmi_complete.html");
// Menue 'J2EE EJB':
else if(strSelectedItem == "Session Beans")
strFile = readFile("src/gui/files/session_beans_complete.html");
else if(strSelectedItem == "Entity Beans")
strFile = readFile("src/gui/files/entity_beans_complete.html");
else if(strSelectedItem == "Message Driven Beans")
strFile = readFile("src/gui/files/message_driven_beans_complete.html");
else if(strSelectedItem == "Transaktionsverwaltung")
strFile = readFile("src/gui/files/transactions_complete.html");
// Menue 'J2EE Web-Development':
else if(strSelectedItem == "Java Servlets")
strFile = readFile("src/gui/files/servlets_complete.html");
else if(strSelectedItem == "Java Server Pages")
strFile = readFile("src/gui/files/jsp_complete.html");
// Menue 'Birds & Feathers':
else if(strSelectedItem == "Sicherheit")
strFile = readFile("src/gui/files/security_complete.html");
else if(strSelectedItem == "J2ME")
strFile = readFile("src/gui/files/j2me_complete.html");
else if(strSelectedItem == "Web Services")
strFile = readFile("src/gui/files/web_services_complete.html");
break;
default:
strFile = readFile("src/gui/files/roadmap.html");
}
pane.setText(strFile);
return pane;
}
/**
* Panel hinzufuegen
*
* @param intIndex
* @return
*/
private static JPanel createPanel(final int intIndex) {
JPanel tab = new JPanel();
JTextPane pane = new JTextPane();
JScrollPane areaScrollPane = new JScrollPane(pane);
String strBorderTitle = new String(strMainItem + " - " + strSelectedItem);
tab.setLayout(new GridLayout());
pane.setAutoscrolls(true);
pane.setContentType("text/html");
pane.setEditable(false);
pane = setPaneText(intIndex, pane);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(strBorderTitle),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));
pane.setMargin(new Insets(10,10,15,10));
pane.setCaretPosition(0);
tab.add(areaScrollPane);
return tab;
}
/**
* Register-Panel generieren
*/
private void createRightPanel() {
// Panel und Register anlegen:
rightPanel = new JPanel(new GridLayout(1,1));
JTabbedPane pane = new JTabbedPane();
JComponent tab;
// Aufgabenstellung:
tab = createPanel(0);
pane.addTab("Aufgabenstellung", new ImageIcon("src/gui/images/middle.gif"), tab);
// Loesungshinweise:
tab = createPanel(1);
pane.addTab("Lösungshinweise", new ImageIcon("src/gui/images/middle.gif"), tab);
// Programmbeispiel:
tab = createPanel(2);
pane.addTab("Programmbeispiel", new ImageIcon("src/gui/images/middle.gif"), tab);
/*
// Loesung (erstmal deaktiviert!)
tab = createPanel(3);
pane.addTab("Lösung", new ImageIcon("src/gui/images/middle.gif"), tab);
pane.setEnabledAt(3, false);
*/
// Register zum Container hinzufuegen
rightPanel.add(pane);
// Scroll-Funktion fuer Register einschalten:
pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
/**
* Linken Bereich der GUI generieren
*
* @return JComponent
*/
protected void createLeftPanel() {
Border raisedetched, loweredetched, raisedbevel, loweredbevel;
raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());
JButton button = new JButton("Kursaufbau");
//button.setEnabled(false);
button.setFocusable(false);
button.setMargin(new Insets(5, 5, 5, 5));
//button.setBorder(BorderFactory.createCompoundBorder(raisedetched, loweredetched));
JScrollPane scrollPane = this.createScrollTextPane("src/gui/files/roadmap.html");
leftPanel.add(button, BorderLayout.NORTH);
leftPanel.add(scrollPane, BorderLayout.CENTER);
}
/**
* SplitPane hinzufuegen
*/
protected void addSplitPane() {
this.createLeftPanel();
this.createRightPanel();
this.createSplitPane();
this.getContentPane().add(splitPane);
}
/**
* GUI anzeigen
*/
public void createAndShowGUI() {
this.getContentPane().setLayout(new BorderLayout());
this.addMenuBar();
this.addSplitPane();
this.setVisible(true);
}
/**
* Programm-Info anzeigen
*/
private static void about() {
String msg = readFile("src/gui/files/infomessage.txt");
String title = "Über das Framework...";
JOptionPane.showMessageDialog(desktop, msg, title, 1);
}
/**
* Menuepunkt hervorheben
*
* @param item
*/
protected void highlightMenuItem(final JMenuItem item) {
if(item == null)
return;
// ausgewaehlten Menuepunkt hervorheben:
//item.setBackground(new Color(173,59,49));
item.setForeground((new Color(0,0,178)));
}
/**
* Menuepunkt zuruecksetzen
*
* @param item
*/
protected void resetMenuItem(final JMenuItem item) {
if(item == null)
return;
item.setBackground(new Color(204,204,204));
item.setForeground(new Color(0,0,0));
}
/**
* Haupt-Menuepunkt zu einem Menueeintrag ermitteln
*
* @param strItem
*
* @return String
*/
protected String getMainLabel(final String strItem) {
if(strItem == "Java Beans" || strItem == "Swing" || strItem == "Datenbankanbindung mit JDBC" || strItem == "Remote Objects und RMI")
return "J2EE Grundlagen";
if(strItem == "Session Beans" || strItem == "Entity Beans" || strItem == "Message Driven Beans" || strItem == "Transaktionsverwaltung")
return "J2EE EJB";
if(strItem == "Java Servlets" || strItem == "Java Server Pages")
return "J2EE Web Development";
if(strItem == "Sicherheit" || strItem == "J2ME" || strItem == "Web Services")
return "Birds & Feathers";
else
return "";
}
/**
* Benutzereingaben auswerten:
*/
public void actionPerformed(ActionEvent ae) {
//MenuSelectionManager msm = new MenuSelectionManager();
// zuvor ausgewaehlten Menuepunkt zuruecksetzen:
this.resetMenuItem(((JMenuItem)objSelectedItem));
// ausgewaehlten Menuepunkt speichern:
objSelectedItem = ae.getSource();
strSelectedItem = ((JMenuItem)objSelectedItem).getLabel();
// Ausgewaehlten Menuepunkt highlighten:
this.highlightMenuItem((JMenuItem)objSelectedItem);
if(strSelectedItem == lblQuit) {
this.quit();
} else if(strSelectedItem == "Über das Framework...") {
about();
} else {
// TabbedPanel aktualisieren:
this.createRightPanel();
splitPane.setRightComponent(rightPanel);
}
}
/**
* Steuerteil
*
* @param args
*/
public static void main(String[] args) {
GUI myGUI = new GUI();
myGUI.createAndShowGUI();
}
}
Hat da jemand eine Idee, woher das seltsame Verhalten des Programms kommt - bin für jeden Tip echt dankbar!