gpt4 book ai didi

java - 使用内部框架在另一个 GUI 中显示一个 GUI

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:32 31 4
gpt4 key购买 nike

我目前正在开发 GUI。

我选择将我的 GUI 基于 bluej 项目 - Scribble。

当您创建 ScribbleGUI 对象时,DrawDemo 类会创建一个同时自动打开的 Canvas 。我希望 Canvas 窗口不打开,除非在打开 ScribbleGUI 时在 ScribbleGUI 内的框架中打开它。

我尝试添加一个内部框架,其中 Canvas 将与代码一起使用:

  //Create an internal frame to display the canvas within the ScribbleGUI.
JInternalFrame internalCanvasFrame = new JInternalFrame("", true,
true, true, true);

我不知道怎么说我想要 Canvas 去那里。

您认为我选择了适当的方法来让两个 GUI 相互连接吗?或者更改 Canvas 类的代码以使其具有与我在 ScribbleGUI 类中操作相同的功能会更好吗?

谢谢

ScribbleGUI 类:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JOptionPane;
/**
* Write a description of class ScribbleGUI here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ScribbleGUI extends DrawDemo
{
private JFrame frame;
private JPanel panel;
private JButton buttonScribble;
private JButton buttonSquare;
private JButton buttonWheel;
private JLabel label;

public ScribbleGUI()
{
GUI();
}

public void GUI()
{

//Set the frame size, visibility
frame = new JFrame("Ugly Sketch");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create an internal frame to display the canvas within the ScribbleGUI.
JInternalFrame internalCanvasFrame = new JInternalFrame("", true,
true, true, true);

internalCanvasFrame.setSize(400, 300);
internalCanvasFrame.setLocation(50, 50);
internalCanvasFrame.setVisible(true);



//Set the backround
panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.WHITE);

//Create buttons
buttonScribble = new JButton("Scribble!");
buttonSquare = new JButton("Square!");
buttonWheel = new JButton("Wheel!");
label = new JLabel("Ugly Sketch");

//Add actions to button
buttonScribble.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eventScribble){
colorScribble();
}

});
buttonSquare.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eventSquare){
drawSquare();
}

});
buttonWheel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eventWheel){
drawWheel();
}

});
//Sreate space between button objects
GridBagConstraints constraint = new GridBagConstraints();

constraint.insets = new Insets(10,10,10,10);
constraint.gridx = 0;
constraint.gridy = 1;
panel.add(label,constraint);
constraint.gridx = 0;
constraint.gridy = 2;
panel.add(buttonScribble,constraint);
constraint.gridx = 0;
constraint.gridy = 3;
panel.add(buttonSquare,constraint);
constraint.gridx = 0;
constraint.gridy = 4;
panel.add(buttonWheel,constraint);




//Set the panel to sit at the west of the frame
frame.add(panel,BorderLayout.WEST);



}

}

<小时/>

DrawDemo类:

import java.awt.Color;
import java.util.Random;

/**
* Class DrawDemo - provides some short demonstrations showing how to use the
* Pen class to create various drawings.
*
* @author Michael Kölling and David J. Barnes
* @version 2011.07.31
*/

public class DrawDemo
{
private Canvas myCanvas;
private Random random;

/**
* Prepare the drawing demo. Create a fresh canvas and make it visible.
*/
public DrawDemo()
{
myCanvas = new Canvas("Drawing Demo", 500, 400);
random = new Random();
}

/**
* Draw a square on the screen.
*/
public void drawSquare()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.BLUE);

square(pen);
}

/**
* Draw a wheel made of many squares.
*/
public void drawWheel()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.RED);

for (int i=0; i<36; i++) {
square(pen);
pen.turn(10);
}
}

/**
* Draw a square in the pen's color at the pen's location.
*/
private void square(Pen pen)
{
for (int i=0; i<4; i++) {
pen.move(100);
pen.turn(90);
}
}

