jpanel

What does .pack() do?

流过昼夜 提交于 2019-11-26 14:41:55
I am learning about JPanel and GridLayout , this snippet of code will produce a simple JPanel with 6 buttons package testing; import java.io.*; import java.util.*; import java.security.*; import javax.xml.bind.DatatypeConverter; import java.lang.*; import java.awt.*; import javax.swing.*; public class Testing { public static class GridPanel extends JPanel { public GridPanel() { setLayout(new GridLayout(2,3)); setBackground(Color.GREEN); this.setPreferredSize(new Dimension(500,500)); JButton b1 = new JButton ("Button 1"); JButton b2 = new JButton ("Button 2"); JButton b3 = new JButton ("Button

JPanel which one of Listeners is proper for visibility is changed

好久不见. 提交于 2019-11-26 14:40:31
Are there some rules, or good/bad experiences with AncestorListener , ComponentListener or HierarchyListener listening for visibility of changes with JPanel and JComponents ? Is one of them better or safer than the others? I would especially like to know about when and how JPanel / JComponent is hidden. Notice the following code contains incorrect Swing rules, like using Thread.sleep(int) , in this case, to allow me to print-out correct order of Listeners in Swing GUI import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.event.ComponentEvent; import java.awt.event

Using addMouseListener() and paintComponent() for JPanel

偶尔善良 提交于 2019-11-26 14:33:01
问题 This is a follow-up to my previous question. I've simplified things as much as I could, and it still doesn't work! Although the good thing I got around using getGraphics() . A detailed explanation on what goes wrong here is massively appreciated. My suspicion is that something's wrong with the the way I used addMouseListener() method here. EDIT completely rewrote the code. Still does not work properly though. import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent;

How can I make image appear randomly every x seconds in java using timer?

限于喜欢 提交于 2019-11-26 13:54:06
I'm working on a game in which I need to 'hit' a mouse/rat, it will disappear and you'll get 1 point. I made it randomly appear everytime i start the app, but I want the image te be drawn randomly every x seconds using Timer() or something. My code for the game screen looks like this: import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing

How to draw in JPanel? (Swing/graphics Java)

前提是你 提交于 2019-11-26 13:06:23
I'm working on a project in which I am trying to make a paint program. So far I've used Netbeans to create a GUI and set up the program. As of right now I am able to call all the coordinated necessary to draw inside it but I am very confused with how to actually paint inside it. Towards the end of my code I have a failed attempt at drawing inside the panel. Can anyone explain/show how to use graphics in a example like this? All examples I have found make a class and extend it with JPanel but I don't know if I can do this since it was generated in netbeans. I need to draw inside a JPanel ,

How to draw grid using swing class Java and detect mouse position when click and drag

喜你入骨 提交于 2019-11-26 13:04:01
问题 I am trying to create a grid UI (5*5) using Swing classes. I tried a nested loop and adding a jPanel dynamically to the jFrame. And I also tried to change the background colour of each jPanel when user clicks and drops over it. But with my code there are huge gaps between each cell and I can\'t get the drag event to work. public class clsCanvasPanel extends JPanel { private static final int intRows = 5; private static final int intCols = 5; private List<JPanel> jpllist = new ArrayList<JPanel>

What is the relation between ContentPane and JPanel?

半腔热情 提交于 2019-11-26 12:56:47
问题 I found one example in which buttons are added to panels (instances of JPanel ) then panels are added to the the containers (instances generated by getContentPane() ) and then containers are, by the construction, included into the JFrame (the windows). I tried two things: I got rid of the containers. In more details, I added buttons to a panel (instance of JPanel ) and then I added the panel to the windows (instance of JFrame ). It worked fine. I got rid of the panels. In more details, I

How to include custom panel with NetBeans GUI Builder?

你离开我真会死。 提交于 2019-11-26 12:46:48
问题 I have written a class that extends JPanel. Is it possible to use this in the NetBeans GUI Builder and have it survive all of the automatic code generation? I have used the customised code option in the GUI builder to instantiate the object as the new class, but it seems like the declaration can\'t be changed from JPanel, so only methods that I have overridden get called, I can\'t call new ones that are not present in JPanel. 回答1: Simply drag the class from the projects tree on to the form in

Absolute Positioning Graphic JPanel Inside JFrame Blocked by Blank Sections

雨燕双飞 提交于 2019-11-26 12:32:52
I'm trying to improve my understanding of Java, particularly Java GUI, by making a puzzle program. Currently the user selects an image, which is cut up into a specified number of pieces. The pieces are drawn randomly to the screen but they seem to be covered by blank portions of other pieces, and not all of them show up, but I can print out all the coordinates. I am using absolute positioning because a LayoutManager didn't seem to work. I briefly tried layeredPanes but they confused me and didn't seem to solve the problem. I would really appreciate some help. Here are the 2 relevant classes:

Adding JPanels from other classes to the cardLayout

孤街浪徒 提交于 2019-11-26 12:32:40
I've got 3 windows in 3 separate classes and I would like to use cardLayout so that when you click the next button, the next window will appear. How do I add JPanels containing different elements to one cardLayout? This is the first window: (the only difference is the background though - but it represents the idea of how I got it actually) public class Window1 extends JPanel implements ActionListener { static CardLayout cardLayout = new CardLayout(); public Window1() { init(); } private void init() { JPanel jp = new JPanel(new BorderLayout()); JPanel jp2 = new Window2(); //JPanel jp3 = new