?? maingui.java
字號:
/** Programmname : MainGUI <br>Klasse : <b> MainGUI </b><hr>Aufgabe : Zentrale Oberfl鋍he zur Pkw-Verwaltung<hr>@autor Li,Haibin Hou,Yujing Zhang,Xiangpeng@version 1.0 / 15.12.2006*/import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.util.Observable;import java.util.Observer;import java.awt.print.*;public class MainGUI extends JFrame //JFrame bietet Grundger黶timplements Observer{ /** PkwArray f黵 die Visualisierung in der JTextArea */ private PkwArray aPkwArray; // Referenz auf PkwArray // private boolean exit,load; /** Button in der MainGUI */ private JButton loadJButton,saveJButton,helpJButton,exitJButton, insertJButton,editJButton, printGUIJButton, printPkwJButton; private JButtonActionListener aJButtonActionListener; private MainGUIWindowListener aMainGUIWindowListener; private JLabel countJLabel,arrayJLabel; private JTextField countJTextField; private JTextArea arrayJTextArea; private JScrollPane aJScrollPane; private static Font aFont=new Font("Courier",Font.BOLD,30); private static Font cFont=new Font("Courier",Font.BOLD,12); private static Color fColor=Color.white; private static Color editColor=Color.blue; private static Color noeditColor=Color.red; private static Color textColor=Color.white; private int pl=50,po=50,pw=100,ph=30; private int plbutton,pwbutton=100,pdelta=120; private int count; private InsertJDialog aInsertJDialog; private HelpJDialog aHelpJDialog; private EditJDialog aEditJDialog; private JFileChooser aJFileChooser; public MainGUI() { super("PkwMainGUI"); setIconImage(getToolkit().getImage("picto.gif")); setSize(600,500); setLocation(30,40); getContentPane().setLayout(null); getContentPane().setBackground(fColor); aMainGUIWindowListener = new MainGUIWindowListener(this); this.addWindowListener(aMainGUIWindowListener); aJButtonActionListener = new JButtonActionListener(this); loadJButton= new JButton("LOAD"); loadJButton.setBounds(plbutton=pl,po,pwbutton,ph); loadJButton.setVisible(true); getContentPane().add(loadJButton); loadJButton.addActionListener(aJButtonActionListener); saveJButton= new JButton("SAVE"); saveJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); saveJButton.setVisible(true); getContentPane().add(saveJButton); saveJButton.addActionListener(aJButtonActionListener); helpJButton= new JButton("HELP"); helpJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); helpJButton.setVisible(true); getContentPane().add(helpJButton); helpJButton.addActionListener(aJButtonActionListener); exitJButton= new JButton("EXIT"); exitJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); exitJButton.setVisible(true); getContentPane().add(exitJButton); exitJButton.addActionListener(aJButtonActionListener); insertJButton= new JButton("INSERT"); insertJButton.setBounds(plbutton=pl,po+=2*ph,pwbutton,ph); insertJButton.setVisible(true); getContentPane().add(insertJButton); insertJButton.addActionListener(aJButtonActionListener); editJButton= new JButton("EDIT"); editJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); editJButton.setVisible(true); getContentPane().add(editJButton); editJButton.addActionListener(aJButtonActionListener); printGUIJButton= new JButton("PRINT GUI"); printGUIJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); printGUIJButton.setVisible(true); getContentPane().add(printGUIJButton); printGUIJButton.addActionListener(aJButtonActionListener); printPkwJButton= new JButton("PRINT PKW"); printPkwJButton.setBounds(plbutton+=pdelta,po,pwbutton,ph); printPkwJButton.setVisible(true); getContentPane().add(printPkwJButton); printPkwJButton.addActionListener(aJButtonActionListener); countJLabel=new JLabel("Anzahl"); countJLabel.setBounds(pl,po+=3*ph,4*pw,ph); countJLabel.setFont(aFont); countJLabel.setVisible(true); getContentPane().add(countJLabel); arrayJLabel=new JLabel("Pkw-Array"); arrayJLabel.setBounds(pl+2*pdelta,po,4*pw,ph); arrayJLabel.setFont(aFont); arrayJLabel.setVisible(true); getContentPane().add(arrayJLabel); countJTextField=new JTextField(); countJTextField.setBounds(pl,po+=2*ph,pw,ph); countJTextField.setFont(cFont); countJTextField.setEditable(false); countJTextField.setBackground(noeditColor); countJTextField.setForeground(textColor); countJTextField.setVisible(true); getContentPane().add(countJTextField); arrayJTextArea = new JTextArea(); arrayJTextArea.setFont(cFont); arrayJTextArea.setEditable(false); arrayJTextArea.setBackground(noeditColor); arrayJTextArea.setForeground(textColor); aJScrollPane = new JScrollPane(arrayJTextArea); aJScrollPane.setBounds(pl+pdelta,po,4*pw,6*ph); aJScrollPane.setVisible(true); getContentPane().add(aJScrollPane); aInsertJDialog = new InsertJDialog(this,true); aHelpJDialog = new HelpJDialog(this,false); aEditJDialog = new EditJDialog(this,true); aJFileChooser = new JFileChooser("."); (Pkw.getPkwArr()).addObserver(this); setLinkPkwArray( Pkw.getPkwArr() ); setVisible(true); } public void setLinkPkwArray ( PkwArray p ) { aPkwArray = p ; } public PkwArray getLinkPkwArray() { return aPkwArray; } public boolean removeLinkPkwArray() { return aPkwArray == null ? false : true; } /* update() *******/ public void update(Observable o, Object obj) { countJTextField.setText(Integer.toString(Pkw.getPkwArr().getCount())); arrayJTextArea.setText(Pkw.alltoString()); } public void callBackLoadJButton() { int status = aJFileChooser.showOpenDialog(this);//Anzeige des Dialogs File aFile = aJFileChooser.getSelectedFile(); //File-Objekt der Auswahl if ( aFile == null || status != 0 ) { JOptionPane.showMessageDialog(this,"LOAD abgebrochen!"); } else { String dirString = aFile.getParent(); //String der Directory-Namens String fnameString = aFile.getName(); //String des Dateinamens String pathnameString = dirString + File.separator + fnameString; //Erzeugung des Pfadnamens System.out.println("Load von Datei:" + pathnameString); boolean x = Pkw.load(pathnameString); if(x) { aPkwArray = Pkw.getPkwArr(); JOptionPane.showMessageDialog(this,"LOAD ist erfolgt!"); } } aHelpJDialog.setText("LOAD-Button:\n" + "Der LOAD-Button wird benutzt, wenn\n" + "man ein gespeichertes Pkw-Array\n" + "laden will\n"); aHelpJDialog.init(); } public void callBackSaveJButton() { int status = aJFileChooser.showSaveDialog(this); File aFile = aJFileChooser.getSelectedFile(); if (aFile == null || status != 0){ JOptionPane.showMessageDialog(this, "Save abgebrochen!"); } else{ String dirString = aFile.getParent(); String fNameString = aFile.getName(); String pathNameString = dirString + File.separator + fNameString; System.out.println("Save nach Datei:" +pathNameString); Pkw.save(pathNameString); JOptionPane.showMessageDialog(this,"SAVE ist erfolgt!"); } aHelpJDialog.setText("SAVE-Button:\n" + "Der Save-Button wird benutzt, wenn\n" + "man das Pkw-Array abspeichern\n" + "will.\n"); aHelpJDialog.init(); } public void callBackHelpJButton() { aHelpJDialog.setText( "SW-Projekt !!! \n" + "autor: Li,Haibin Hou,Yujing Zhang,Xiangpeng\n" + "version: 1.0 / 15.12.2006\n"); aHelpJDialog.init(); aHelpJDialog.setVisible(true); } public void callBackExitJButton() { System.exit(0); } public void callBackInsertJButton() { aHelpJDialog.setText("INSERT-Button\n" + "Der INSERT-Button wird benutzt, um\n" + "ein neuen Pkw in das Pkw-Array abzuspeichern\n"); aHelpJDialog.init(); aInsertJDialog.setVisible(true); } public void callBackEditJButton() { if(aPkwArray.getCount()!=0){ int selectionStart = arrayJTextArea.getSelectionStart(); int numberOfNewLines = 0; int max = aPkwArray.getCount()-1; String theText = arrayJTextArea.getText(); for(int pos=0; (selectionStart > pos)&&(numberOfNewLines < max); pos++){ if(theText.charAt(pos)=='\n'){ numberOfNewLines++; } } System.out.println("selectionStart: "+selectionStart+"numberOfNewLines: "+numberOfNewLines); aEditJDialog.setLinkPkwArray(aPkwArray); aEditJDialog.setPosition(numberOfNewLines); aEditJDialog.setVisible(true); } aHelpJDialog.setText("EDIT-Button\n" + "Der EDIT-Button wird benutzt, um\n" + "einen Pkw zu bearbeiten\n"); aHelpJDialog.init(); } public void callBackPrintGuiJButton() { MainGUIPrint aMainGUIPrint = new MainGUIPrint(this); aMainGUIPrint.execute(); System.out.println("PRINT GUI-Button pressed!!!"); aHelpJDialog.setText("PRINT GUI-Button:\n" + "Der PRINT GUI-Button wird benutzt, wenn\n" + "man das Main-Fanster ausdr點ken will"); aHelpJDialog.init(); } public void callBackPrintPkwJButton() { PkwArrayPrint aPkwArrayPrint = new PkwArrayPrint(aPkwArray); aPkwArrayPrint.execute(); System.out.println("PRINT PKW-Button pressed!!!"); aHelpJDialog.setText("PRINT PKW-Button:\n" + "Der PRINT PKW-Button wird benutzt, wenn\n" + "man alle PKW ausdr點ken will.\n"); aHelpJDialog.init(); } public void callBackWindowClosing() { System.exit(0); } }class JButtonActionListener implements ActionListener{ MainGUI aGUI; public JButtonActionListener(MainGUI mGUI) { aGUI = mGUI; } public void actionPerformed ( ActionEvent evt ) { String [] commandString = {"LOAD","SAVE","HELP","EXIT","INSERT","EDIT", "PRINT GUI","PRINT PKW"}; int i,nr=-1; for ( i=0, nr=-1 ; i < commandString.length ; i++ ) if ( commandString[i].equals(evt.getActionCommand())) { nr=i; break; } switch ( nr ) { case 0 : aGUI.callBackLoadJButton(); break; case 1 : aGUI.callBackSaveJButton(); break; case 2 : aGUI.callBackHelpJButton(); break; case 3 : aGUI.callBackExitJButton(); break; case 4 : aGUI.callBackInsertJButton(); break; case 5 : aGUI.callBackEditJButton(); break; case 6 : aGUI.callBackPrintGuiJButton(); break; case 7 : aGUI.callBackPrintPkwJButton(); break; default : break; } }}class MainGUIWindowListenerimplements WindowListener{ private MainGUI mGUI; public MainGUIWindowListener(MainGUI aGUI){mGUI=aGUI;} public void windowActivated(WindowEvent e) { System.out.println("windowActivated"); } public void windowClosed(WindowEvent e) { System.out.println("windowClosed"); } public void windowClosing(WindowEvent e) { System.out.println("windowClosing"); mGUI.callBackWindowClosing(); } public void windowDeactivated(WindowEvent e) { System.out.println("windowDeactivated"); } public void windowDeiconified(WindowEvent e) { System.out.println("windowDeiconified"); } public void windowIconified(WindowEvent e) { System.out.println("windowIconified"); } public void windowOpened(WindowEvent e) { System.out.println("windowOpened"); }}class MainGUIPrint implements Printable{ private MainGUI mGUI; public MainGUIPrint( MainGUI aGUI ) { mGUI = aGUI; } public void execute() { PrinterJob pjob = PrinterJob.getPrinterJob(); PageFormat pf = pjob.defaultPage(); pf=pjob.pageDialog(pf); //Auswahl des Seitenformats mit dem Page-Dialog pjob.setPrintable(new MainGUIPrint(mGUI),pf); if (pjob.printDialog()) //Auswahl von Drucker bzw. Datei mit Print-Dialog try { pjob.print(); } catch ( PrinterException pex) {System.out.println("Printer Exception!");} } public int print(Graphics g,PageFormat pf,int pageIndex) { System.out.println("PrintMainGUI-Function "); if( pageIndex > 0 ) return Printable.NO_SUCH_PAGE; Graphics2D g2D = (Graphics2D) g; g2D.setClip(0,0,600,1000); drawGraphics(g2D,pf); return Printable.PAGE_EXISTS; } public void drawGraphics( Graphics2D g2D,PageFormat pf) { mGUI.print(g2D);}}class PkwArrayPrint implements Printable{ private PkwArray printPkwArray; private int anz; // Horizontaltabulatoren der Spaltenbezeichner und Felder private static int identnummerHT = 100; private static int typHT = 120; private static int herstellerHT = 200; private static int farbeHT = 300; private static int kmleistungHT = 400; private static int preisHT = 470; //Vertikaltabulatoren fuer Ueberschrift, Spaltenbezeichner und 1.Pkw-Zeile private static int hVT = 90; private static int sVT = 120; private static int zVT = 150; private static int dy = 15; // y-Differenz zweier Pkw- Zeilen // Fonts fuer Ueberschrift, Spaltenbezeichner und Pkw- Zeilen private static Font hFont = new Font("Courier",Font.BOLD,16); private static Font sFont = new Font("Courier",Font.BOLD,10); private static Font zFont = new Font("Arial",Font.PLAIN,10); public PkwArrayPrint( PkwArray p ) // Klonen des Pkw- Arrays { printPkwArray = p; } public void execute() { PrinterJob pjob = PrinterJob.getPrinterJob(); PageFormat pf = pjob.defaultPage(); pf=pjob.pageDialog(pf); pjob.setPrintable(new PkwArrayPrint(printPkwArray),pf); if ( pjob.printDialog() ) try { pjob.print(); } catch ( PrinterException pex) {System.out.println("PrinterException!");} } public int print(Graphics g,PageFormat pf,int pageIndex) { System.out.println("PrintArray-Function"); if( pageIndex > 0 ) return Printable.NO_SUCH_PAGE; Graphics2D g2D = (Graphics2D) g; g2D.translate(20,20); drawGraphics(g2D,pf); return Printable.PAGE_EXISTS; } public void drawGraphics( Graphics2D g2D,PageFormat pf) { g2D.setFont(hFont); g2D.drawString("Pkw Array",100,hVT); g2D.setFont(sFont); g2D.drawString("ID",identnummerHT,sVT); g2D.drawString("Typ",typHT,sVT); g2D.drawString("Hersteller",herstellerHT,sVT); g2D.drawString("Farbe",farbeHT,sVT); g2D.drawString("Km",kmleistungHT,sVT); g2D.drawString("Preis",preisHT,sVT); // Schleife zur Ausgabe der Pkw-Zeilen int pMax = printPkwArray.getCount(); int loopCount; int verticalPos = sVT+dy; for(loopCount=0; loopCount<pMax; loopCount++){ g2D.drawString( ""+(printPkwArray.getPkw(loopCount).getIdentnummer()) ,identnummerHT,verticalPos); g2D.drawString( printPkwArray.getPkw(loopCount).getTyp() ,typHT,verticalPos); g2D.drawString(printPkwArray.getPkw(loopCount).getHersteller() ,herstellerHT,verticalPos); g2D.drawString( printPkwArray.getPkw(loopCount).getFarbe() ,farbeHT,verticalPos); g2D.drawString( ""+(printPkwArray.getPkw(loopCount).getKmleistung()) ,kmleistungHT,verticalPos); g2D.drawString( printPkwArray.getPkw(loopCount).getFarbe() ,preisHT,verticalPos); verticalPos+=dy; } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -