jpanel

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

时间秒杀一切 提交于 2019-11-29 00:16:27
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? 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 calculates its prefSize to the max of prefSizes of all cards. Simply subclass and implement to return the prefSize

How can I align all elements to the left in JPanel?

走远了吗. 提交于 2019-11-28 22:23:36
I would like to have all elements in my JPanel to be aligned to the left. I try to do it in the following way: JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setAlignmentX(Component.LEFT_ALIGNMENT); As a result Java use left side of all elements as a position of the element and then put all elements in the center (not left part) of the JPanel. Chris The easiest way I've found to place objects on the left is using FlowLayout. JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); adding a component normally to this panel will place it on the

Export JPanel Graphics to .png or .gif or .jpg

霸气de小男生 提交于 2019-11-28 20:43:27
I'm trying to develop some sort of paint using Java. I have a JComponent that is located inside of a JPanel. I already can draw lines and rectangles into that JComponent. Now, how can I export this drawings as an image (png, gif, jpg)? I tried this: BufferedImage b = new BufferedImage(1700,1100,BufferedImage.TYPE_INT_RGB); this.print(getGraphics()); try{ImageIO.write(b,"png",new File("test.png"));}catch (Exception e) {} But that only creates a .png file all black. Help!!! RESOLVED!!! BufferedImage bi = new BufferedImage(this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_ARGB);

Java: Add Background image to frame [duplicate]

二次信任 提交于 2019-11-28 14:50:16
Possible Duplicate: java swing background image drawing your own buffered image on frame I am trying to add a back ground image to my frame, but nothing that I have done works. I designed a slot machine consisting of several panels added to the container. Now, I am trying to add a nice background to the frame. I tried using the paint method. But, since I am already using the paint method to paint the reel images, it is not working on the background. I also tried adding a JLabel, but when I do it overwrites everything or get overwritten depending on how I call it. Following is my code; any help

Java Swing — Key Input with JPanel added to JOptionpane

廉价感情. 提交于 2019-11-28 14:43:31
When I run the code, the added Example1 class to the JOptionPane (in Frame) should get keyInput and then change the y value of the player instance (in example1), but it doesn't work. Also, how would I be able to rotate the ship on its axis and then move in the direction its facing? Currently it moves in the direction its rotated towards, but it rotates on what seems the coordinates 0,0. Frame import javax.swing.*; import java.awt.*; /** * Created by griffin on 12/7/2015. */ public class Frame extends JFrame { public Frame() { initUI(); } private void initUI() { JTabbedPane jtp = new

Java swing, scrollbar does not working with absolute layout

我的未来我决定 提交于 2019-11-28 14:40:23
I want to have a jpanel with fixed size without layout manager in jscrollpane (which will be also without layout manager). I cant use any layout manager because i need to create an rectangle/circle at a location where user clicks (and allow drag & drop for all the created emelents) setLocationRelativeTo(null); setSize(300,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setName("dnd test"); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane scroll = new JScrollPane(panel,v,h); scroll.setLayout(null); scroll

How can I update JPanel continuously?

≯℡__Kan透↙ 提交于 2019-11-28 14:39:24
I've got a slight problem, I'm writing a gps tracking app to track several objects at once. The data comes in over a serial interface, this is coming in fine from what I can tell. The issue is that I need to continually update the JPanel where the map is created and displayed. public JPanel mapDisplay(){ JPanel mapPanel = new JPanel(); mapPanel.setSize(560, 540); Coordinate start = new Coordinate (-34.9286, 138.6); trackMap.addMapMarker(new MapMarkerDot(1Lat, 1Lon)); trackMap.setDisplayPosition(start,8); System.out.println(1Lat); mapPanel.add(trackMap); mapPanel.setVisible(true); return

How to draw multiple shapes on JComponent or Jpanel?

冷暖自知 提交于 2019-11-28 14:38:02
I'm trying to build Paint app and I doing something wrong in DrawingArea class. The problem is when I try to draw second shape , the first shape or figure is auto deleting so I need to some idea about how to solve this.All answers acceptable. THANKS FOR HELP. There is part of DrawingArea.class codes : @Override // GETTING FIRST (STARTING) COORDINATE WHEN THE MOUSE PRESSED public void mousePressed(MouseEvent e) { oldX = e.getX(); oldY = e.getY(); repaint(); } @Override // GETTING RELEASED COORDINATE TO DRAW LINE. public void mouseReleased(MouseEvent e) { lastX = e.getX(); lastY = e.getY();

JPanel setLocation

纵饮孤独 提交于 2019-11-28 14:30:28
Where it says: usePanel.setLocation(800,usePanel.getY()); it doesn't set the location it just starts in the middle, but inside the for loop where it gradually changes the position for a cool animation, it works fine. Any idea why the first set location to get it started isn't working? This is under FlowLayout btw so I assume that may have something to do with it but am not sure. usePanel.setLocation(800,usePanel.getY()); for(int x=0;x<3500;x+=6){ usePanel.setLocation(usePanel.getX()-5,usePanel.getY()); Thread.sleep(500); } Updated with current code private Timer nextloc; private JPanel

JPanel not listening to key event when there is a child component with JButton on it

我怕爱的太早我们不能终老 提交于 2019-11-28 14:23:46
I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should. This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I