Hallo meine frage wie rufe ich die methode "private void defineToolTips()" (zeile 178) auf, ist in meinem Lehrbuch nicht wirklich beschrieben.:rtfm:
Java:
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.JComponent.*;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.swing.Action;
import javax.swing.JEditorPane;
import java.io.*;
import java.net.URL;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.io.File;
public class SwingAction extends JFrame implements ActionListener{
FileBrowser fileBrowser;
JTextField jtfStatus;
Container contentPane;
JToolBar toolBar;
JEditorPane editorPane;
JFileChooser fileChooser;
URL url;
//Menüobjekte
JMenuBar menuBar;
JMenu menuFile, menuOpen, menuHelp;
JMenuItem jmiNewFile, jmiOpenFile, jmiOpenWeb, jmiClose,jmiSave,
jmiSaveAs, jmiHelp, jmiAbout;
//ToolBar Button
private JButton jbtbNew, jbtbOpenFile, jbtbOpenWeb, jbtbSave, jbtbHelp, jbtbAbout;
private String iconSize="24", title, startDir;
private Action actNewFile, actOpenFile, actOpenWeb, actSaveFile, actHelp, actAbout;
private KeyStroke keyStroke;
private File file;
public SwingAction(){
super("Swing-Frame mit Menü");
setSize(600, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=this.getContentPane();
//Statusfeld einbauen
jtfStatus=new JTextField("");
jtfStatus.setBackground(Color.black);
jtfStatus.setForeground(Color.white);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(BorderLayout.SOUTH, jtfStatus);
editorPane=new JEditorPane();
JScrollPane editorSP=new JScrollPane(editorPane);
panel.add(BorderLayout.CENTER,editorSP);
contentPane.add(BorderLayout.CENTER, panel);
file=null;
setVisible(true);
//Schnellstart Zeichen
menuFile.setMnemonic('d');
jmiOpenFile.setMnemonic('e');
jmiOpenWeb.setMnemonic('w');
menuOpen.setMnemonic('f');
jmiClose.setMnemonic('s');
jmiSave.setMnemonic('p');
jmiSaveAs.setMnemonic('a');
menuHelp.setMnemonic('h');
jmiHelp.setMnemonic('l');
jmiAbout.setMnemonic('i');
//ToolTipText
//ToolBar
toolBar = new JToolBar();
jbtbNew = toolBar.add(actNewFile);
jbtbOpenFile=toolBar.add(actOpenFile);
jbtbOpenWeb = toolBar.add(actOpenWeb);
jbtbSave = toolBar.add(actSaveFile);
toolBar.addSeparator();
jbtbHelp = toolBar.add(actHelp);
jbtbAbout = toolBar.add(actAbout);
contentPane.add(BorderLayout.NORTH, toolBar);
}
private void CreateMenu(){
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//DateiMenü
menuFile = new JMenu("Datei");
actNewFile=new AbstractAction("Neu...", new ImageIcon("images/New"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"Datei - Neu ...\" gewählt.");
}
};
//Untermenü "Öffne" (JMenu-Objekt!)
menuOpen = new JMenu ("Öffnen");
actOpenFile=new AbstractAction("Dateisystem...", new ImageIcon("images/Open"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"Datei - Öffnen - "+" Dateisystem...\" gewählt.");
}
};
keyStroke =KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK);
actOpenFile.putValue(Action.ACCELERATOR_KEY, keyStroke);
jmiOpenFile=menuOpen.add(actOpenFile);
actOpenWeb=new AbstractAction("Web...",new ImageIcon("images/WebComponent"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"Datei - Öffnen - "+" Web...\" gewählt.");
}
};
keyStroke =KeyStroke.getKeyStroke(KeyEvent.VK_U,InputEvent.CTRL_MASK);
actOpenWeb.putValue(Action.ACCELERATOR_KEY, keyStroke);
jmiOpenWeb = menuOpen.add(actOpenWeb);
menuFile.add(menuOpen);
jmiClose = new JMenuItem("Schließen");
jmiClose.addActionListener(this);
menuFile.add(jmiClose);
//Trennlinie einfügen
menuFile.addSeparator();
actSaveFile=new AbstractAction("Speichern",new ImageIcon("images/Save"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"Datei - Speichern\" gewählt.");
}
};
jmiSave=menuFile.add(actSaveFile);
menuFile.add(jmiSave);
jmiSaveAs=new JMenuItem("Speichern als...");
jmiSaveAs.addActionListener(this);
menuFile.add(jmiSaveAs);
menuBar.add(menuFile);
//Hilfe-Menü
menuHelp = new JMenu("?");
actHelp=new AbstractAction("Hilfe",new ImageIcon("images/Help"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"? - Hilfe\" gewählt.");
}
};
jmiHelp=menuHelp.add(actHelp);
menuHelp.addSeparator();
actAbout=new AbstractAction("Info",new ImageIcon("images/About"+iconSize+".gif")){
public void actionPerformed(ActionEvent ae){
jtfStatus.setText("\"? - Info\" gewählt.");
}
};
jmiAbout=menuHelp.add(actAbout);
menuBar.add(menuHelp);
}
//ToolTipText
private void defineToolTips(){
jmiOpenFile.setToolTipText("Öffne ein Datei");
jmiOpenWeb.setToolTipText("URL eingabe");
jmiClose.setToolTipText("Schließt das Programm");
jmiSave.setToolTipText("Schnell Speichern");
jmiSaveAs.setToolTipText("...speichern unter");
jmiHelp.setToolTipText("ruft Hilfe auf");
jmiAbout.setToolTipText("zeigt INFO");
}
public static void main (String [] args){
new SwingAction();
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(jmiClose))
System.exit(0);
else if (ae.getSource().equals(jmiSaveAs))
jtfStatus.setText("\"Datei - Speichern" + " als...\" gewählt");
}
public class FileBrowser{
private JTextField jtfStatus;
private JFileChooser fileChooser;
public FileBrowser(JTextField jtfStatus){
this.jtfStatus = jtfStatus;
}
}
public URL chooseURL(String title, String startDir){
URL url =null;
fileChooser = new JFileChooser(new File(startDir));
int returnVal = fileChooser.showDialog(null, title);
if(returnVal== JFileChooser.APPROVE_OPTION){
try{
url = fileChooser.getSelectedFile().toURL();
}catch (java.net.MalformedURLException murlex){
jtfStatus.setText("MalformedURLException: "+murlex.getMessage());
}
}else if(returnVal == JFileChooser.CANCEL_OPTION){
jtfStatus.setText("Keine Datei ausgewählt");
}
return url;
}
public File getFile()
{
return file;
}
}