jpanel

JPanel with image background

三世轮回 提交于 2019-11-27 15:36:30
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? Here's an explanation. 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 like the following: public void paintComponent(Graphics page) { super.paintComponent(page); page.drawImage(img,

How to set a JFrame size to fit the CardLayout displayed JPanel?

蹲街弑〆低调 提交于 2019-11-27 15:15:59
问题 I have a JFrame containing a set of JPanels in a CardLayout . Each JPanel has a different size and I want the JFrame to adapt to the size of the currently displayed JPanel (not the JPanel to adapt to the size of the JFrame ). How can I achieve this? 回答1: The general is: if you have a layout problem, always solve it with an appropriate LayoutManager. Never tweak a component's sizing hint to reach your goal. In this case, it's particularly easy to adjust the CardLayout. By default, it

add JMenuBar to a JPanel?

此生再无相见时 提交于 2019-11-27 15:03:40
I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so? You can use a BorderLayout for your JPanel and put the JMenuBar into the NORTH area of the panel with JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(menubar, BorderLayout.NORTH); JMenuBar is a JComponent and can be added to a Container like any other JComponent. JMenuBars are set to the JFrame using the setJMenuBar method. See the following tutorial on how to use them. http://download.oracle.com/javase/tutorial/uiswing/components/menu.html Panday Manish Sahay I tried too but

How to fit Image size to JFrame Size?

别说谁变了你拦得住时间么 提交于 2019-11-27 14:48:49
I have a JPanel into a JFrame . I loaded a picture on the JPanel but its shown just a part of the picture: This is the part of the code where i did it: JPanel panelImg = new JPanel() { public void paintComponent(Graphics g) { Image img = new ImageIcon("Welcome.png").getImage(); Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); g.drawImage(img, 0, 0, null); } }; mainFrame.add(panelImg); So this is how it looks like: The complete picture looks like this: Is there a way to

how to trigger an action in parent JPanel when a component in a child JPanel is updated (Java Swing)

只愿长相守 提交于 2019-11-27 14:29:47
I am trying to build an MVC application in Java Swing. I have a JPanel that contains four JComboBoxes and this JPanel is embedded into a parent JPanel. The parent JPanel has other controls in addition to the child JPanel. The child JPanel's model gets correctly updated whenever I change the values of the JComboBoxes (it's basically a date picker with one combo box each for year, month, day of month, and hour of day). What I cannot figure out is how I can trigger the parent JPanel's model to update itself to match the value stored in the child JPanel's model whenever one of the JComboBoxes is

Display a jpg image on a JPanel

帅比萌擦擦* 提交于 2019-11-27 14:02:26
What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel? Cheers. ImageIcon image = new ImageIcon("image/pic1.jpg"); JLabel label = new JLabel("", image, JLabel.CENTER); JPanel panel = new JPanel(new BorderLayout()); panel.add( label, BorderLayout.CENTER ); You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel. I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel. Here's Sun's docs on the subject matter. I would use a Canvas that I add to the JPanel,

Can I add a component to a specific grid cell when a GridLayout is used?

怎甘沉沦 提交于 2019-11-27 13:31:41
When I set the GridLayout to the JPanel and then add something, it is added subsequently in the "text order" (from left to right, from top to bottom). But I want to add an element to a specific cell (in the i-th row in the j-th column). Is it possible? No, you can't add components at a specific cell. What you can do is add empty JPanel objects and hold on to references to them in an array, then add components to them in any order you want. Something like: int i = 3; int j = 4; JPanel[][] panelHolder = new JPanel[i][j]; setLayout(new GridLayout(i,j)); for(int m = 0; m < i; m++) { for(int n = 0;

JPanel Drop Shadow

℡╲_俬逩灬. 提交于 2019-11-27 11:57:29
I have a JPanel element and I would like added a drop shadow to it, how can I add a nice faded drop shadow to the element? Do I need to use external libraries or is there something that is built in that I can use? Example: So I looked into swingx which extends JPanel and was able to achieve the results I was looking for with the following code: public class Canvas extends JXPanel{ public Canvas(){ DropShadowBorder shadow = new DropShadowBorder(); shadow.setShadowColor(Color.BLACK); shadow.setShowLeftShadow(true); shadow.setShowRightShadow(true); shadow.setShowBottomShadow(true); shadow

JPanel vs JFrame in Java

馋奶兔 提交于 2019-11-27 11:20:17
I am learning Java gui. The way I learnt to create a window is to inherit or Extend JFrame class and it is good to use it, as JFrame contains all the properties of a Window. Now If I want to add something to this window, I need to use add() method. But Today I came across JPanel which also creates a windows and we can add stuff by jpanelObjec.add() . What is the difference between the two methods? Are they somehow related? You should not extend the JFrame class unnecessarily (only if you are adding extra functionality to the JFrame class) JFrame : JFrame extends Component and Container . It is

How do I paint multiple objetcs that move at different speeds in Java?

爷,独闯天下 提交于 2019-11-27 09:39:34
I am working on homework for class, and its late because I can't seem to understand the material despite all the research that I am doing. I am a beginner and do not know much in the way of java. Also, this is my first post so please be forgiving when you are reading this. I am building on source code from my textbook, which I updated recently for past homework, but now I am trying to generate a class that draws multiple squares and moves those objects independently and at different speeds. They will all need to rebound off the walls as well. I followed the instructions and created two arrays