jpanel

is there a way to set a jbutton on top of a jbutton?

旧街凉风 提交于 2019-12-25 05:00:51
问题 I am wondering about this since we are making a game in Swing and we made our map tiles into jButtons instead of jPanels for whatever reason. Now we want to put units on top of them so the map background is still shown when the unit is on top of them. Anyone know if this is possible? 回答1: OK, so I am not sure this is really what you are looking for and how your application is currently set up, but this is an example to have JButtons on top of each other (if this is not what you are looking

fixing JScrollPane with JLabel

时间秒杀一切 提交于 2019-12-25 04:25:04
问题 In order to add a background Image , I work on JLabels instead of JPanel . JFrame frame=new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label=new JLabel(new ImageIcon("bg.png")); label.setLayout(new BoxLayout(label, BoxLayout.Y_AXIS)); for(int i=0;i<20;i++) label.add(new JLabel(i.toString())); frame.add(label); the problem here is that only components that fit the background image will be shown (in my example only the 10 first JLabels) I tried using a JScrollPane

Pack method called from actionPerformed functions only sometimes

允我心安 提交于 2019-12-25 04:12:29
问题 Problem Upon compiling and running my program multiple times, sometimes pack() works and the components of newGamePanel are compressed, and sometimes it doesn't work, and newGamePanel expands to fill the JFrame values set by setSize() . I have been unable to reliably reproduce either result-- it really seems to be random. Note: As I cut down on the amount of GUI formatting to have a reasonable amount of code to review, the GUI is pretty trash. However, the problem is still easy to identify.

How to add multiple classes to a single JFrame?

北战南征 提交于 2019-12-25 04:06:15
问题 So I'm trying to add multiple classes to my JFrame 'frame' using a JPanel 'panel' but it doesn't seem to have any effect. Here's my main class: import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class Frame { public static void main (String[] args) { JPanel panel = new JPanel(); panel.setBackground (Color.WHITE); panel.add (new Player()); // Class with paintComponent method. panel.add (new Terrain()); // Class with paintComponent method. JFrame frame = new

How to add multiple classes to a single JFrame?

筅森魡賤 提交于 2019-12-25 04:06:04
问题 So I'm trying to add multiple classes to my JFrame 'frame' using a JPanel 'panel' but it doesn't seem to have any effect. Here's my main class: import java.awt.Color; import javax.swing.JFrame; import javax.swing.JPanel; public class Frame { public static void main (String[] args) { JPanel panel = new JPanel(); panel.setBackground (Color.WHITE); panel.add (new Player()); // Class with paintComponent method. panel.add (new Terrain()); // Class with paintComponent method. JFrame frame = new

How do I arrange JPanels from top to bottom?

十年热恋 提交于 2019-12-25 04:05:20
问题 I'm currently self-studying Java. I'm learning Graphical User Interface(GUI) programming. I want JPanel s to be arranged from top to bottom in a JFrame .First of all,I have a JLabel added to the first JPanel . The second JPanel has 5 JRadioButtions . The third JPanel has a JButton and a JLabel . When the JButton is pressed,the JLabel in the 3 rd JPanel shows some text. I used BoxLayout ( BoxLayout.X_AXIS ) for all the JPanels and added all 3 of them into a JFrame which has FlowLayout() . Here

Changing cell background on click in another cell

拟墨画扇 提交于 2019-12-25 04:03:25
问题 I am trying to build a 9x9 sudoku puzzle. For the UI part, I want to change the background of cell when clicked and return to normal when any other cell is clicked. Below is my Cell Class. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class Cell extends JPanel implements MouseListener{ Cell[][] cell; private int x;//x pos; private int y;//y pos; private int num=0; private JLabel lnum; private Color bg; private static int xpos=

Changing cell background on click in another cell

主宰稳场 提交于 2019-12-25 04:03:09
问题 I am trying to build a 9x9 sudoku puzzle. For the UI part, I want to change the background of cell when clicked and return to normal when any other cell is clicked. Below is my Cell Class. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class Cell extends JPanel implements MouseListener{ Cell[][] cell; private int x;//x pos; private int y;//y pos; private int num=0; private JLabel lnum; private Color bg; private static int xpos=

Accessing GUI JTextField objects that are dynamically generated

旧时模样 提交于 2019-12-25 03:32:42
问题 I am writing a program that contains a JButton . Every time the button is clicked, a new JTextField is added to a JPanel . My problem is that, after the user has created all the JTextFields and filled them with information, I need to get the text of each field. How can I get access to the JTextFields when they are dynamically generated, as they don't have instance names? Is there a better way to get the text of each one, without knowing their instance name. Here is the code of the

How to move a Rectangle with arrow keys?

不问归期 提交于 2019-12-25 02:57:07
问题 I have a frame which as a rectangle in it. I want to know how can I move the rectangle in if I clicked the arrow keys. I searched, and find few examples but nothing worked (weird, as it should be a simple thing to do) Here is my Rectangle class: public class PlayerOne implements KeyListener { int x,y; public PlayerOne(JPanel panel){ this.x = panel.getWidth()/2; this.y = panel.getHeight()/2; } public void paint(Graphics g){ g.setColor(Color.RED); g.fillRect(125, 480, 60, 10); } @Override