📜  如何在 jframe 中制作字符 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:07.154000             🧑  作者: Mango

代码示例1
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class primary extends JFrame {
    public primary(){

        //Creates Title Image 
        JLabel title = new JLabel(" ");
        ImageIcon tl = new ImageIcon("title.gif");
        title.setIcon(tl);

        //Creates Start Image
        final JButton start = new JButton("");
        ImageIcon st = new ImageIcon("start.gif");
        start.setIcon(st);

        //Creates Options Image
        JButton options = new JButton("");
        ImageIcon opt = new ImageIcon("options.gif");
        options.setIcon(opt);
        options.setBackground(Color.BLACK);

        //Create first frame for "Start" button
        final JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(1, 1));
        p1.add(start, BorderLayout.CENTER);

        //Create second panel for title label
        final JPanel p2 = new JPanel(new BorderLayout());
        p2.setLayout(new GridLayout(1, 3));
        p2.add(title, BorderLayout.WEST);

        //Create third panel for "Options" button
        final JPanel p3 = new JPanel(new BorderLayout());
        p3.setLayout(new GridLayout(1, 1));
        p3.add(options, BorderLayout.SOUTH);

        //Creates fourth panel to organize all other primary
        final JPanel p4 = new JPanel(new BorderLayout());
        p4.setLayout(new GridLayout(1, 3));
        p4.add(p1, BorderLayout.WEST);
        p4.add(p2, BorderLayout.CENTER);
        p4.add(p3, BorderLayout.EAST);


        //When button is clicked, it changes the level
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(start.isEnabled()) {
                    remove(p4);
                    setSize(1440, 500);
                    add(new ContentPanel1());
                    validate();
                }
                else {
                    return;
                }
            }
        });

        //Adds fourth panel to frame
        add(p4, BorderLayout.CENTER);
    }

    public static void main(String arg[]) {
        primary frame = new primary();

        //Finds screen size of monitor
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        //Creates the frame
        frame.setTitle("Cockadoodle Duty: Awakening");
        frame.setSize(screenSize);
        frame.setLocale(null); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        String background = "#000000";
        frame.setBackground(Color.decode(background));
    }
}

class coordinate {
    public static int x;
    public static int y;
}

class ContentPanel1 extends JPanel{
    Image back = Toolkit.getDefaultToolkit().getImage("back.gif");
    Image chick = Toolkit.getDefaultToolkit().getImage("chicken.gif");
    Image corn = Toolkit.getDefaultToolkit().getImage("corn.gif");

    ContentPanel1() {
        MediaTracker mt = new MediaTracker(this);

        mt.addImage(back, 0);
        try {
            mt.waitForAll();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    protected void paintComponent(Graphics g){
        coordinate.x = 20;
        coordinate.y = 321;
        super.paintComponent(g);
        int imwidth = back.getWidth(null);
        int imheight = back.getHeight(null);
        g.drawImage(back, 1, 1, null);
        g.drawImage(chick, coordinate.x, coordinate.y, null);
        g.drawImage(corn, 700, 337, null);
    }
}