2014-01-17 39 views
-2
import java.io.*; 
import java.awt.*; 
import org.xml.sax.*; 
import java.awt.event.*; 
import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.xml.parsers.*; 
import org.xml.sax.helpers.DefaultHandler; 

public class ch17_05 extends DefaultHandler 
{ 
    static int totalFigures = 0; 
    static int x[] = new int[100]; 
    static int y[] = new int[100]; 
    static int width[] = new int[100]; 
     public static void main(String args[]) 
    { 
     ch17_05 obj = new ch17_05(); 
     obj.childLoop("C:\\Users\\xxxx\\Desktop\\ch17_04.xml"); 

     AppFrame frame = new AppFrame(totalFigures, x, y, width); 

     frame.setSize(400, 400); 

     frame.addWindowListener(new WindowAdapter() {public void 
      windowClosing(WindowEvent e) {System.exit(0);}}); 

     frame.show(); 
    } 

    public void childLoop(String uri) 
    { 
     DefaultHandler defaultHandler = this; 
     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     try { 
      SAXParser saxParser = factory.newSAXParser(); 
      saxParser.parse(new File(uri), defaultHandler); 
     } catch (Throwable t) {} 
    } 

    public void startElement(String uri, String localName, 
     String qualifiedName, Attributes attrs) 
    { 
     if (qualifiedName.equals("square")) { 
      x[totalFigures] = Integer.parseInt(attrs.getValue("x")); 
      y[totalFigures] = Integer.parseInt(attrs.getValue("y")); 
      width[totalFigures] = Integer.parseInt(attrs.getValue("width")); 
      totalFigures++; 
     } 
    } 

    public void warning(SAXParseException exception) 
    { 
     System.err.println("Warning: " + 
      exception.getMessage()); 
    } 

    public void error(SAXParseException exception) 
    { 
     System.err.println("Error: " + 
      exception.getMessage()); 
    } 

    public void fatalError(SAXParseException exception) 
    { 
     System.err.println("Fatal error: " + 
      exception.getMessage()); 
    } 
} 

class AppFrame extends Frame 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    int totalFigures; 
    int[] xValues; 
    int[] yValues; 
    int[] widthValues; 

    public AppFrame(int number, int[] x, int[] y, int[] width) 
     { 


     totalFigures = number; 
     xValues = x; 
     yValues = y; 
     widthValues = width; 
     final JButton[] buttons = new JButton[totalFigures]; 
     for(int loopIndex = 0; loopIndex < totalFigures; loopIndex++){ 
      //System.out.println("    "+xValues[loopIndex]); 
      buttons[loopIndex] = new JButton(""+loopIndex); 
      buttons[loopIndex].setBounds(xValues[loopIndex], yValues[loopIndex],widthValues[loopIndex], widthValues[loopIndex]); 
      add(buttons[loopIndex]); 
      System.out.println(xValues[loopIndex]); 
     } 

    } 


    /* public void paint(Graphics g) 
    { 
     for(int loopIndex = 0; loopIndex < totalFigures; loopIndex++){ 
      g.drawRect(xValues[loopIndex], yValues[loopIndex], 
       widthValues[loopIndex], widthValues[loopIndex]); 
       } 
    }*/ 
}  

這是我的xml文件:在這裏,我解析XML在Java然後製作的JButton

<?xml version = "1.0" encoding="UTF-8"?> 
<!DOCTYPE document [ 
<!ELEMENT document (square)*> 
<!ELEMENT square EMPTY> 
<!ATTLIST square 
    x CDATA #IMPLIED 
    y CDATA #IMPLIED 
    width CDATA #IMPLIED> 
]> 
<document> 
    <square x='10' y='130' width='50' /> 
    <square x='140' y='180' width='15' /> 
</document> 

第一個按鈕顯示正確地,第二按鈕顯示整個幀。 請儘快給出答案。
這裏我解析XML並在Java中製作Jbutton。

+1

你究竟想要達到什麼目的?你是否遇到過這個問題?你得到的結果與你得到的結果有什麼不同? – npinti

回答

3
  1. 你不應該像你在做混合Swing和AWT組件。取而代之的是將所有Swing組件。
  2. 你不應該發佈很多與你的問題無關的代碼,因爲你必須記住我們都是志願者。而是發佈一個最小的有效完整示例MVCE。例如,XML代碼不是必需的,您可以直接發佈硬編碼數字。
  3. 請學習並遵循Java命名約定。班級名稱應以大寫字母開頭,而不是小寫字母。
  4. 這是你的家庭作業,所以你應該首先完成工作。你做了什麼來調試情況?
  5. 避免在Swing GUI中使用setBounds(...),因爲這樣做會導致您創建不靈活的GUI,這些GUI不易維護或可升級。請從這裏開始:Laying out Components within a Container。例如,您將JButtons直接添加到Frame(不要使用Frames或其他AWT組件),Frames使用BorderLayout。它看起來像你想要使用GridLayout。如果在罕見的情況下,您絕對必須使用絕對定位,則佈局必須設置爲空,但實際上這是罕見的情況。
  6. "Please give the answer for this as soon as possible." - 請將這些評論留下。記住我們是志願者,不應該被推或者趕。我們會盡快爲您解決問題。
0
import java.io.*; 
import java.awt.*; 

import org.xml.sax.*; 
import java.awt.event.*; 

import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.xml.parsers.*; 
import org.xml.sax.helpers.DefaultHandler; 

public class Ch17_05 extends DefaultHandler 
{ 
    static int totalFigures = 0; 
    static int x[] = new int[100]; 
    static int y[] = new int[100]; 
    static int width[] = new int[100]; 

    public static void main(String args[]) 
    { 
     ch17_05 obj = new ch17_05(); 
     obj.childLoop("C:\\Users\\xxx\\Desktop\\ch17_04.xml"); 

     AppFrame frame = new AppFrame(totalFigures, x, y, width); 

     frame.setSize(400, 400); 

     frame.addWindowListener(new WindowAdapter() {public void 
      windowClosing(WindowEvent e) {System.exit(0);}}); 

     frame.show(); 
    } 

    public void childLoop(String uri) 
    { 
     DefaultHandler defaultHandler = this; 
     SAXParserFactory factory = SAXParserFactory.newInstance(); 
     try { 
      SAXParser saxParser = factory.newSAXParser(); 
      saxParser.parse(new File(uri), defaultHandler); 
     } catch (Throwable t) {} 
    } 

    public void startElement(String uri, String localName, 
     String qualifiedName, Attributes attrs) 
    { 
     if (qualifiedName.equals("square")) { 
      x[totalFigures] = Integer.parseInt(attrs.getValue("x")); 
      y[totalFigures] = Integer.parseInt(attrs.getValue("y")); 
      width[totalFigures] = Integer.parseInt(attrs.getValue("width")); 
      totalFigures++; 
     } 
    } 

    public void warning(SAXParseException exception) 
    { 
     System.err.println("Warning: " + 
      exception.getMessage()); 
    } 

    public void error(SAXParseException exception) 
    { 
     System.err.println("Error: " + 
      exception.getMessage()); 
    } 

    public void fatalError(SAXParseException exception) 
    { 
     System.err.println("Fatal error: " + 
      exception.getMessage()); 
    } 
} 

class AppFrame extends Frame 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    int totalFigures; 
    int[] xValues; 
    int[] yValues; 
    int[] widthValues; 
    JPanel content = new JPanel(); 
    public AppFrame(int number, int[] x, int[] y, int[] width) 
     { 

     add(content); 
     content.setLayout(null); 
     totalFigures = number; 
     xValues = x; 
     yValues = y; 
     widthValues = width; 
     final JButton[] buttons = new JButton[totalFigures]; 
     for(int loopIndex = 0; loopIndex < totalFigures; loopIndex++){ 
      //System.out.println("    "+xValues[loopIndex]); 
      buttons[loopIndex] = new JButton(""+loopIndex); 
      buttons[loopIndex].setBounds(xValues[loopIndex], yValues[loopIndex],widthValues[loopIndex], widthValues[loopIndex]); 
      content.add(buttons[loopIndex]); 
      System.out.println(xValues[loopIndex]); 
     } 

    } 


    /* public void paint(Graphics g) 
    { 
     for(int loopIndex = 0; loopIndex < totalFigures; loopIndex++){ 
      g.drawRect(xValues[loopIndex], yValues[loopIndex], 
       widthValues[loopIndex], widthValues[loopIndex]); 
       } 
    }*/ 
}