jpanel

Drawing rectangles on a JPanel

混江龙づ霸主 提交于 2019-11-26 22:26:48
问题 I have a JScrollPane and on top of it I have a JPanel named 'panel1'. I want some rectangles to be drawn on this JPanel. I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff. The problem is that, I tried to draw the rectangles on panel1 by writing the following code : panel1.add(new DrawRectPanel()); but nothing appeared on panel1 then I tried, just as a test to the class DrawRectPanel : JFrame frame = new JFrame(); frame.setSize(1000, 500); Container

Too many JPanels inside a JPanel (with GridBagLayout)

一个人想着一个人 提交于 2019-11-26 22:26:43
问题 So basically if I put JPanel s inside a JPanel that uses GridBagLayout and I restrict the size with setPreferredSize , eventually it reaches a point where it can't hold all of them, and it exhibits the behavior shown in the attached picture: I'm making an accordion. This is just an example to showcase the problem I'm having. Each part of the accordion can open individually and they're of arbitrary size and get added on the fly. Its easy enough to get the heights of all the individual panels

Why can't I access my panel's getWidth() and getHeight() functions?

五迷三道 提交于 2019-11-26 22:25:51
问题 I'm writing a simple program to test out basic GUI. The program prints a letter in the middle of the screen and allows the user to move it with the arrow keys. Everything works fine, but when I try to center the letter at the start of the program, it seems that the getWidth and getHeight functions aren't returning the proper numbers. Here's the snippet containing my Panel class static class LinePanel extends JPanel{ int xCenter = getWidth() /2; int yCenter = getHeight() /2; private int x =

JPanel with image background

时光毁灭记忆、已成空白 提交于 2019-11-26 22:25:31
问题 How to put image background on JPANEL? JPanel pDraw = new JPanel(new GridLayout(ROWS,COLS,2,2)); pDraw.setPreferredSize(new Dimension(600,600)); //size of the JPanel pDraw.setBackground(Color.RED); //How can I change the background from red color to image? 回答1: Here's an explanation. 回答2: It is probably easiest to load the Image into an ImageIcon and display it in a JLabel , however: To directly 'draw' the image to the JPanel, override the JPanel's paintComponent(Graphics) method to something

Concerns about the function of JPanel: paintcomponent()

只谈情不闲聊 提交于 2019-11-26 22:11:57
hello I'm new in java programming, and i need someone explaining me these lines of code: public class drawpanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); ... } } I dont understand the line public void paintComponent(Graphics g) : why do i have to declare that function like that if it's predefined in JPanel? And this line super.paintComponent(g) : I dont understand it at all. Thank you for your help. The Basic Structure: The extends keyword means that DrawPanel inherits from JPanel . In other words, DrawPanel "is a" JPanel . As such, it can override its

GridBagLayout panels alignment

£可爱£侵袭症+ 提交于 2019-11-26 22:02:41
问题 I have a little issue with the GridBag Layout Manager. I am trying to display 9 panels like this: To do so, I separated the Grid like this: For the panels 1 to 7 there is no problem, they show up just as I want. But the issue starts with the panels S8 and S9 . As you can see, the S8 and S9 takes up half the frame, but I can't make it display like this. The S8 ends at the start of S4 , and the S9 begins at the end of S4 , I cannot handle the half space. Only way I figured out is to put the S8

How to print a large JPanel in several page

 ̄綄美尐妖づ 提交于 2019-11-26 21:59:42
问题 I want to print a very large panel and this panel contains some components like jtable, jlabel and others jpanel. Now i want to print it in differents pages. But i don't know how to do it. I have implemented Printable in my panel class. But if i print it, I get only one page. 回答1: Try This ? package com.mymoney.util; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.PageFormat; import java.awt.print.Pageable;

add controls vertically instead of horizontally using flow layout

左心房为你撑大大i 提交于 2019-11-26 21:27:01
问题 I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally. I want to add checkboxes vertically on the Panel. What is the possible solution? 回答1: I hope what you are trying to achieve is like this. For this please use Box layout. package com.kcing.kailas.sample.client; import javax.swing.BoxLayout; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax

Java items appear only after the window is resize

南笙酒味 提交于 2019-11-26 21:13:28
问题 I have 2 JPanels in a frame. The first panel contains java items like buttons etc. The two buttons I added appears but the JSpinner appear just after I resize the window. I guess this will happen also with other items I will add. How could I solve this problem? import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax

Obtaining focus on a JPanel

爷,独闯天下 提交于 2019-11-26 21:10:25
问题 I have a JPanel inside a JFrame . I have registered a KeyListener , based on which I want to update the JPanel . The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment: myFrame.setFocusable(false); myPanel.setFocusable(true); myPanel.addKeyListener(myKL); myFrame.add(myPanel); Has