jcomponent

Java - set the exact position of a JComponent

徘徊边缘 提交于 2021-02-10 18:25:35
问题 I need help with setting the x and y position of a JLabel on a JPanel, not North,South,West,East. Thank you for your time. 回答1: To have a JPanel respect exact locations, you should set its layout to null. Then, you can use setLocation on any JComponent to set its location within that JPanel. JPanel panel = new JPanel(); panel.setLayout(null); JLabel label = new JLabel("text"); label.setLocation(50, 20); panel.add(label); However, note that there are several downsides of absolute position

Java Swing: Access panel components from another class

假如想象 提交于 2020-01-29 20:33:27
问题 Hi i basically have two classes, one main and one just to separate the panels, just for code readability really. i have : public class Main{ public static void main (String args[]) { JFrame mainJFrame; mainJFrame = new JFrame(); //some other code here CenterPanel centerPanel = new CenterPanel(); centerPanel.renderPanel(); mainFrame.add(centerPanel.getGUI()); } } class CenterPanel{ JPanel center = new JPanel(); public void renderPanel(){ JButton enterButton = new JButton("enter"); JButton

Get a component from a JTextPane through javax.swing.text.Element?

点点圈 提交于 2020-01-19 16:22:50
问题 I am using a JTextPane to display characters and symbols, where the latter are represented by custom painted JComponents . For example, the text pane might show something like this: The text pane is user editable and it is allowed for the user to add more symbols via a button at any position and as a replacement for selected text. I do this via the JTextPane.insertComponent() method. At some point in the application I need to know what is currently being displayed in the text pane, and by

Is it possible to use KeyBindings in JFrame & Canvas?

女生的网名这么多〃 提交于 2020-01-16 16:10:34
问题 I've been have difficulty with KeyListener (not detecting keys) and I'm trying to switch over to KeyBindings but JFrame doesn't extend JComponent which is needed for the getInputMap() function. Any ideas? 回答1: Use a surrogate JComponent instead. For example, set the frame's contentPane to be a JPanel and use it to register the key bindings. This is a very basic example, which uses a JPanel as the contentPane and adds a Canvas to it. The key bindings are then registered through the JPanel on

Prevent Java from repainting the content of a JPanel while updating

社会主义新天地 提交于 2020-01-09 11:58:13
问题 I have a JPanel which contains a lot of child components. While updating\adding new components to the parent JPanel I'd like to prevent it from repainting, how can this achieved? 回答1: Try RepaintManager.currentManager(component).markCompletelyClean(component). It will prevent the component from repainting. You might need to do this after each time you add new components. 回答2: setVisible(false) update setVisible(true) 回答3: you could try by using setIgnoreRepaint(boolean value) but it's a

Prevent Java from repainting the content of a JPanel while updating

我只是一个虾纸丫 提交于 2020-01-09 11:58:08
问题 I have a JPanel which contains a lot of child components. While updating\adding new components to the parent JPanel I'd like to prevent it from repainting, how can this achieved? 回答1: Try RepaintManager.currentManager(component).markCompletelyClean(component). It will prevent the component from repainting. You might need to do this after each time you add new components. 回答2: setVisible(false) update setVisible(true) 回答3: you could try by using setIgnoreRepaint(boolean value) but it's a

JComponents are not showing up on JPanel

谁说我不能喝 提交于 2020-01-07 03:00:52
问题 My JComponents aren't showing up on my JPanel for some reason. Here's my code: MAIN CLASS import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; // Main Class - executes the game public class Main extends JFrame { public static int WIDTH = 800, HEIGHT = 800; private JPanel jp; private Dimension d; private Snake s; private Food f; public Main() { setTitle("Snake"); setSize(WIDTH, HEIGHT);

Why is my JFrame just a black window?

冷暖自知 提交于 2020-01-06 14:41:07
问题 My code is this, when I run it I just get a black window and I have no idea why. Thanks for any feedback. Its supposed to print out a picture, and eventually make it move. package assignment04; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; public class GoLDriver { public static void main(String[] args) { GoLModel model= new GoLModel(); JFrame frame = new JFrame(); JPanel panel= new JPanel(); panel.setLayout(new BorderLayout());

Why is my JFrame just a black window?

余生颓废 提交于 2020-01-06 14:39:03
问题 My code is this, when I run it I just get a black window and I have no idea why. Thanks for any feedback. Its supposed to print out a picture, and eventually make it move. package assignment04; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; public class GoLDriver { public static void main(String[] args) { GoLModel model= new GoLModel(); JFrame frame = new JFrame(); JPanel panel= new JPanel(); panel.setLayout(new BorderLayout());