jpanel

JPanel setLocation

时光总嘲笑我的痴心妄想 提交于 2019-11-27 19:36: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

How to set a transparent background of JPanel?

懵懂的女人 提交于 2019-11-27 19:22:40
Can JPanel s background be set to transparent? My frame is has two JPanel s: Image Panel and Feature Panel . Feature Panel is overlapping Image Panel . The Image Panel is working as a background and it loads image from a remote URL. On Feature Panel I want to draw shapes. Now Image Panel cannot be seen due to Feature Panel's background color. I need to make Feature Panel background transparent while still drawing its shapes and I want Image Panel to be visible (since it is doing tiling and cache function of images). I'm using two JPanel 's, because I need to seperate the image and shape

How to display a JTable in a JPanel with Java?

一曲冷凌霜 提交于 2019-11-27 18:28:41
问题 How to display a JTable in a JPanel with Java? 回答1: Imports and table model left as an exercise to the user of this code. Also, the panel layout is arbitrarily chosen for simplicity. public class JTableDisplay { public JTableDisplay() { JFrame frame = new JFrame("JTable Test Display"); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); JTable table = new JTable(); JScrollPane tableContainer = new JScrollPane(table); panel.add(tableContainer, BorderLayout.CENTER); frame

Drawing rectangles on a JPanel

喜夏-厌秋 提交于 2019-11-27 16:22:25
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 contentPane = frame.getContentPane(); contentPane.add(new DrawRectPanel()); frame.show(); This worked, and

Not showing graphics in JPanel which is added to another JPanel

北城余情 提交于 2019-11-27 16:20:49
When adding a JPanel that has graphics to a JFrame , it's working fine. But when I try to add a JPanel in which I have added another JPanel with graphics, its not showing in the JFrame . Please see code below package sample; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JFrame{ public static void main(String[] args) { new Main(); } public Main(){ setTitle("Sample"); setVisible(true); setSize(500,500); setDefaultCloseOperation(EXIT_ON_CLOSE); add(new SamplePanel2()); } } class SamplePanel2 extends JPanel{

Animated Sprites with Java Swing

爱⌒轻易说出口 提交于 2019-11-27 15:58:34
Can someone tell me how can I slowdown the sprites appearance to create a more smooth animation? When I run the code it appears the last (27th) sprite in the JPanel. The animation processing is too fast! Someone told me about Swing Timer, but unfortunately I tried several times with that and I couldn't put the code to run well :( Here is the code that I have so far: package sprites; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame;

How to switch JPanels in a JFrame from within the panel?

若如初见. 提交于 2019-11-27 15:54:18
So, I'm trying to make a basic functional menu for a simple game. I tried to do this by creating 2 JPanels, one for the actual game, and another for my menu. What I'm trying to do is have a button on my Menu panel that when pressed, switches the JPanel being displayed in the parent JFrame from that of the menu to that of the actual game. Here is my code: class Menu extends JPanel { public Menu() { JButton startButton = new JButton("Start!"); startButton.addActionListener(new Listener()); add(startButton); } private class Listener implements ActionListener { public void actionPerformed

Change size of JPanel using CardLayout

心已入冬 提交于 2019-11-27 15:52:49
is it possible to change the size of Jpanels when using Java CardLayout? shoot, something like this where the component (here a JLabel rather than a JPanel) has it's preferredSize set, then place it in another JPanel that uses an appropriate layout, here GridBagLayout which with default settings will center the component, and add the GridBagLayout using JPanel to the CardLayout using panel:: import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.Border; public class MultiSizedPanels { private static void createAndShowUI() { final CardLayout cardLayout = new

Place JLabel on top of JLabel with image in

梦想与她 提交于 2019-11-27 15:46:49
I am pretty sure that this question has been asked before, but my case is slightly different as in i am trying to place a JLabel on top of a JLabel acting as a background, I want to display changing numbers using the JLabels and the numbers need to display over the background, however i am a bit of a swing n00b, thanks in advance, Jonathan Without fully appreciating your requirements, if you simply need to display text over a background image, you'd be better off placing the label on top a custom panel which is capable of painting your background. You get the benefit of a layout manager

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

Deadly 提交于 2019-11-27 15:40:55
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 = xCenter; private int y = yCenter; private char keyChar = 'A'; public LinePanel(){ addKeyListener(new