jpanel

Images and Panels

若如初见. 提交于 2019-12-13 02:40:50
问题 I'm having a problem adding a JPanel on top of an Image . This is what I'm trying to do: Image bgImage = loadImage(filename); JPanel jp = new JPanel(); jp.setBounds(100,100,100,100); jp.setOpaque(true); jp.setBackgroudColor(Color.red); bgImage.add(jp); After doing this, I only see the bgImage . I tried everything but I still can't show the panel. Can somebody help me? 回答1: You cannot place a component inside an Image . What you want to do is paint the Image onto the background of a swing

Which swing containers can be set focusable?

独自空忆成欢 提交于 2019-12-13 02:25:28
问题 I’m using a JPanel to add JComponents on and I tried to set the JPanel focusable so when the user clicks on the JPanel , it will unselect any JComponent and call that JComponent ’s listener. I've read pretty much every Q&A asked about this and I can't find anything that works. So then I thought: is there another container that I can use? 回答1: Did you try panel.requestFocus(); ? Since JPanel is a sub class of Component it could also call this method. This will take focus from whatever has it

Switching jPanels in Java from external class

六眼飞鱼酱① 提交于 2019-12-13 01:35:16
问题 I have jFrame class MainForm which contains main() , placePanel(panel) and drop down menu. From the items in drop down menu I call on placePanel(panel) method to place a specific panel in the jFrame container. This works fine. But, I don't know how to switch panels when I click on a button which is inside a jPanel class. When I try to call jFrame 's MainForm.placePanel(panel) from any jPanel which is loaded into jFrame 's container, I get the error: cannot reference non-static content etc. I

Delay between pressing a key and the key being read as held down

会有一股神秘感。 提交于 2019-12-13 01:27:20
问题 I have noticed that while holding a key down, there is a short delay between the key being read as being held down. What I mean is this; hold down any key and observe closely. The pattern goes like this: h.....hhhhhhhhhhhhhhhhhhhhhh where . symbolizes a pause. This is fine, because when we type, it would be harder trying to write a single letter e.g. 'i'. But what if we want to make a game? I mean, we wouldn't like the delay. This is exacly my problem. So now, let me show you my code: import

Java, When using GridBagLayout to layout panels, adding buttons to the panels changes the sizes, how do I prevent this?

旧城冷巷雨未停 提交于 2019-12-13 01:24:11
问题 Ok so I am messing around with GridBagLayout to get to grips with it. I want to use it to layout JPanels in a JFrame but these individual panels will use different layout managers, GridBagLayout probably won't suit. But i have a problem. If i don't add anything to the panels, all of the proportions work out perfectly as in this example: public class RealMess extends JFrame{ private JPanel panel; public RealMess(){ initUI(); } public final void initUI(){ Dimension screensize = Toolkit

Painting on a JPanel inside a JScrollPane doesn't paint in the right location

断了今生、忘了曾经 提交于 2019-12-13 01:24:10
问题 So I have a JPanel that's inside a JScrollPane . Now I am trying to paint something on the panel, but it's always in same spot. I can scroll in all directions but it's not moving. Whatever I paint on the panel does not get scrolled. I already tried: A custom JViewPort switching between Opaque = true and Opaque = false Also I considered overriding the paintComponent method of the panel but that would be really hard to implement in my code. public class ScrollPanePaint{ public ScrollPanePaint()

JPanel is not shown after adding it to JFrame

大兔子大兔子 提交于 2019-12-13 01:23:55
问题 I have to setup a basic JAVA application including a GUI based on swing. My idea was to create one JFrame and different JPanels and based on the users input to change the shown JPanel. After running my application the JFrame is shown but not the content of the JPanel. I thought up to here it should be strait forward... but as it seems it isn't. Hope you can give me a hint :) Here is my main: public class Client { public static void main(String[] args) { MainWindow window = new MainWindow();

Modifying the font size for all JPanels in an ArrayList<JPanel>

梦想与她 提交于 2019-12-13 00:56:21
问题 So, I have an ArrayList that contains JPanels; all the JPanels have a BorderLayout with a JPanel (that contains two JLabels) set on NORTH and a JTextArea (containing text, of course) set on CENTER. My question is how do I modify each JTextArea's font size? 回答1: Here is some simple code that allows the JTextArea font sizes to be set via a setFontSize(int index, int fontSize) method. Note that this will only work for the text areas in the panels array list. In the following example, I have

JPanel taking up the whole JFrame

可紊 提交于 2019-12-13 00:56:11
问题 I have an issue where when I include the JPanel in my JFrame, it covers the entire screen. My code reads import java.awt.*; import javax.swing.*; public class TestFrameExample extends JPanel { static String TheQuestion; static String QN1 = "Question 1: "; static String Q1 = "What is Lead's chemical symbol?"; static String brk = "___________________"; public void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(new Color(135, 206, 250)); g.setFont(new Font("Tahoma",

adding multiple components to Jframe.getcontentpane()

北城以北 提交于 2019-12-13 00:42:59
问题 I have a class that extends JPanel and draws a triangle. I called it from other class to create three triangles but when third triangle is drawn the previous two disappeared. How can I add multiple triangles that are shown together. Code is as follows: Triangle.Java: public class Triangle extends JPanel{ Point p1, p2, p3; public Triangle(Point _p1, Point _p2, Point _p3) { this.p1=_p1; this.p2=_p2; this.p3=_p3; } public void paint(Graphics g) { super.paint(g); int[] xs = {p1.x,p2.x,p3.x}; int[