jpanel

Positioning a JPanel in a JFrame at specific position

怎甘沉沦 提交于 2019-12-01 06:31:35
I need an help for positioning a JPanel in a specific position into a Jframe. I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a specific x,y position. It's something like this: public class Frame extends JFrame { public Frame(){ this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocation(250, 250); this.setSize(300,300); this.setVisible(true); JPanel panel = new JPanel(); this.add(panel); panel.setLocation(150, 150); panel.add(new JButton("Hello!")); // just to show JPanel }//Frame() }//Frame I don't need a LayoutManager for the position of the JPanel,

JPanel custom drawing using Graphics

≡放荡痞女 提交于 2019-12-01 06:31:04
I have a custom JPanel and sometimes throughout my program, I need to call a method which paints the screen black, that's it. public void clearScreen() { Graphics g = getGraphics(); g.setColor(Color.black); g.fillRect(0,0,getWidth(),getHeight()); } When I launch the program, I call this method. However, I find that sometimes it works, and sometimes it doesn't. It's very odd. I also found out that when it doesn't work, the graphics object is NOT null, and the width and height are also correctly defined (from getWidth() and getHeight()). Why would this sometimes work and sometimes not work? What

Positioning a JPanel in a JFrame at specific position

早过忘川 提交于 2019-12-01 05:08:55
问题 I need an help for positioning a JPanel in a specific position into a Jframe. I have a JPanel in a class who extends JFrame, and I need to put this JPanel in a specific x,y position. It's something like this: public class Frame extends JFrame { public Frame(){ this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocation(250, 250); this.setSize(300,300); this.setVisible(true); JPanel panel = new JPanel(); this.add(panel); panel.setLocation(150, 150); panel.add(new JButton("Hello!")); // just

How to force-refresh/repaint a JScrollPane?

社会主义新天地 提交于 2019-12-01 04:32:58
I am adding lots of components (JPanels, JLabels etc.) into a JScrollPane programagically at the start of my program based on some stuff from a database. It seems that this procedure is too fast for the GUI(?), so the JScrollPane does not always update correctly, i.e the scroll bars are not visible even though the inner JPanel is bigger than the visible area. Resizing the Window (JFrame) fixes the problem, as I assume Java is re-printing the components when they are resized. As a test, I have added a debug-button that I can click after the startup of the program has finished. I am trying to

Black square showing when adding a .GIF on JPanel

孤街浪徒 提交于 2019-12-01 04:20:45
问题 My problem is that when adding a .GIF to a JPanel, it shows this black square background for the .GIF. Result when adding on JPanel: It happens when I use this line: p2.add(loadBar); // where p2 = new JPanel(); However, when I add the same .GIF on the JFrame, the black square is not there anymore. Like this: jf.add(loadBar); // where jf = new JFrame(); Result when adding on JFrame: Part of the class code: String loadLink = "http://i.imgur.com/mHm6LYH.gif"; URL ajaxLoad = null; try { ajaxLoad

JFrame : Getting actual content size

北城余情 提交于 2019-12-01 03:54:52
I have created a JFrame, and attempting to get it's size is giving an incorrect result, compared to what I expect. I have determined that it is including the operating system borders around the edges, and the title bar. How can I get/set the real size which I have available for rendering? The size you are getting is the size of the content and the size of the insets . If you use Jcomponent.getInsets() , you can find the size of the content with simple subtraction. Your question is confusing: JPanels don't have title bars or window borders, they are abstract containers that are INSIDE JFrames.

Dynamically remove Component from JPanel

三世轮回 提交于 2019-12-01 03:44:18
I am adding and deleting components dynamically in a JPanel . Adding and deleting functionality works fine but when I delete the component it deletes the last component rather than the component to be deleted. How can I solve this issue? Billy Bob Interestingly enough I am coming across the same issue and I am surprised people are upvoting the other answer, as he is clearly asking about dynamically created Components, not components already created under a variable name which is obtainable, instead of anonymously created objects. The answer is pretty simple. Use getComponents() to iterate

Java add buttons dynamically as an array [duplicate]

老子叫甜甜 提交于 2019-12-01 03:37:06
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: java - How would I dynamically add swing component to gui on click? I want to add array of buttons dynamically. I tried like this: this.pack(); Panel panel = new Panel(); panel.setLayout(new FlowLayout()); this.add(panel); panel.setVisible(true); for (int i = 0; i < Setting.length; i++) { for (int j = 0; j < Setting.width; j++) { JButton b = new JButton(i+""); b.setSize(30, 30); b.setLocation(i * 30, j * 30);

JPanel setBackground(Color.BLACK) does nothing

a 夏天 提交于 2019-12-01 03:33:50
I have the folowing custom JPanel and I have aded it to my frame using Netbeans GUI builder but the background won't change! I can see the circle, drawing with g.fillOval(). What's wrong? public class Board extends JPanel{ private Player player; public Board(){ setOpaque(false); setBackground(Color.BLACK); } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.red); g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius()); } public void updatePlayer(Player player){ this.player=player; } } If your panel is 'not opaque'

How to force-refresh/repaint a JScrollPane?

会有一股神秘感。 提交于 2019-12-01 02:35:55
问题 I am adding lots of components (JPanels, JLabels etc.) into a JScrollPane programagically at the start of my program based on some stuff from a database. It seems that this procedure is too fast for the GUI(?), so the JScrollPane does not always update correctly, i.e the scroll bars are not visible even though the inner JPanel is bigger than the visible area. Resizing the Window (JFrame) fixes the problem, as I assume Java is re-printing the components when they are resized. As a test, I have