jpanel

Repaint JPanel doesn't work in JApplet

不问归期 提交于 2019-12-12 17:06:18
问题 I have the the main JPanel (in JApplet ) which containts the child JPanel and the button. I want to click the button make the child JPanel removed and another child JPanel added to the main JPanel, but the problem is that only when I reclick the button or adjust the JApplet's size the second child JPanel apprear then. My button's listener : button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { panel.remove(custompanel); panel.add(new CustomPanel

Adding/Removing Lines from a JPanel

為{幸葍}努か 提交于 2019-12-12 17:02:36
问题 I am trying to create a graph implementation where the graph is displayed on a JPanel. I am adding and removing vertices without a problem (I am using more panels for these). The problem I am having is adding lines to represent the edges. I know I can use contentPane.getGraphics().drawLine(x1, y1, x2, y2) to add a line, but it leaves no way for me to delete that line later on. Any ideas? 回答1: GraphPanel uses a List<Edge> to model edges connecting the nodes of a graph. 回答2: I don't think that

Aligning Shape's Center to JPanel's Center

送分小仙女□ 提交于 2019-12-12 16:50:26
问题 I have been trying to align my java2d shape's center to the JPanel's center with no success. I was able to do it for an image and many 2D shapes like parallelogram using getBounds method but not for rhombus though all of them follow the same pattern. Drastically, when I prepared an SSCCE out of the actual project I could align none of them correctly. I've written a drawShape method for drawing shapes on center. I didn't understand where I'm going wrong. This is SSCCE: import java.awt.*;

Fully REMOVE JLabel from JPanel…not setVisible(False)

吃可爱长大的小学妹 提交于 2019-12-12 16:25:31
问题 I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime? ImageIcon image7= new ImageIcon("archmageanim.gif"); JLabel label7 = new JLabel("", image7, JLabel.CENTER); p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it //I'm using - it works fine. I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)..

Listening for key strokes in a nested panel

落爺英雄遲暮 提交于 2019-12-12 16:14:40
问题 In the Java file below, I create a frame containing a panel, which then nests a second panel. I'm trying to listen for key strokes in the nested panel. My approach is to use an input map and an action map. I've found if I only have an input map for the nested panel, things work as expected. However, if the parent panel also has an input map, key stroke events are not passed to the nested panel. You can observe this behavior by commenting and uncommenting the first call to getInputMap().put.

Java - Screen Size on a Mac

风格不统一 提交于 2019-12-12 16:07:53
问题 I have a Java application, and in order to find the size of the screen to use, I do the following, for example: Toolkit.getDefaultToolkit().getScreenSize().width; Toolkit.getDefaultToolkit().getScreenSize().height; In my application, I am painting some items on the screen. And to determine the position, I use these values for width and height (because I want a particular item at the very bottom). But, I'm finding that it is going off the screen, even though it's position doesn't exceed what

How do I recursively disable my components in Swing?

 ̄綄美尐妖づ 提交于 2019-12-12 16:05:24
问题 How do I recursively disable all of my components in a JPanel? 回答1: void setEnabled(Component component, boolean enabled) { component.setEnabled(enabled); if (component instanceof Container) { for (Component child : ((Container) component).getComponents()) { setEnabled(child, enabled); } } } Be aware that the previous enabled/disabled state of each component will be lost, unless you keep track of it somewhere else. 来源: https://stackoverflow.com/questions/13920279/how-do-i-recursively-disable

JPanel does not generate MouseEvents when cursor is on child components

a 夏天 提交于 2019-12-12 14:47:33
问题 It is a bit strange for me but JPanel does not generate MouseEvents when cursor is on child components: JTextField and JToolBar but it generates MouseEvents when cursor is on JLabel. Could someone explaind me why? Is there any way to force JPanel to generate events even if mouse is on child components? 回答1: The event dispatcher will forward mouse events to the listeners registered to the component that is returned by the package-level getMouseEventTarget method in Container . This will be

Creating endless number of objects in JPanel and draw them through PaintComponent in Java

六月ゝ 毕业季﹏ 提交于 2019-12-12 13:53:17
问题 I have one dilemma , how to realize application. I have JPanel with width 288 and height 512, then I created two objects ( images ) and drew them through paintComponent using coordinates drawImage (Image1,288,128,this) ; drawImage (Image2, 288, 384, this); . They are decrementing simultaneously in the X axis and when it reaches x = 144 , new (same) images should be drawn at the coordinates ‘( x = 288 , y = (int)Math.random()* 512 )’ and begin decrement as well as first ones should still

JTextArea inside JPanel inside JScrollPane

浪子不回头ぞ 提交于 2019-12-12 12:37:04
问题 I am creating a console and need to have two JTextArea s inside a JPanel inside a JScrollPane . Since setLineWrap(true) does not work when it is not directly inside the JScrollPane , I found a neat workaround here. This allows the LineWrap property to work, but sadly breaks the BorderLayout.CENTER property for one of the JTextArea s. So now the second JTextArea won't fill the entire Pane. Here is the stripped down code for the console: import java.awt.BorderLayout; import java.awt.Color;