jpanel

Keep BoxLayout From Expanding Children

不打扰是莪最后的温柔 提交于 2019-12-23 21:28:56
问题 I want to stack some JComponents vertically inside a JPanel so they stack at the top and any extra space is at the bottom. I'm using a BoxLayout. The components will each contain a JTextArea that should allow the text to wrap if necessary. So, basically, I want the height of each of these components to be the minimum necessary for displaying the (possibly wrapped) text. Here's a contained code example of what I'm doing: import javax.swing.*; import java.awt.*; public class TextAreaTester {

Clear foreground of a drawing panel in Swing

北城以北 提交于 2019-12-23 19:46:44
问题 Making this paintbrush program I decided I want to clear the panel container ( panel2 in the linked code). I used PanelName.setForeground(null) and it worked. What I'd like to know is if there are known drawbacks of this approach and other ways of removing graphics objects from the container. 回答1: If you override paintComponent , you can clear its Graphics using Graphics#clearRect. But overall, I prefer your approach because mine would require you to either subclass JPanel , or anonymously

.getSize() isn't updated

柔情痞子 提交于 2019-12-23 19:27:28
问题 I need to be able to change the size of an JPanel in a event function and then get the size again. It seems that the JPanel is not updated untill the function call has been finished. How can I get the real size? This is an SSCCE: import java.awt.*; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import javax.swing.*; public class Test extends JFrame implements MouseWheelListener{ JPanel p; Test(){ setLayout(new FlowLayout()); setPreferredSize(new Dimension

Nested JPanel with GridBagLayout

左心房为你撑大大i 提交于 2019-12-23 17:35:05
问题 I have a JFrame with GridBagLayout . weightx and weighty values are assigned different no-zero values and GridBagConstraints.fill = GridBagConstraints.BOTH. I nested a JPanel in one of the cells and gave it a GridBagLayout too. When adding components to the nested JPanel, the cell size where the nested JPanel resides grows on all sides missing up the parent's layout. Insets and padding are not used. How can I fix this problem?Thanks. This is an example of the GridBagConstraints values:

Canvas and InputMap

时光毁灭记忆、已成空白 提交于 2019-12-23 15:43:09
问题 I'm building a 2d game in Java and I decided to use Canvas on which I would display the images relevant to the current frame. I'm using Canvas because I've heard it is more efficient in terms of time than JPanel. Is it true? Also, I would like to add some input to the game through key bindings since key listeners could cause focus issues and are lower level construct: keylistener not working after clicking button (see answer). Is there a way to use key bindings with a Canvas? Or I would

java grid bag layout: avoiding center alignment

纵饮孤独 提交于 2019-12-23 12:54:07
问题 In my GUI application, I have several JPanels that are instantiated at various points during the running of my program, and certain actions will cause one of them to be displayed in a scroll pane: mViewport.setViewportView(currentPanel); The trouble is, that my panels are done using Grid Bag Layouts, and the behaviour of that is to center itself within the JScrollPane it is inside of. This make the GUI look weird. Anyone know of a way to force a grid bag layout panel to top-left align? Thanks

Repainting a JPanel in a JScrollPane

左心房为你撑大大i 提交于 2019-12-23 12:14:58
问题 The project that I am working on is an inter-computer utility for playing table top games, and a key part of that is an interactive grid/map. Well I have built in functionality for custom map creation, and want the screen to be able to display very large maps, as well as very small maps. To this effect I have created a series of classes with which I can display a grid of any size. However what I would like to do is to put the JPanel which this is accomplished with into a JScrollPane, thereby

addMouseListener for a JPanel

佐手、 提交于 2019-12-23 07:46:44
问题 Today I have a problem.. My program make a 8x8 grid and show the coord when I click on a JButton . BUT I refuse to use JButton and I need to go for JPanel .. But my addMouseListener isn't working so I don't know how is it possible to fix that I'm searching since 4h..... package coordboutons; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CoordBoutons extends JFrame { CoordBoutons() { super("GridLayout"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container

Why is my fillpolygon shape looking odd (Java GUI)?

走远了吗. 提交于 2019-12-23 07:03:00
问题 I'm trying to make a black polygon with 8 points and put it in the center underneath the button. I believe I did everything correctly, but the polygon looks partly smushed and not centered under the button. Could someone explain why it's not working properly? My overriding panel to create the polygon. import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; class OwnJpanel extends JPanel{ public void paintComponent(Graphics g){ int[] xpoints = {10, 20, 25, 25, 20, 10, 5, 5}

Java JTextArea that auto-resizes and scrolls

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:01:24
问题 I have a JTextArea in a JPanel. How can I have the JTextArea fill the whole JPanel and resize when the JPanel resizes and scroll when too much text is typed in? 回答1: JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); //give your JPanel a BorderLayout JTextArea text = new JTextArea(); JScrollPane scroll = new JScrollPane(text); //place the JTextArea in a scroll pane panel.add(scroll, BorderLayout.CENTER); //add the JScrollPane to the panel // CENTER will use up all available