Java 面板显示在小程序上,但不显示在应用程序模式上
我正在努力完成交给我的家庭作业,我被要求创建一个绘制矩形和圆形的“画师”,能够使用减号按钮等删除它们。。 最后一项任务是使应用程序能够同时作为小程序和应用程序运行。。 我试着按照老师的指示,让一个应用程序同时作为小程序和应用程序运行,现在面板只在小程序模式下显示在框架上Java 面板显示在小程序上,但不显示在应用程序模式上,java,applet,jframe,jpanel,japplet,Java,Applet,Jframe,Jpanel,Japplet,我正在努力完成交给我的家庭作业,我被要求创建一个绘制矩形和圆形的“画师”,能够使用减号按钮等删除它们。。 最后一项任务是使应用程序能够同时作为小程序和应用程序运行。。 我试着按照老师的指示,让一个应用程序同时作为小程序和应用程序运行,现在面板只在小程序模式下显示在框架上 import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; import javax.swing.b
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
public class HW4 extends JApplet {
private final String[] EraseComboBoxList = { "None", "All", "Rect",
"Circle" };
private final Dimension EraseShapeColorPanelDim = new Dimension(130, 65);
private final Dimension ControlPanelDim = new Dimension(200, 600);
private final int PanelsBorderThickness = 3;
private final int ControlPanelHGap = 50;
private final int ControlPanelVGap = 20;
private final static int FrameHGap = 10;
private final static int FrameVGap = 0;
private final static Dimension FramePanelDim = new Dimension(800, 600);
private JTextArea drawnShapes = new JTextArea(11, 12);
private ArrayList<Shape> shapeList = new ArrayList<Shape>();
private static Shape tempShape;
private boolean draw = false;
ControlPanel controlPanel = new ControlPanel();
PainterPanel paintPanel = new PainterPanel();
public HW4() {
tempShape = new Shape();
setBackground(Color.LIGHT_GRAY);
add(paintPanel, BorderLayout.CENTER);
setANDrequestFocus();
add(controlPanel, BorderLayout.EAST);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new HW4());
frame.setSize(FramePanelDim);
frame.setTitle("My Painter");
frame.setLayout(new BorderLayout(FrameHGap, FrameVGap));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
private class PainterPanel extends JPanel {
private PainterPanel() {
setBorder(new LineBorder(Color.GRAY, PanelsBorderThickness));
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent mouseRelease) {
if ((Math.abs(tempShape.startX - tempShape.width) + Math
.abs(tempShape.startY - tempShape.height)) != 0) {
Shape shape = new Shape();
shape.color = tempShape.color;
shape.shape = tempShape.shape;
shape.filled = tempShape.filled;
shape.startX = tempShape.startX;
shape.startY = tempShape.startY;
shape.width = tempShape.width;
shape.height = tempShape.height;
shapeList.add(shape);
appendToTextArea(shape);
}
draw = false;
}
@Override
public void mousePressed(MouseEvent mousePress) {
tempShape.startX = mousePress.getX();
tempShape.startY = mousePress.getY();
tempShape.width = mousePress.getX();
tempShape.height = mousePress.getY();
draw = true;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent mouseDrag) {
tempShape.width = mouseDrag.getX();
tempShape.height = mouseDrag.getY();
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int startX, startY, width, height;
setANDrequestFocus();
for (int i = 0 ; i < shapeList.size() ; i++) {
startX = Math.min(shapeList.get(i).startX, shapeList.get(i).width);
startY = Math.min(shapeList.get(i).startY, shapeList.get(i).height);
width = Math.abs((shapeList.get(i).startX - shapeList.get(i).width));
height = Math.abs((shapeList.get(i).startY - shapeList.get(i).height));
g2d.setColor(shapeList.get(i).color);
g2d.setStroke(new BasicStroke(3));
if ((width != 0) && (height != 0)) {
if (shapeList.get(i).shape.equals("Rect")) {
if (shapeList.get(i).filled) {
g2d.fillRect(startX, startY, width, height);
}
else {
g2d.drawRect(startX, startY, width, height);
}
}
else {
if (shapeList.get(i).filled) {
g2d.fillOval(startX, startY, width, height);
}
else {
g2d.drawOval(startX, startY, width, height);
}
}
}
}
if (draw) {
startX = Math.min(tempShape.startX, tempShape.width);
startY = Math.min(tempShape.startY, tempShape.height);
width = Math.abs(tempShape.startX - tempShape.width);
height = Math.abs(tempShape.startY - tempShape.height);
g2d.setColor(tempShape.color);
g2d.setStroke(new BasicStroke(3));
if ((width != 0) && (height != 0)) {
if (tempShape.shape.equals("Rect")) {
if (tempShape.filled) {
g2d.fillRect(startX, startY, width, height);
}
else {
g2d.drawRect(startX, startY, width, height);
}
}
else {
if (tempShape.filled) {
g2d.fillOval(startX, startY, width, height);
}
else {
g2d.drawOval(startX, startY, width, height);
}
}
}
}
}
}
private class ControlPanel extends JPanel implements ActionListener {
private JComboBox<String> eraseComboBox = new JComboBox<String>(EraseComboBoxList);
private JPanel erasePanel;
private JScrollPane scrollPane = new JScrollPane(drawnShapes,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
private JPanel shapePanel;
private JRadioButton shapeRect = new JRadioButton("Rect", true);
private JRadioButton shapeCircle = new JRadioButton("Circle");
private JPanel colorPanel;
private JRadioButton colorRed = new JRadioButton("Red");
private JRadioButton colorBlue = new JRadioButton("Blue", true);
private JCheckBox fillCheckBox = new JCheckBox("Fill");
private ControlPanel() {
createControlPanel();
}
public void createControlPanel() {
setLayout(new FlowLayout(FlowLayout.CENTER, ControlPanelHGap,
ControlPanelVGap));
setPreferredSize(ControlPanelDim);
createErasePanel();
createShapePanel();
createColorPanel();
createFillCheckBox();
addKeyListeners();
drawnShapes.setEditable(false);
add(erasePanel);
add(shapePanel);
add(colorPanel);
add(fillCheckBox);
add(scrollPane);
setBorder(new LineBorder(Color.GRAY, PanelsBorderThickness));
}
public void createErasePanel() {
erasePanel = new JPanel();
erasePanel.setBorder(new TitledBorder("Erase"));
eraseComboBox.setToolTipText("Please select which type of shapes you would like to remove");
eraseComboBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (eraseComboBox.getSelectedItem().equals("Rect")) {
for (int i = 0; i < shapeList.size(); i++) {
if ((shapeList.get(i).shape).equals("Rect")) {
shapeList.remove(i);
--i;
}
}
removeReWriteTextArea();
paintPanel.repaint();
}
if (eraseComboBox.getSelectedItem().equals("Circle")) {
for (int i = 0; i < shapeList.size(); i++) {
if ((shapeList.get(i).shape).equals("Circle")) {
shapeList.remove(i);
--i;
}
}
removeReWriteTextArea();
paintPanel.repaint();
}
if (eraseComboBox.getSelectedItem().equals("All")) {
for (int i = 0; i < shapeList.size(); i++) {
shapeList.remove(i);
--i;
}
removeReWriteTextArea();
paintPanel.repaint();
}
}
});
erasePanel.add(eraseComboBox);
erasePanel.setPreferredSize(EraseShapeColorPanelDim);
}
public void createShapePanel() {
shapePanel = new JPanel();
shapePanel.setBorder(new TitledBorder("Shape"));
ButtonGroup shapeGroup = new ButtonGroup();
shapeGroup.add(shapeRect);
shapeGroup.add(shapeCircle);
shapeRect.setMnemonic('R');
shapeCircle.setMnemonic('C');
shapeRect.addActionListener(this);
shapeCircle.addActionListener(this);
shapePanel.add(shapeRect);
shapePanel.add(shapeCircle);
shapePanel.setPreferredSize(EraseShapeColorPanelDim);
}
public void createColorPanel() {
colorPanel = new JPanel();
colorPanel.setBorder(new TitledBorder("Color"));
ButtonGroup colorGroup = new ButtonGroup();
colorGroup.add(colorRed);
colorGroup.add(colorBlue);
colorRed.setMnemonic('e');
colorBlue.setMnemonic('B');
colorBlue.addActionListener(this);
colorRed.addActionListener(this);
colorPanel.add(colorRed);
colorPanel.add(colorBlue);
colorPanel.setPreferredSize(EraseShapeColorPanelDim);
}
public void createFillCheckBox() {
fillCheckBox.setMnemonic('F');
fillCheckBox.addActionListener(this);
}
public void addKeyListeners() {
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_MINUS) {
if (shapeList.size() > 0) {
shapeList.remove(shapeList.size()-1);
shapeList.trimToSize();
removeReWriteTextArea();
paintPanel.repaint();
}
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
tempShape.shape = "Rect";
if (shapeCircle.isSelected()) {
tempShape.shape = "Circle";
}
tempShape.color = Color.BLUE;
if (colorRed.isSelected()) {
tempShape.color = Color.RED;
}
tempShape.filled = false;
if (fillCheckBox.isSelected()) {
tempShape.filled = true;
}
}
}
private class Shape {
private boolean filled = false;
private Color color = Color.BLUE;
private String shape = "Rect";
private int startX, startY, width, height;
public String getColorString() {
if (color == Color.RED) {
return "Red";
}
else {
return "Blue";
}
}
}
private void setANDrequestFocus() {
controlPanel.setFocusable(true);
controlPanel.requestFocusInWindow();
}
public void removeReWriteTextArea() {
drawnShapes.setText(null);
for(int i = 0; i < shapeList.size(); i++) {
appendToTextArea(shapeList.get(i));
}
}
public void appendToTextArea(Shape shape) {
String append = shape.shape + ", " + shape.getColorString() + ", " + "fill = " + shape.filled;
drawnShapes.append(append + "\n");
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.ArrayList;
导入javax.swing.*;
导入javax.swing.border.*;
公共类HW4扩展了JApplet{
私有最终字符串[]ReaseComboxList={“无”、“全部”、“Rect”,
“圆”};
专用最终尺寸橡皮擦ShapeColorPanelDim=新尺寸(130,65);
专用最终尺寸控制面板尺寸=新尺寸(200600);
专用最终内板订单厚度=3;
私人最终内部控制面板hgap=50;
私人最终内部控制面板Vgap=20;
私有最终静态int FrameHGap=10;
私有最终静态int FrameVGap=0;
专用最终静态尺寸FramePanelDim=新尺寸(800600);
私人JTextArea drawnShapes=新JTextArea(11,12);
私有ArrayList shapeList=新ArrayList();
私有静态形状;
私有布尔绘制=false;
ControlPanel ControlPanel=新的ControlPanel();
PainterPanel paintPanel=新的PainterPanel();
公共卫生服务4({
tempShape=新形状();
立根背景(颜色:浅灰色);
添加(paintPanel、BorderLayout.CENTER);
setANDrequestFocus();
添加(控制面板,BorderLayout.EAST);
}
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.add(新的HW4());
frame.setSize(FramePanelDim);
frame.setTitle(“我的画家”);
frame.setLayout(新的BorderLayout(FrameHGap,FrameVGap));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(空);
frame.setresizeable(false);
frame.setAlwaysOnTop(真);
frame.setVisible(true);
}
私有类PainterPanel扩展了JPanel{
私人画板(){
setBorder(新线条边框(颜色为灰色,拼板顺序厚度));
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseRelease(MouseEvent mouseRelease){
if((Math.abs(tempShape.startX-tempShape.width)+Math
.abs(tempShape.startY-tempShape.height))!=0){
形状=新形状();
shape.color=tempShape.color;
shape.shape=tempShape.shape;
shape.filled=tempShape.filled;
shape.startX=tempShape.startX;
shape.startY=tempShape.startY;
shape.width=tempShape.width;
shape.height=tempShape.height;
添加(形状);
附加区域(形状);
}
绘制=假;
}
@凌驾
public void mousePress(MouseEvent mousePress){
tempShape.startX=mousePress.getX();
tempShape.startY=mousePress.getY();
tempShape.width=mousePress.getX();
tempShape.height=mousePress.getY();
绘制=真;
}
});
addMouseMotionListener(新的MouseMotionAdapter(){
@凌驾
公共无效鼠标标记(MouseEvent mouseDrag){
tempShape.width=mouseDrag.getX();
tempShape.height=mouseDrag.getY();
重新油漆();
}
});
}
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
int startX、startY、宽度、高度;
setANDrequestFocus();
对于(int i=0;ipublic static void main(String[] args) {
JFrame frame = new JFrame();
JApplet hw4 = new HW4();
hw4.init();
hw4.start();
frame.add(hw4);
frame.setSize(FramePanelDim);
frame.setTitle("My Painter");
//frame.setLayout(new BorderLayout(FrameHGap, FrameVGap));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//frame.setResizable(false);
//frame.setAlwaysOnTop(true);
frame.setVisible(true);
frame.pack();
}