[英]Why isn't my GUI working, do I need a visible code?
我有一個作業問題,我正在嘗試設置 GUI 並執行以下操作:
Step 1. Create a random list of 52 elements holding 52 numbers
corresponding to 52 cards in the cards folder
Step 2. Create a JFrame
2a. set title of your frame
2b. set layout of your frame to have a GridLayout
Step 3. Create 3 cards
3a. create 3 objects of ImageIcon whose image's location is
pointing to the image in the cards folder
3b. create 3 JLabel object, each one hold an ImageIcon object
created above
Step 4. Add three JLabel into your JFrame
Step 5. Pack and display your frame
我在嘗試使我的 GUI 可見時遇到了問題,而且我在理解如何實現 ImageIcon 時也遇到了問題。 我需要做一個網格布局,但沒有一個顯示出來。
import javax.swing.*;
import java.awt.*;
public class Question_2 {
static String location = "cards/";
public static void main( String[] args ) {
String[] cards;
cards = new String[ 52 ];
JFrame frmMyWindow = new JFrame( "Random Cards" );
frmMyWindow.setSize( 300, 200 );
frmMyWindow.setLocationRelativeTo( null );
frmMyWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frmMyWindow.setVisible( true );
}
}
class frmMyWindow extends JFrame {
JLabel lblName;
JPanel panelMain, panelLeft, panelCenter, panelRight;
private int realityState;
private int commState;
public frmMyWindow( String Cards ) {
super( "Cards" );
realityState = commState = 0;
lblName = new JLabel( "Cards" );
panelMain = new JPanel( new GridLayout( 1, 3, 10, 10 ) );
setLayout( new BorderLayout( 20, 10 ) );
add( lblName, BorderLayout.NORTH );
add( panelMain, BorderLayout.CENTER );
panelLeft = new JPanel( new FlowLayout( FlowLayout.CENTER, 5, 10 ) );
panelCenter = new JPanel( new FlowLayout( FlowLayout.LEFT, 5, 5 ) );
panelRight = new JPanel( new FlowLayout( FlowLayout.CENTER, 5, 10 ) );
panelMain.add( panelLeft );
panelMain.add( panelCenter );
panelMain.add( panelRight );
}
}
我希望代碼顯示在標題中包含單詞 Cards 的位置,然后是帶有從卡片文件中隨機選擇的 3 張卡片的網格布局。
一切都很好,除了您創建的是JFrame
實例而不是您的自定義類。
以下行
JFrame frmMyWindow = new JFrame( "Random Cards" );
應該
JFrame frmMyWindow = new frmMyWindow( "Random Cards" );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.