2013-04-20 31 views
2

在這個程序中創建一個多邊形,以顯示在JPanel選項卡中。創建並覆蓋適合JPanel的形狀?

爲了讓它表現出來,我必須重載形狀併爲它創建一個setter方法。不幸的是,它沒有顯示,程序也沒有運行。

錯誤:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
  at SelectShape component1 = new SelectShape(x, y, vert); in method Page1.

它的工作的唯一方法是通過使框架和去除JTAB和分配的形狀在框架上,但是這不是我想要做什麼。我想製作一個程序,可以使用一種圖形方法將圖形分配給*不同的選項卡。

下面是代碼:

import java.awt.*; 
import java.io.IOException; 
import javax.swing.*; 


/* This program create a graphics component that draws a polygon 
*/ 
public class SelectShape extends JFrame 
{ 
    private JTabbedPane tabbedPane; 
    private JPanel panel1; 

    // ////////////////////////// 

    static int[] x = { 20, 40, 50, 65, 80, 95 }; // Co-ords for a polygon 
    static int[] y = { 60, 105, 105, 110, 95, 95 }; 
    static int vert = 6; 

    public SelectShape() throws IOException // Builds GUI 
    { 
     setTitle("Program"); 
     setSize(900, 600); 
     setBackground(Color.gray); 

     JPanel topPanel = new JPanel(); 
     topPanel.setLayout(new BorderLayout()); 
     getContentPane().add(topPanel); 

     // Create the tab pages 
     createPage1(); 

     // Create a tabbed pane 
     tabbedPane = new JTabbedPane(); 
     tabbedPane.addTab("Shape Panel", panel1); 
    } 

    public void createPage1() throws IOException // Creates JPanel 
    { 
     panel1 = new JPanel(); 
     panel1.setLayout(null); 

     SelectShape component1 = new SelectShape(x, y, vert); //error 
     SelectShape component2 = new SelectShape(x, y, vert); //over-rides shape 

     component1.setBounds(290, 70, 120, 40); 
     component2.setBounds(290, 70, 120, 40); 

     panel1.add(component1); // is not displayed! 
     panel1.add(component2); // component2 overwrites component1!!! 
     panel1.setVisible(true); 

    } 

    // overrides javax.swing.JComponent.paintComponent 
    public void paintComponent(Graphics g) { 
     // Recover Graphics2D 
     Graphics2D g2 = (Graphics2D) g; 

     // Construct a polygon then draw it 
     Polygon polygon = new Polygon(x, y, vert); 
     g2.draw(polygon); 
     g2.fill(polygon); 
    } 

    public SelectShape(int[] x, int y[], int vert) { // setter method 
     this.x = x; 
     this.y = y; 
     this.vert = vert; 
    } 

    public static void main(String[] args) throws IOException { 

     SelectShape mainFrame = new SelectShape(); //Frame 
     mainFrame.setVisible(true); 

    } 
} 
+2

'JFrame'不延長'JComponent'和沒有'paintComponent'方法。 1)擴展'Jpanel'並將其設置爲'Jframe'的內容窗格。 2)在'JPanel'中覆蓋'paintComponent' 3)使用'@ Override'註釋來防止這種情況發生,如果你認爲你的東西沒有被覆蓋,但實際上沒有。 – 2013-04-20 15:54:42

+4

@GuillaumePolet你可以添加這個答案嗎? – Reimeus 2013-04-20 15:56:30

+0

'SelectShape'類擴展了'JFrame',從這個意義上說,你試圖向'JPanel'添加一個'JFrame',而不是相反,這是造成異常的原因。 – 2013-04-20 15:58:39

回答

3

我認爲你是在你的代碼最終導致ununderstandable代碼中的許多概念混合在一起。

  • JFrame不延伸JComponent和不具有paintComponent方法。考慮在覆蓋另一個方法的方法上使用@Override註釋。這會讓你很容易犯這樣的錯誤。
  • 無需反正延長JFrame和永不覆蓋一個頂層容器的paint()方法(JDialogJFrame,...)
  • 始終調用的方法paintXXX
  • public SelectShape(int[] x, int y[], int vert) { // setter method超級方法是不是setter方法。它是一個構造函數,需要3個參數並賦值。在所有情況下,這對你的情況完全沒有影響,因爲你做了這些變量static。除非您描述常量,否則應避免使用static,在這種情況下,還應該跟隨final關鍵字。
  • 在事件分派線程(EDT)上啓動UI,並對UI執行所有修改。這可以通過使用SwingUtilities.invokeLater()輕鬆完成。
  • 您所看到的錯誤:異常在線程「主」 java.lang.IllegalArgumentException異常:由於您嘗試添加JFrameJComponent,嚴禁將窗口添加到容器被拋出。 JFrame不能添加到任何東西。如果你想這樣做,你需要使用JDesktopPane並添加JInternalFrame(但那是另一回事)。

我也不太清楚,以你想要達到的,但這裏是從你獲得的工作代碼,其效果要好得多:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Polygon; 
import java.awt.Rectangle; 
import java.awt.Shape; 
import java.awt.geom.Ellipse2D; 
import java.io.IOException; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTabbedPane; 
import javax.swing.SwingUtilities; 

/* This program create a graphics component that draws a polygon 
*/ 
public class SelectShape extends JPanel { 

    // Constants 
    private static final int[] x = { 20, 40, 50, 65, 80, 95 }; // Co-ords for a polygon 
    private static final int[] y = { 60, 105, 105, 110, 95, 95 }; 

    private static final Polygon POLYGON = new Polygon(x, y, Math.min(x.length, y.length)); 
    private static final Ellipse2D CIRCLE = new Ellipse2D.Double(100, 40, 45, 45); 

    // Class variables 
    private final Shape shape; 
    private Dimension preferredSize; 

    public SelectShape(Shape shape) { 
     this.shape = shape; 
     Rectangle bounds = shape.getBounds(); 
     this.preferredSize = new Dimension(bounds.x + bounds.width, bounds.y + bounds.height); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return preferredSize; 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     g.setColor(Color.BLUE); 
     g2.draw(shape); 
     g2.fill(shape); 
    } 

    public static void main(String[] args) throws IOException { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame mainFrame = new JFrame("Program"); 
       mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       SelectShape polygon = new SelectShape(POLYGON); 
       SelectShape circle = new SelectShape(CIRCLE); 
       // Create a tabbed pane 
       JTabbedPane tabbedPane = new JTabbedPane(); 
       tabbedPane.addTab("Polygon", polygon); 
       tabbedPane.addTab("Circle", circle); 
       mainFrame.add(tabbedPane); 
       mainFrame.pack(); 
       mainFrame.setVisible(true); 
      } 
     }); 

    } 
} 
+1

所以我假設我必須使用一個明確的框架,並將其擴展到與JFrame相對的JPanel。好的。謝謝!並感謝您向我解釋我的錯誤。 – BDillan 2013-04-22 14:18:20