jpanel

Java adding ImageIcon to JLabel

◇◆丶佛笑我妖孽 提交于 2019-12-04 03:50:35
I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame . It has worked in the past for me and now is not, i can't see what I did wrong. I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon ) to the JLabel , or that to the JFrame . This is my code... JFrame frame = new JFrame("no image"); ImageIcon image = new

how to put two jpanels side by side

≯℡__Kan透↙ 提交于 2019-12-04 03:46:09
问题 I try to put two jpanels side by side, but in this moment i can not do what i want I have this code, this.videoPanel= new JPanel(); this.videoPanel.setBackground(new Color(102, 102, 102)); this.videoPanel.setPreferredSize(new Dimension(320, 240)); this.videoPanel.setLayout(new BoxLayout(this.videoPanel, 1)); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createSequentialGroup() .addGroup(layout.createParallelGroup

Is there any way to force GridLayout to leave empty cells?

…衆ロ難τιáo~ 提交于 2019-12-04 03:37:57
I have a JTabbedPane with 2 JPanels set to GridLayout(13, 11). The first JPanel has enough of the cells filled out that it leaves the empty cells. The second JPanel has significantly fewer cells filled and this results in each button getting stretched to fill an entire row. Is there any way to get GridLayout to honor the empty cells, so the buttons in both JPanels are the same size? Paul Samsotha Use nested layouts to get your desired result. Some layouts respect the preferred size of components and some don't. GridLayout is one of the ones that don't. Have a look at this answer to see which

Button and textfield don't show up in Java

南楼画角 提交于 2019-12-04 03:11:29
问题 For school I had to make a JFrame and within that One button and Two textfields. Whatever you put in Textfield one have to get into textfield two when the button is pressed. I got the code to the point I should see the textfields and the button when i run the program. For whatever reason it doesn't. My come so far: package helloworld; import javax.swing.*; import java.awt.event.*; public class HelloWorld extends JFrame { public static void main(String[] args) { JFrame frame = new HelloWorld()

Place components at arbitrary (x,y) coordinates

耗尽温柔 提交于 2019-12-04 03:05:52
I want to place some buttons in a JPanel at random positions (x,y), and these layout classes are annoying. Is this even possible in Swing? You can set the coordinates if you use a null layout: panel.setLayout(null); Button b = new Button(....); panel.add(b); b.setSize(width, height); b.setLocation(x,y); But it is strongly recommended to use layouts. Layout classes are not "annoying", they are your friend if you understand them properly. I propose reading a tutorial about GridBagLayout , it is easy to understand (kinda html tables) and very powerful. use null as "Layout Manager": http://docs

Components on the JPanel doesn't show

假如想象 提交于 2019-12-04 02:15:35
问题 I created a JPanel and i modify it a bit. I change it's background to gradient color here is the class. public class JGradientPanel extends JPanel { private static final int N = 32; private Color color1, color2; public JGradientPanel(Color color1, Color color2) { this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N)); this.color1 = color1; this.color2 = color2; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //Color color1 =

KeyListener on JPanel randomly unresponsive

若如初见. 提交于 2019-12-04 01:56:22
问题 I'm having trouble with the default Java KeyListener in my project. I noticed that the KeyListener doesn't seem to get KeyEvents forwarded sometimes when I start. Symptoms of the problem: When starting the application key input isn't processed. This only happens sometimes. Sometimes I have to close and start the app 7-8 times until this shows up. Sometimes it's the first try. When it happens it won't work until I restart the app again. What I'm using: Window 7 x64 and the newest Eclipse and

JPanel inside another

青春壹個敷衍的年華 提交于 2019-12-04 00:57:24
问题 I have a problem with a JPanel inside another one. I don't know why, but the result is a simple square, but the dimensions aren't correct. Why is that? import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import javax.swing.JPanel; public class jj extends JFrame { private JPanel painel3; private JPanel painel5; private Container container; public jj() { container = getContentPane(); container.setLayout(null); painel5 = new JPanel(); painel5.setBackground(Color.red);

JAVA How to embed JAVAFX's label into a JPanel in swing?

空扰寡人 提交于 2019-12-03 22:00:30
问题 How can I embed a custom label I created using JAVAFX into an existing swing's JPanel? E.g. Custom JavaFX Label: public class CustomJavaFXLabel extends Label{ public CustomJavaFXLabel(){ super(); setFont("blah blah blah"); setText("blah blah blah"); /* *and so on... */ } } Existing JPanel in swing public class SwingApp(){ private JPanel jpanel; public SwingApp(){ jpanel = new JPanel(); jpanel.add(new CustomJavaFXLabel()); //this line does not work } } Error I got is: The method add(Component)

Trying to draw lines with JPanel

两盒软妹~` 提交于 2019-12-03 21:02:37
I am trying to draw lines using JPanel and I have hit somewhat of a wall. I can get two sides down but once it comes to subtracting from the x cord it all goes wrong. package GUIstuff; import java.awt.Graphics; import javax.swing.JPanel; public class DrawPanel extends JPanel{ public void paintComponent (Graphics g){ super.paintComponent(g); int width = getWidth(); int height = getHeight(); int drawCounter = 0; // counters for all the while statements int drawCounter2 = 0; int drawCounter3 = 0; int drawCounter4 = 0; int x1 = 0; // cords change with the while statemetns int x2 = 0; int y1 = 0;