简体   繁体   English

卡布局-Java GUI

[英]Card Layout - java GUI

How does CardLayout() in java work? Java中的CardLayout()如何工作? I used the internet and can't seem to get CardLayout to work. 我使用互联网,但似乎无法使CardLayout正常工作。 This is the code I have so far and it is not working: 这是我到目前为止的代码,它不起作用:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GameManager
{
    JFrame frame;
    JPanel cards,title;
    public GameManager()
    {
        cards = new JPanel(new CardLayout());
        title = new JPanel();
        cards.add(title,"title");
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "title");
    }
    public static void main(String [] args)
    {
        GameManager gm = new GameManager();
        gm.run();
    }
    public void run()
    {
        frame = new JFrame("Greek Olympics");
        frame.setSize(1000,1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(cards);
        frame.setVisible(true);
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "title");
    }
    public class title extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.fillRect(100,100,100,100);
        }
    }
}

What should I do so that the panel title shows the rectangle i drew, because it is not showing with the code I have so far. 我应该怎么做才能使面板标题显示我绘制的矩形,因为到目前为止我的代码没有显示它。

When initializing the local variable title you are creating an instance of JPanel , not the title class you defined. 初始化局部变量title您将创建JPanel的实例,而不是您定义的title类。

For clarity purposes, and to comply with the Java naming convention, you should capitalize the class name ( Title ). 为了清楚起见并遵守Java命名约定,应使用大写的类名( Title )。 Then you need to change to type of the title variable to be Title . 然后,您需要将title变量的类型更改为Title

Here is the updated code, where I highlited places changed. 这是更新的代码,我在其中高亮显示了地方。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class GameManager {
    JFrame frame;
    JPanel cards; // <-----
    Title title; // <-----

    public GameManager(){
        cards = new JPanel(new CardLayout());
        title = new Title(); // <-----
        cards.add(title,"title");
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "title");
    }

    public static void main(String [] args){
        GameManager gm = new GameManager();
        gm.run();
    }

    public void run(){
        frame = new JFrame("Greek Olympics");
        frame.setSize(1000,1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(cards);
        frame.setVisible(true);
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, "title");
    }

    public class Title extends JPanel { // <-----
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.fillRect(100,100,100,100);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM