jpanel

Replacement of a JPanel element without adding to the end of the panel?

拈花ヽ惹草 提交于 2019-12-24 16:02:55
问题 I posted a question earlier about this but the solution never worked and now it's under different circumstances. I'm making a "penny pitch" program, that, when the "confirm" button is pressed, a randomized number will dictate which spot on the board(the board is fill with image icons) the "penny" will fall, and in the process it removes the image icon that use to occupant the chosen space. I set up a GridBagLayout to constrain each icon down, and my button has no problem removing the chosen

Why does Jtoolbar break my keyBindings?

别等时光非礼了梦想. 提交于 2019-12-24 13:10:53
问题 I have been trying to use the arrow keys as part of my application. Following the best practice I have stuck with using key bindings. I gound that the arow keys do not produce a key typed event so I employed this answer. However, my application has a number of components and I found that if I have a JToolbar in my JFrame the method in the previous link no longer works. Why is this and how do I have a JToolbar and use key bindings? Here is a SSCCE public class ArrowTest extends JFrame { public

Java Swing: Mouse cursor misbehaves when held over a rectangle

。_饼干妹妹 提交于 2019-12-24 12:28:00
问题 I need to change the cursor while it moves over an array-list of rectangles by the contain(p) method.The problem is My first algorithm to use an iterator to iterate through the rectangles doesn't work as expected.The cursor only changes when hovering over the first rectangle,in the other rectangles neither does it respond by showing the cursor changing nor indicate through the console that the cursor is hovering above them?!! My second solution also refuses to work properly.I use a for loop

BorderLayout.CENTER doesn't center

 ̄綄美尐妖づ 提交于 2019-12-24 12:19:10
问题 I can't get my JLabel to center in my JPanel after adding a ChartPanel to it: JPanel panel = new JPanel(new BorderLayout()); panel.add(visualiser(ternaire), BorderLayout.NORTH);//visualiser(ternaire) is the ChartPanel panel.add(new JLabel("L'alliage a bien été enregistré."), BorderLayout.CENTER); JOptionPane jop = new JOptionPane(); jop.showMessageDialog(null, panel, "Information", JOptionPane.PLAIN_MESSAGE); Any idea why BorderLayout.CENTER doesn't work ? 回答1: JLabel by default is

mousePressed won't respond on JPanel

旧城冷巷雨未停 提交于 2019-12-24 12:13:47
问题 I am trying to write a code that when the user clicks once, a little dot shows up, twice a line forms from the first and second clicks, and third forms a triangle out of all the clicks, and clicking again restarts with a dot. Currently I can get my JFrame and JPanel to open, but clicking does nothing. I added in a System.out.print command just to see if my mousePressed method ever ran, but it never does. Here is my code currently: import java.awt.Graphics; import java.awt.Point; import java

How to center JPanel with dynamic size

回眸只為那壹抹淺笑 提交于 2019-12-24 11:35:16
问题 I need to display chessboard. I have a BoardPanel class which extends JPanel and a GamePanel (also extending JPanel) class containing BoardPanel. GamePanel fills all the application frame. I want BoardPanel to always be a square with size equal to the minimum of GamePanel's width and height (if GamePanel's width is greater than height there should be empty space on the left and right, if it's smaller there should be empty space on top and bottom). It's also important that BoardPanel should be

JTable refreshing

本小妞迷上赌 提交于 2019-12-24 09:49:27
问题 I use JTable with horizontal and vertical scrollbars. My JTable has empty space after rows with data. When I open panel that is situated down of my table it hides a part of JTable and scrolling appears on JTable. This is normal behavior, but then I close that panel, empty space without data becomes grey instead of white color. This only happens when I have horizontal scrollbar on my JTable. I suppose I must force JTable to repaint, I tried resizeAndRepaint() on TableHeader and JTable but it

JPanel Won't Focus After Switching in CardLayout

℡╲_俬逩灬. 提交于 2019-12-24 09:47:05
问题 I am creating a Tetris clone as a personal project. In order to switch between the menus, I am using a CardLayout , but I have come across a problem. When I switch from one menu to the other, the focus does not transfer to the other JPanel . How can I fix this? Here is my JFrame: package com.cgp.tetris; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; import javax.swing.JPanel; public class TetrisFrame extends JFrame { private static

Paint a JPanel to a BufferedImage or Print it without rendering it to screen first

微笑、不失礼 提交于 2019-12-24 07:48:15
问题 Picture this... A program GUI JFrame that is split in 2, EAST and WEST. The first JPanel is just a print preview screen. The EAST side of the JFrame is where the user can create a 1 2 or 3 size image. The user clicks the "Add" button and the defined image on the right goes to the panel on the left. So if the user clicks "Add" 3 times with different size images, then the panel uses FlowLayout to organize the added panel images added on the left. When you run this code, you can see a sorta idea

Set the size of JTextField fixed in a JPanel

懵懂的女人 提交于 2019-12-24 06:42:37
问题 for school I am working on a Java GUI program to store some administrative data. Now I want to display eventdata from my DB (mysql) into a JPanel with use of a JTextField, my problem is I can't get the size of the JTextField fixed as it always takes up a lot of place (see picture) Picture: http://postimg.org/image/5pcklo5n1/ Here's my code, anyone some tips? (I am new to java): public void editEvent() { JFrame frEventEdit = new JFrame ("Event Edit Menu"); frEventEdit.setDefaultCloseOperation