/**
* Draw some random squiggles on the screen, in random colors.
*/
public void colorScribble()
{
Pen pen = new Pen(250, 200, myCanvas);

for (int i=0; i<10; i++) {
// pick a random color
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
pen.setColor(new Color(red, green, blue));

pen.randomSquiggle();
}
}

/**
* Show canvas
*/
public Canvas showCanvas()
{
myCanvas = new Canvas("Drawing Demo", 500, 400);
return myCanvas;
}
/**
* Clear the screen.
*/
public void clear()
{
myCanvas.erase();
}
}

Canvas 类:

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

/**
* Class Canvas - a class to allow for simple graphical
* drawing on a canvas.
*
* @author Michael Kölling (mik)
* @author Bruce Quig
*
* @version 2011.07.31
*/

public class Canvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;

/**
* Create a Canvas with default height, width and background color
* (300, 300, white).
* @param title title to appear in Canvas Frame
*/
public Canvas(String title)
{
this(title, 300, 300, Color.white);
}

/**
* Create a Canvas with default background color (white).
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
*/
public Canvas(String title, int width, int height)
{
this(title, width, height, Color.white);
}

/**
* Create a Canvas.
* @param title title to appear in Canvas Frame
* @param width the desired width for the canvas
* @param height the desired height for the canvas
* @param bgClour the desired background color of the canvas
*/
public Canvas(String title, int width, int height, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
frame.pack();
setVisible(true);
}

/**
* Set the canvas visibility and brings canvas to the front of screen
* when made visible. This method can also be used to bring an already
* visible canvas to the front of other windows.
* @param visible boolean value representing the desired visibility of
* the canvas (true or false)
*/
public void setVisible(boolean visible)
{
if(graphic == null) {
// first time: instantiate the offscreen image and fill it with
// the background color
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width, size.height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(true);
}

/**
* Provide information on visibility of the Canvas.
* @return true if canvas is visible, false otherwise
*/
public boolean isVisible()
{
return frame.isVisible();
}

/**
* Draw the outline of a given shape onto the canvas.
* @param shape the shape object to be drawn on the canvas
*/
public void draw(Shape shape)
{
graphic.draw(shape);
canvas.repaint();
}

/**
* Fill the internal dimensions of a given shape with the current
* foreground color of the canvas.
* @param shape the shape object to be filled
*/
public void fill(Shape shape)
{
graphic.fill(shape);
canvas.repaint();
}

/**
* Fill the internal dimensions of the given circle with the current
* foreground color of the canvas.
* @param xPos The x-coordinate of the circle center point
* @param yPos The y-coordinate of the circle center point
* @param diameter The diameter of the circle to be drawn
*/
public void fillCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
fill(circle);
}

/**
* Fill the internal dimensions of the given rectangle with the current
* foreground color of the canvas. This is a convenience method. A similar
* effect can be achieved with the "fill" method.
*/
public void fillRectangle(int xPos, int yPos, int width, int height)
{
fill(new Rectangle(xPos, yPos, width, height));
}

/**
* Erase the whole canvas.
*/
public void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
Dimension size = canvas.getSize();
graphic.fill(new Rectangle(0, 0, size.width, size.height));
graphic.setColor(original);
canvas.repaint();
}

/**
* Erase the internal dimensions of the given circle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseCircle(int xPos, int yPos, int diameter)
{
Ellipse2D.Double circle = new Ellipse2D.Double(xPos, yPos, diameter, diameter);
erase(circle);
}

/**
* Erase the internal dimensions of the given rectangle. This is a
* convenience method. A similar effect can be achieved with
* the "erase" method.
*/
public void eraseRectangle(int xPos, int yPos, int width, int height)
{
erase(new Rectangle(xPos, yPos, width, height));
}

/**
* Erase a given shape's interior on the screen.
* @param shape the shape object to be erased
*/
public void erase(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.fill(shape); // erase by filling background color
graphic.setColor(original);
canvas.repaint();
}

/**
* Erases a given shape's outline on the screen.
* @param shape the shape object to be erased
*/
public void eraseOutline(Shape shape)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.draw(shape); // erase by drawing background color
graphic.setColor(original);
canvas.repaint();
}

