簡體   English   中英

java程序,該程序允許用戶使用循環輸入許多面板的行和列,但無法找出錯誤

[英]java program that allows the user to enter a number of columns and rows of panels with a loop but can't figure out the error

該代碼已顯示在我的教科書中,但是以下代碼在編譯時似乎有錯誤。

Color backColor = Color(red,green,blue);
ColorPanel panel = new ColorPanel(backColor);
pane.add(panel);

這是整個代碼:

import javax.swing.*; 

import java.awt.*; 
import java.util.Random; 
import javax.swing.JOptionPane; 

public class GUIWindow { 
    public static void main(String [] args){ 
        JFrame theGUI = new JFrame(); 
        theGUI.setTitle("GUI Example"); 
        String inputStr = JOptionPane.showInputDialog("Number of rows", "5"); 
        if( inputStr ==null ) return; 
        int rows = Integer.parseInt(inputStr); 
        String inputStr1 = JOptionPane.showInputDialog("Number of columns", "5"); 
        if(inputStr1 == null)return; 
        int cols = Integer.parseInt(inputStr1); 
        theGUI.setSize(cols*5, rows*5); 
        theGUI.setDefaultCloseOperation(JFram... 
        Container pane = theGUI.getContentPane(); 
        pane.setLayout(new GridLayout(rows,cols)); 
        Random gen = new Random(); 
        for(int i =1; i<= rows*cols; i++){ 
            int red = gen.nextInt(256); 
            int green = gen.nextInt(256);   
            int blue = gen.nextInt(256); 
            Color backColor = new Color(red,green,blue); 
            ColorPanel panel = new ColorPanel(backColor); 
            pane.add (panel); 
        } 
        theGUI.setVisible(true); 
    }
}

您沒有導入不是標准swing類的ColorPanel ,必須添加正確的導入,例如( com.some.package名稱更正 ):

import com.some.package.ColorPanel; 

我發現的只有與搖擺相關的參考是bycom.esri.arcgis.beans.ui.ColorPanel ,該類擴展了javax.swing.JPanel但不確定是否需要或是否創建了自己的。

只要您的評論說:

@FastSnail:您是否創建了一個名為“ ColorPanel”的類?
@Ayah Al-Harthy:不,這不是在教科書上

並查看代碼,您可以將JPanelsetBackground(Color)結合使用來做同樣的事情(我想)。 使用Color(int,int,int)構造函數。

Color backColor = new Color(red,green,blue);
JPanel panel = new JPanel();
panel.setBackground(backColor);
// set some dimensions if necessary before adding
panel.add(panel);

另請注意,此行不完整:

theGUI.setDefaultCloseOperation(JFram... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM