jpanel

Using paintComponent() to draw rectangle in JFrame

怎甘沉沦 提交于 2019-12-01 18:13:14
问题 I'm trying to create a program that draws shapes (a rectangle on the example below) using JPanel's paintComponent(), but I can't get it to work and can't spot what is wrong. The code is as follows: import javax.swing.*; import java.awt.*; public class RandomRec{ JFrame frame; public void go(){ frame = new JFrame(); frame.setSize(500,500); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DrawPanel panel = new DrawPanel(); } public static void main (String[] args){

How to use MouseListener to find a specific cell in a grid

那年仲夏 提交于 2019-12-01 18:09:16
I'm trying to create a Java game with a 10 x 10 grid made up of cells. The Grid looks liks this: public class Grid extends JPanel implements MouseListener { public static final int GRID_SIZE = 10; public Grid() { setPreferredSize(new Dimension(300, 300)); setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); for (int x = 0; x < GRID_SIZE; x++) for (int y = 0; y < GRID_SIZE; y++) add(new Cell(x, y)); addMouseListener(this); } // All Mouse Listener methods are in here. The Cell class looks like this: public class Cell extends JPanel { public static final int CELL_SIZE = 1; private int xPos; private

Painting over the top of components in Swing?

浪子不回头ぞ 提交于 2019-12-01 17:45:59
I have a JPanel added to a JViewport , and the panel has several other panels added to it. I'm trying to implement a dragging selection, where you can select more than one component by dragging the mouse. The only problem I'm facing is that the selection rectangle is being painted behind the components added to the main JPanel . How can I paint over the top of them? My structure is as follows: JFrame -> ContentPane -> JLayeredPane -> JScrollPane -> JPanel -> JPanel [] . Design draft for college assignment: As you can see, the rectangle is behind the other panels. This is what I'm already doing

How to make JFrame background and JPanel transparent with only image showing

一世执手 提交于 2019-12-01 17:25:46
Hey I am trying to make a some kind of launcher and the "window" must be transparent because I want the image I am using to be the design of it if you understand what I mean. I tried to do setUndecorated(true); and setBackground(new Color(0, 0, 0, 0)); but it just looked weird. Here is a picture on how it looks: http://prntscr.com/2pqohq Here is my code: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridBagLayout; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java

Get location of a swing component

☆樱花仙子☆ 提交于 2019-12-01 17:14:41
问题 I have put some JPanels into another JPanel which its' layout is Box Layout and Y-Axis. After I have put all the panels I need to get the Y position of each added JPanel in from the JPanel container panel. But, when I try to get that I always get zero for Y position of each JPanel. Please tell me how to get the Y position of each Jpanel from the JPanel container JPanel. This is what I have done, JPanel panelHolder = new JPanel(new BoxLayout(this, BoxLayout.Y_AXIS)); for(int i = 0; i< 10; i++)

How to make JFrame background and JPanel transparent with only image showing

妖精的绣舞 提交于 2019-12-01 17:02:29
问题 Hey I am trying to make a some kind of launcher and the "window" must be transparent because I want the image I am using to be the design of it if you understand what I mean. I tried to do setUndecorated(true); and setBackground(new Color(0, 0, 0, 0)); but it just looked weird. Here is a picture on how it looks: http://prntscr.com/2pqohq Here is my code: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridBagLayout; import java.awt.image

Button and textfield don't show up in Java

こ雲淡風輕ζ 提交于 2019-12-01 16:54:35
For school I had to make a JFrame and within that One button and Two textfields. Whatever you put in Textfield one have to get into textfield two when the button is pressed. I got the code to the point I should see the textfields and the button when i run the program. For whatever reason it doesn't. My come so far: package helloworld; import javax.swing.*; import java.awt.event.*; public class HelloWorld extends JFrame { public static void main(String[] args) { JFrame frame = new HelloWorld(); frame.setSize(400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Hello

Invisible components still take up space JPanel

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 16:49:21
问题 I have a series of components underneath each other in a JPanel set as a GridLayout. I need to temporarily hide the components but setVisible(false) doesn't cut it, because there is still an empty gap where the components were. Is there a quick and easy way to do this? Or do I have to stay saving the state of the JPanel, removing the components, then restoring it? SSCCE: [GridLayout2.java] import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt

Communication between two JPanels

烈酒焚心 提交于 2019-12-01 16:28:36
问题 I have this "main" panel (let's call it AAA ) with BorderLayout, and two panels ( BBB and CCC ) in it: public class AAA extends JPanel { BBB pnlNorth = new BBB(); CCC pnlCenter = new CCC(); public AAA(){ setLayout(new BorderLayout()); add(pnlNorth,BorderLayout.NORTH); add(pnlCenter,BorderLayout.CENTER); } } Panel CCC is currently empty, with GridLayout. My panel BBB looks like this: public class BBB extends JPanel { public BBB (){ JLabel labNum = new JLabel("Number of items: "); JTextField

How can I get all the components of a panel in Java Swing?

给你一囗甜甜゛ 提交于 2019-12-01 15:58:29
How can I get all the components of a panel in Java Swing? Is there any method like foreach in C# to deal all the child components of a JPanel? You may use the method getComponents : Component[] components = jpanel.getComponents(); 来源: https://stackoverflow.com/questions/15727661/how-can-i-get-all-the-components-of-a-panel-in-java-swing