我正在尝试添加一个包含八个项目的JList,以便用户可以在此处更改applet的背景颜色。我不确定应该在哪一部分中包含这些添加的详细信息,或者确切地指向何处。我正在使用的书已过时,我想就如何更改此代码以执行后台更改功能获得一些建议或解决方案。
// Java Package Imports
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Ch12_PrExercise1 extends JApplet
{
int number;
public void init()
{
String input;
input = JOptionPane.showInputDialog("Enter a digit");
number = Integer.parseInt(input);
}
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;
}
}
}
这里是您应该如何制作图形的示例。不仅如此。包含List<Color>
以提供颜色。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Ch12_PrExercise1 extends JPanel {
int number;
JFrame frame = new JFrame("Ch12 PrExercise1");
// add more if you want.
List<Color> colors = List.of(Color.red, Color.blue, Color.green);
Color color = Color.red;
public Ch12_PrExercise1() {
setPreferredSize(new Dimension(500, 500));
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities
.invokeLater(() -> new Ch12_PrExercise1().start());
}
// nothing special about start. I just wanted to get out of the static
// context of main.
public void start() {
String input;
input = JOptionPane.showInputDialog("Enter a digit");
number = Integer.parseInt(input);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
// from the list of colors.
g.setColor(colors.get(1));
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;
}
}
}