java - 如何修改此程序以包含可用于更改此小程序的背景颜色的八种颜色的 JList?

标签 java swing applet

我正在尝试添加一个包含八个项目的 JList,以便用户可以在此处更改小程序的背景颜色。我不确定应该在哪一部分包含这些添加的详细信息,或者确切地转到哪里。我正在使用的书已经过时了,我只是想获得一些关于如何更改此代码以执行后台更改功能的建议或解决方案。

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

public class Ch12_PrExercise1 extends JApplet {

    int number;

    @Override
    public void init() {
        String input;

        input = JOptionPane.showInputDialog("Enter a digit");

        number = Integer.parseInt(input);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        switch (number) {
            case 0:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 175);
                g.fillRect(50, 200, 125, 25);
                g.fillRect(150, 25, 25, 175);
                break;

            case 1:
                g.fillRect(75, 25, 75, 25);
                g.fillRect(100, 50, 50, 125);
                g.fillRect(50, 175, 150, 25);
                break;

            case 2:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(150, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(50, 125, 25, 50);
                break;

            case 3:
                g.fillRect(150, 50, 25, 175);
                g.fillRect(50, 50, 100, 25);
                g.fillRect(50, 125, 100, 25);
                g.fillRect(50, 200, 100, 25);
                break;

            case 4:
                g.fillRect(50, 25, 25, 75);
                g.fillRect(50, 100, 100, 25);
                g.fillRect(150, 25, 25, 175);
                break;

            case 5:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                break;

            case 6:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(50, 125, 25, 50);
                break;

            case 7:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(150, 50, 25, 150);
                break;

            case 8:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(50, 125, 25, 50);
                g.fillRect(150, 50, 25, 50);
                break;

            case 9:
            default:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(150, 50, 25, 50);
                break;
        }
    }
}

最佳答案

这是一个经过一些调整的实现:

  1. 它绘制到面板而不是顶级容器(例如小程序)作为自定义绘制的组件。
  2. 它使用框架而不是小程序来显示面板。
  3. 它定义了一个NamedColor来封装名称和颜色。这对于列表来说很方便,因为这意味着它可以包含命名颜色,而不是使用字符串或索引来映射到颜色。
  4. 自定义绘制组件 (ColoredNumberPanel) 定义首选尺寸,因此可以打包顶层容器以使其正确放置。

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;

public class Ch12_PrExercise1_Panel {

    private JComponent ui = null;
    private final NamedColor[] namedColors = {
        new NamedColor("CYAN", Color.CYAN),
        new NamedColor("PINK", Color.PINK),
        new NamedColor("GREEN", Color.GREEN),
        new NamedColor("MAGENTA", Color.MAGENTA),
        new NamedColor("ORANGE", Color.ORANGE),
        new NamedColor("RED", Color.RED),
        new NamedColor("WHITE", Color.WHITE),
        new NamedColor("YELLOW", Color.YELLOW)
    };

    Ch12_PrExercise1_Panel() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }
        SpinnerNumberModel numberModel = new SpinnerNumberModel(0, 0, 9, 1);
        JSpinner spinner = new JSpinner(numberModel);
        JOptionPane.showMessageDialog(null, spinner, "Which Number?", JOptionPane.QUESTION_MESSAGE);

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        DefaultListModel listModel = new DefaultListModel();
        for (NamedColor namedColor : namedColors) {
            listModel.addElement(namedColor);
        }
        final JList list = new JList(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(8);
        ui.add(new JScrollPane(list), BorderLayout.LINE_START);

        final ColoredNumberPanel coloredNumberPanel = 
                new ColoredNumberPanel(numberModel.getNumber().intValue());
        ui.add(coloredNumberPanel);

        ListSelectionListener listSelectionListener = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    NamedColor namedColor = (NamedColor)list.getSelectedValue();
                    Color color = namedColor.color;
                    coloredNumberPanel.setBackground(color);
                }
            }
        };
        list.addListSelectionListener(listSelectionListener);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            Ch12_PrExercise1_Panel o = new Ch12_PrExercise1_Panel();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

class ColoredNumberPanel extends JPanel {

    int number;
    Dimension preferredSize = new Dimension(200, 250);

    ColoredNumberPanel(int number) {
        this.number = number;
    }

    /**
     * Note: The correct method to do custom painting in a JComponent (like
     * JPanel) is paintComponent(..) rather than paint(..) as used for an
     * applet/frame/window..
     */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // this also needs to be changed

        switch (number) {
            case 0:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 175);
                g.fillRect(50, 200, 125, 25);
                g.fillRect(150, 25, 25, 175);
                break;

            case 1:
                g.fillRect(75, 25, 75, 25);
                g.fillRect(100, 50, 50, 125);
                g.fillRect(50, 175, 150, 25);
                break;

            case 2:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(150, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(50, 125, 25, 50);
                break;

            case 3:
                g.fillRect(150, 50, 25, 175);
                g.fillRect(50, 50, 100, 25);
                g.fillRect(50, 125, 100, 25);
                g.fillRect(50, 200, 100, 25);
                break;

            case 4:
                g.fillRect(50, 25, 25, 75);
                g.fillRect(50, 100, 100, 25);
                g.fillRect(150, 25, 25, 175);
                break;

            case 5:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                break;

            case 6:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(50, 125, 25, 50);
                break;

            case 7:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(150, 50, 25, 150);
                break;

            case 8:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(50, 125, 25, 50);
                g.fillRect(150, 50, 25, 50);
                break;

            case 9:
            default:
                g.fillRect(50, 25, 125, 25);
                g.fillRect(50, 50, 25, 50);
                g.fillRect(50, 100, 125, 25);
                g.fillRect(50, 175, 125, 25);
                g.fillRect(150, 125, 25, 50);
                g.fillRect(150, 50, 25, 50);
                break;
        }
    }

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

class NamedColor {

    String name;
    Color color;

    public NamedColor(String name, Color color) {
        this.name = name;
        this.color = color;
    }

    @Override 
    public String toString() {
        return name;
    }
}

一般提示

  1. 为什么要编写小程序代码?如果是老师指定的,请引用Why CS teachers should stop teaching Java applets
  2. 对于与互联网上的“所有人”打交道的网站来说,小程序已经变得完全无法使用。 Safari 和 FF 默认情况下都会阻止小程序,而 Chrome 和 IE 则设置为完全删除对它们的支持。最好尽早从 applet 过渡到纯 JS/HTML。
  3. 使用逻辑一致的缩进代码行和 block 形式。缩进的目的是使代码流程更容易理解!大多数 IDE 都有专门用于格式化代码的键盘快捷键。

关于java - 如何修改此程序以包含可用于更改此小程序的背景颜色的八种颜色的 JList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61180445/

相关文章:

java - 如何在透明 JPanel 上添加 JComboBox?

java - 迁移到 Java 8 时的 Swing 布局问题

java - 由于 chrome 删除了 NPAPI 支持,是否有其他方法在 chrome 中显示 NPAPI?

java - ideone 上线程 "main"java.util.NoSuchElementException 中的异常

java - 添加光照 glLightfv 方法不起作用

java - Android 应用程序不断崩溃,致命异常

Java ImageIcon/Icon 和 JLabel 不工作

java - 小程序 - init()、EDT 和线程

Java 小程序在 Web 浏览器中运行

java - 在用户输入java上暂停线程池