jcomponent

Can I get the right JComponent size before it's shown?

孤街醉人 提交于 2019-12-08 16:37:51
问题 When is the size of the JComponent is calculated? After being shown in the screen or before that? if I send .getSize() message before .setVisible(true) , would it give me the right answer? Thanks 回答1: I sometimes check the sizes of my components when debugging to find out why I can't see them for instance. In most cases, the sizes will be realized when the GUI has been rendered . This can occur when pack() or setVisible(true) has called on the top-level window. My usual sequence of method

JFrame appears blank when components are loading images outside the paintComponent(); method

半城伤御伤魂 提交于 2019-12-08 09:02:03
问题 Now this might sound very strange, which is also the reason I think it's a bug in Java itself. I'm currently making custom components for my applications. These components (which extent JComponent ) overwrite the paintComponent(); method. For some reason the frame appeared blank when any of these componenets was used as soon as I implemented images in the components, I did some debuggin and I found out the following: As soon as the code inside this overwritten method draws an image which was

Importing a JLabel array into a JComponent

守給你的承諾、 提交于 2019-12-08 04:23:44
问题 I looked around trying to resolve my issue but could not find anything to resolve this. I am really new to java and am trying to do projects from the book without using the console but here is what I have, import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; public class FutureInvestmentValue { public static void main(String[] args) { double investmentAmount = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter in the investment amount as a double.

JScrollPane scrollbars not scrollable

◇◆丶佛笑我妖孽 提交于 2019-12-08 01:34:14
问题 I have a class that draw some very simple graphics like lines, circles and rectangles. The lines are dynamically expandable and sometimes when they expand beyond the resolution, it is impossible to see without a scroll bar. Therefore, I've added JScrollPane to my JFrame but unfortunately, the scroll bar is not scrollable despite calling the Layout Manager already. Here's what I have: - A class that draws components (lines, rectangles, circles) - A class that sets up the JFrame/JScrollPane

Drawing multiple graphic2d components into JPanel

ぃ、小莉子 提交于 2019-12-07 22:19:56
问题 I've read a lot of tutorials on drawing Graphics2D components and adding to JPanel/JFrame but I can't find how to add multiple these components into one JPanel simply. My code below adds only 1 component (line) and I can't find why it isn't possible to add more. What am I doing wrong? Desired behaviour: there should be 3 red lines. My whole code: package Examples; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt

Importing a JLabel array into a JComponent

吃可爱长大的小学妹 提交于 2019-12-07 15:41:24
I looked around trying to resolve my issue but could not find anything to resolve this. I am really new to java and am trying to do projects from the book without using the console but here is what I have, import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; public class FutureInvestmentValue { public static void main(String[] args) { double investmentAmount = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter in the investment amount as a double.", "Investment Amout", JOptionPane.PLAIN_MESSAGE)); double monthlyInterestRate = Double.parseDouble

Using Java's JComponent repaint()

大憨熊 提交于 2019-12-07 12:20:12
问题 I'm writing a simple Game of Life program in Java and am having a bit of trouble getting it to animate. I've got a JComponent class called LifeDraw , which displays a grid of pixels, with the following paint method: protected void paintComponent(Graphics g) { super.paintComponent(g); for (int y = 0; y < lGrid.getHeight(); y++) { for (int x = 0; x < lGrid.getWidth(); x++) { if (lGrid.getCell(x,y) == 1) { g.setColor(Color.red); g.fillRect(x * lGrid.getSqSize(), y * lGrid.getSqSize(), lGrid

Make a simple GUI editor in Java Swing using Swing components

梦想的初衷 提交于 2019-12-07 05:25:35
问题 I'm currently working on a project in which I need a very simple editor for GUI-like objects. This editor would be a canvas on which well known GUI widgets can be placed. For example one can place a button and a textfield on there, move them around and resize them. No interaction with the widgets themselves is needed. I've been trying to accomplish this by adapting a very simple paint tutorial, I thought it would be easy to implement it this way, yet I bump into many problems with drawing

Overriding getPreferredSize() breaks LSP

前提是你 提交于 2019-12-06 20:28:56
问题 I always see advices in this site of overriding getPreferredSize() instead of using setPreferredSize() as shown in these previous threads for example. Use of overriding getPreferredSize() instead of using setPreferredSize() for fixed size Components Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? Overriding setPreferredSize() and getPreferredSize() See this example: public class MyPanel extends JPanel{ private final Dimension dim = new Dimension(500,500);

Java get JPanel Components

懵懂的女人 提交于 2019-12-06 18:00:48
问题 I have a JPanel full of JTextFields... for (int i=0; i<maxPoints; i++) { JTextField textField = new JTextField(); points.add(textField); } How do I later get the JTextFields in that JPanel? Like if I want their values with TextField.getText(); Thanks 回答1: Well bear in mind they didn't get there by them selves ( I think a read some questions about dynamically creating these panels at runtime ) In the answers posted there, someone said you should kept reference to those textfields in an array.