jpanel

add JMenuBar to a JPanel?

妖精的绣舞 提交于 2019-12-17 12:42:30
问题 I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so? 回答1: 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. 回答2: JMenuBars are set to the JFrame using the setJMenuBar method. See the following tutorial on how to use them.

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

江枫思渺然 提交于 2019-12-17 10:53:23
问题 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? 回答1: 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[][]

Java GUI repaint() problem?

痴心易碎 提交于 2019-12-17 09:48:21
问题 I have a JFrame. This JFrame contains a JButton. I click the JButton and 10 JTextFields are created. the problem: I cannot see them until "I force a repaint()" by resizing the window. Only then do I see the JTextFields created. CODE: JPanel points = new JPanel(); //Creating the JTextFields: for (int i=0; i<10; i++) { JTextField textField = new JTextField(); points.add(textField); } repaint(); this.repaint(); super.repaint(); points.repaint(); THANK YOU - after the for loop, I just called

Java Container remove method not working correctly

血红的双手。 提交于 2019-12-17 07:53:44
问题 i hava added 1.TextArea 2.TextField then i start adding JButton successively on container...,now by using JRadioButton i want to remove JButton from container using this code i=0; k=0; while(!birdButton[i].isSelected()){ i++; } System.out.println(i); k=i+2; list.removeElementAt(i); listName.removeElementAt(i); System.out.println(k); c.getContentPane().remove(k); but when i select the 1st radiobutton 1st JButton should be deleted because of k=i+2; instead of deleting this one it deletes the

JPanel Padding in Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 07:14:12
问题 I have a formatting question for my Java swing application. It should be fairly straightforward, but I am having difficulty finding any aid (Every topic seems to be regarding removing any default padding in JPanel). The text in my various JPanels hug the sides and top, touching the colored borders: how can I add padding? Thank you. 回答1: Set an EmptyBorder around your JPanel . Example: JPanel p =new JPanel(); p.setBorder(new EmptyBorder(10, 10, 10, 10)); 回答2: When you need padding inside the

Sending messages between two JPanel objects

青春壹個敷衍的年華 提交于 2019-12-17 05:11:10
问题 I have a Java JFrame containing a JPanel. Within that JPanel, there are two separate JPanels. When the user clicks a button in the first JPanel, a message needs to be sent to the other JPanel notifying it which button was clicked. What is the easiest way to send messages between objects like this? 回答1: For mKorbel (and the original poster): What I'm recommending is looser coupling, that the one JPanel has no knowledge of the other JPanel and that all connections are done through a control of

addKeyListener() doesn't work for JPanel

只谈情不闲聊 提交于 2019-12-17 05:04:32
问题 I am trying to make a game engine. I have made the Game class but the error resides in the KeyBoard class. Here I leave some code. Class:: Game package transfer2pc.co.cc.game.tileengine; import java.awt.Graphics; import java.util.HashMap; import javax.swing.JPanel; import transfer2pc.co.cc.game.tileengine.input.KeyBoard; public abstract class Game extends JPanel implements Runnable { /** * */ private static final long serialVersionUID = 640206679500196209L; HashMap<String, ?> maps = null;

Printing a JPanel with Scrollable Jtable On it [closed]

北城余情 提交于 2019-12-17 03:44:51
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I just want to print a JPanel which has a scrollable JTable on it. I have oriented JPanel with JTable on it. But I can print only visible area. The contents in scrollable JTable area are not printed. 回答1: There is a reason why I suggest using something like Jasper Reports, this has taken the better part of two

Rotate BufferedImage Inside JPanel

◇◆丶佛笑我妖孽 提交于 2019-12-17 02:51:39
问题 I am trying to rotate a BufferedImage and display it inside a JLabel (which is inside a JPanel ). The current result produces a white square rotated 10 degrees against a black background, but the image is not present inside the square. I know myPicture is not blank, since myPicture itself displays properly inside the JPanel when not rotated. Here is the code: int w = myPicture.getWidth(); int h = myPicture.getHeight(); BufferedImage newImage = new BufferedImage(w, h, myPicture.getType());

Absolute Positioning Graphic JPanel Inside JFrame Blocked by Blank Sections

隐身守侯 提交于 2019-12-17 02:33:14
问题 I'm trying to improve my understanding of Java, particularly Java GUI, by making a puzzle program. Currently the user selects an image, which is cut up into a specified number of pieces. The pieces are drawn randomly to the screen but they seem to be covered by blank portions of other pieces, and not all of them show up, but I can print out all the coordinates. I am using absolute positioning because a LayoutManager didn't seem to work. I briefly tried layeredPanes but they confused me and