/**
* Draws an image onto the canvas.
* @param image the Image object to be displayed
* @param x x co-ordinate for Image placement
* @param y y co-ordinate for Image placement
* @return returns boolean value representing whether the image was
* completely loaded
*/
public boolean drawImage(Image image, int x, int y)
{
boolean result = graphic.drawImage(image, x, y, null);
canvas.repaint();
return result;
}

/**
* Draws a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void drawString(String text, int x, int y)
{
graphic.drawString(text, x, y);
canvas.repaint();
}

/**
* Erases a String on the Canvas.
* @param text the String to be displayed
* @param x x co-ordinate for text placement
* @param y y co-ordinate for text placement
*/
public void eraseString(String text, int x, int y)
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
graphic.drawString(text, x, y);
graphic.setColor(original);
canvas.repaint();
}

/**
* Draws a line on the Canvas.
* @param x1 x co-ordinate of start of line
* @param y1 y co-ordinate of start of line
* @param x2 x co-ordinate of end of line
* @param y2 y co-ordinate of end of line
*/
public void drawLine(int x1, int y1, int x2, int y2)
{
graphic.drawLine(x1, y1, x2, y2);
canvas.repaint();
}

/**
* Sets the foreground color of the Canvas.
* @param newColor the new color for the foreground of the Canvas
*/
public void setForegroundColor(Color newColor)
{
graphic.setColor(newColor);
}

/**
* Returns the current color of the foreground.
* @return the color of the foreground of the Canvas
*/
public Color getForegroundColor()
{
return graphic.getColor();
}

/**
* Sets the background color of the Canvas.
* @param newColor the new color for the background of the Canvas
*/
public void setBackgroundColor(Color newColor)
{
backgroundColor = newColor;
graphic.setBackground(newColor);
}

/**
* Returns the current color of the background
* @return the color of the background of the Canvas
*/
public Color getBackgroundColor()
{
return backgroundColor;
}

/**
* changes the current Font used on the Canvas
* @param newFont new font to be used for String output
*/
public void setFont(Font newFont)
{
graphic.setFont(newFont);
}

/**
* Returns the current font of the canvas.
* @return the font currently in use
**/
public Font getFont()
{
return graphic.getFont();
}

/**
* Sets the size of the canvas.
* @param width new width
* @param height new height
*/
public void setSize(int width, int height)
{
canvas.setPreferredSize(new Dimension(width, height));
Image oldImage = canvasImage;
canvasImage = canvas.createImage(width, height);
graphic = (Graphics2D)canvasImage.getGraphics();
graphic.setColor(backgroundColor);
graphic.fillRect(0, 0, width, height);
graphic.drawImage(oldImage, 0, 0, null);
frame.pack();
}

/**
* Returns the size of the canvas.
* @return The current dimension of the canvas
*/
public Dimension getSize()
{
return canvas.getSize();
}

/**
* Waits for a specified number of milliseconds before finishing.
* This provides an easy way to specify a small delay which can be
* used when producing animations.
* @param milliseconds the number
*/
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e)
{
// ignoring exception at the moment
}
}

/************************************************************************
* Inner class CanvasPane - the actual canvas component contained in the
* Canvas frame. This is essentially a JPanel with added capability to
* refresh the image drawn on it.
*/
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
}

最佳答案

在 Java GUI 中,您需要容器,您可以在其中添加任何组件。由于 Frame 本身是容器和组件(请参阅 API ),您可以将其添加到另一个容器,从而添加到另一个 Frame 或 Panel 或任何其他容器。

唯一的困惑可能是如果您添加相同的组件两次。对于相同或不同的容器都没有关系。同一个对象意味着两个容器中的一个对象 - 因为两个容器都会尝试使用不同的事件来操作/修改/通知同一个对象,这使得它不稳定。

只要每个组件都是唯一的,您就可以按任何顺序将它们组合在任何容器中。再次强调,容器也是组件,因此也使其具有唯一性。

关于java - 使用内部框架在另一个 GUI 中显示一个 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29977010/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com