jpanel

Using a JPanel extension class to set a background

前提是你 提交于 2019-12-08 04:16:28
问题 In my Main I want to create a JFrame , then I want to create a BackgroundPanel and I want to add this one in the JFrame . This is Main class: public class Main { public static void main(String args[]) { Frame frame = new Frame(); BackgroundPanel back = new BackgroundPanel(); frame.getContentPane().add(back); frame.setSize(400, 287); frame.setVisible(true); } } This is BackgroundPanel class: import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.Toolkit;

Java - JPanel with background image AND normal functionality

痞子三分冷 提交于 2019-12-08 03:43:38
问题 I've been looking around and working on making a JPanel able to have an image background. I know there is a ton of information reguarding this ( example 1 and example 2 ). Neither of these are exactly what I'm looking for. What I am looking for, is a way to just add a function or the functionality to set a background image on a JPanel while maintaining everything else. The problem that I have right now is that I have a few classes that extend JPanel and when I change them to extend my new

Parent JPanel - How to listen to the events generated by components of a Child JPanel? [duplicate]

梦想的初衷 提交于 2019-12-08 03:19:05
问题 This question already has answers here : Adding ActionListener to a Panel - Panel implements ActionListener vs Panel HAS A ActionListener (2 answers) Closed 6 years ago . I made a JPanel Child which contains a couple of radio buttons in it. Whenever a radio button is clicked i want an ActionEvent to be generated from the Child also. This action event should "contain" a reference to the button which actually generated an event. This Child will be used as a component inside another JPanel

Resize the panel without revalidation

我的未来我决定 提交于 2019-12-08 03:08:17
问题 I have a JPanel in which I draw lines to create an illusion of pencil. This panel is in a ScrollPane . When I resize the panel one call to revalidate() method is automatically placed and all my drawn lines in this panel are gone. Is there any way to keep my drawn line in the panel with the new size ? import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event

Update LookAndFeel Values On The Fly

孤者浪人 提交于 2019-12-08 03:02:23
问题 I want to be able to update the LookAndFeel attributes of my Swing GUI on the fly. In this case, I have a simple Swing/Awt game running what starts out as the Nimbus LookAndFeel . At various points after start up I want to change (let us say) just one detail: the background color of my application. I can change the background color by doing this: for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info

Programmatically swap two JPanels in JFrame

一曲冷凌霜 提交于 2019-12-08 01:42:57
问题 I am trying to accomplish the above functionality, and am having little success. I am using GridLayout with 2 columns and 2 rows to show the user a puzzle-like game, where there are 4 (200x200 pixel) JPanels (3 colored, 1 default bgColor) which fill the whole contentPane. Clicking on a colored panel resolves in an assessment if the panel is next to the gray panel. If so, they should swap. I have accomplished every step to the last one, where they swap. Here's the code: public class MainClass

Java Swing: Clearing custom painting from a JPanel overlayed with other JPanels in a JLayeredPane

被刻印的时光 ゝ 提交于 2019-12-08 00:44:23
问题 I've got a JLayeredPane containing three JPanels, two of which overlap, that I'm painting shapes to. One of the two JPanels that overlap needs to have every shape drawn to it cleared without affecting the shapes drawn to the JPanel under it disappear from the screen. Currently I'm using something like this: Graphics g = pane2.getGraphics(); g.clearRect (0, 0, 1000, 1000); But this not only clears everything painted to pane2 but also pane1, which is under it. So my question is: Is there any

Java drag and drop custom cursor

筅森魡賤 提交于 2019-12-08 00:37:18
问题 I have defined a custom canvas style component, using JPanel, that will support the dragging of objects onto the canvas. What I can't seem to figure out is how to change the drag and drop (DnD) cursor to a custom one, using a TransferHandler. For example, instead of the default link cursor during DnD, I want substitute my own. Is there a way to do this using a TransferHandler? I suspect I will have to use the AWT DnD support to do this but I am hoping to avoid that if I can. 回答1: By digging

Changing on-screen component for JFrame is clunky and messy

亡梦爱人 提交于 2019-12-07 21:36:15
问题 I haven't been able to find a solution to this problem so far through research and may in fact be a problem with JFrame as a whole. Like any desktop application, most times the clicking of a button or "object" presents an action of changing what is currently being displayed. So for example, if I currently have an "input form" on the display frame and then the user clicks a button labeled " submit ," the screen changes to a congratulations screen saying " your form has correctly been submitted

How does addNotify() and requestFocus() work in Java with JPanel?

大兔子大兔子 提交于 2019-12-07 20:03:56
问题 I have not got a clear idea how does super.addNotify() and requestFocus() methods of JPanel work in general and within the code below in particular: public class Panel extends JPanel implements keyListener, MouseListener, MouseMotionListener { public Panel() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); setFocusable(true); requestFocus(); } public void addNotify() { super.addNotify(); if (thread == null) { addKeyListener(this); addMouseListener(this); addMouseMotionListener(this); thread