Which is a better top level container for this des

2019-02-26 05:57发布

问题:

I'm a beginner with Swing and AWT, I'm looking to build a GUI using Netbeans. My GUI requires three areas, that I'm currently thinking of as JPanels on a JFrame. I require a "Drawing" panel that will listen for mouse input and draw Java2D shapes. The drawing panel will be anchored to the top left. The other two areas are a "Properties" scroll pane, and a "Controller" scroll pane. The controller pane will determine which drawn elements will be displayed via buttons, and the properties scroll pane will show details about any shape object that is clicked in the drawing pane (endpoints, color, etc..).

A typical use case for the drawing area is such that the first click in the drawing area will be the first set of co-ordinates for a shape, IE: a line. That click will be captured as a point2d object. The second click will be the second point2D object, and a line will be built between the two. The controller area will have buttons like "Circle" "Line" "Elipse", etc. Depending on which controller button is clicked, only shapes of that type will be drawn in the drawing area. The controller and properties scroll panes will not be moveable, they will be locked and resizable to the bottom and right sides of the GUI, respectively.

As a beginner, I'm struggling with the best components to use. I've been reading up on JDesktop and JInternalFrame, but it seems like I don't really need all that. A simple JFrame with three JPanels should suffice, right? More specifically, I think I need a top level JFrame, with a JPanel for the drawing frame and two JScrollPanels. I'll need a mouse listener on the drawing pane to capture the co-ordinates of each click, and listeners for each button in the control pane. Can I put a mouse listener on a JPanel, and draw Java2D shapes to it, or will I need a different (better?) container inside the JPanel to do my drawing?

回答1:

GraphPanel may give you some ideas for constructing such an application.



回答2:

JInternalFrames typically have titlebars, go-away boxes etc. example here which doesn't seem to fit your model. (Unless you want users to be able to move the panels anywhere they want, or make them go away?) So I'd suggest the simpler JFrame with JPanels.



回答3:

Consider two JSplitPanes to separate the drawing area, control area, and properties area. JSplitPane allows the user to resize the areas by clicking and dragging the split line with the mouse.

(Use a JFrame for the top level component.)

As a beginner, if you don't need resizing and scrolling, you will have a much easier time if you make an app that doesn't resize or scroll.