Results 1 to 1 of 1
- 03-15-2012, 09:52 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 12
- Rep Power
- 0
Trouble displaying an image using GridBagLayout
hi
I am creating a GUI and have a number of labels and buttons within my JPanel. I have successfully added an image however it is not displaying in full. Only the top left corner is visible. I have constraints in place allowing me to position the image using gridx and gridy and have tried using gridHeight/gridWidth but it still does not sort the problem out. Here is my code.
The image is passed by the following class:Java Code:public class GUI extends JFrame implements ActionListener{ int mode =0 ; JFrame frame = new JFrame("interface"); JPanel panel; public int run() { frame = new JFrame("Interface"); frame.setVisible(true); frame.setSize(800,700); frame.setResizable(false); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); panel = new JPanel(new GridBagLayout()); frame.getContentPane().add(panel, BorderLayout.NORTH); GridBagConstraints con = new GridBagConstraints(); con.gridx = 0; con.gridy =0; con.insets = new Insets(10,10,10,10);// spacing from item on left, right, up ,down JLabel title = new JLabel("WELCOME TO BLACKJACK!"); Font tFont = new Font("serif",Font.BOLD,25); title.setFont(tFont); panel.add(title,con); JLabel option = new JLabel("Please select a mode"); con.gridx = 0; con.gridy = 3; panel.add(option,con); JButton button = new JButton ("Player"); button.setActionCommand("Button"); button.addActionListener(this); con.gridx = 0; con.gridy =4; panel.add(button,con); JButton button2 = new JButton ("Dealer"); button2.addActionListener(this); con.gridx = 1; con.gridy =4; panel.add(button2,con); CardImages s = new CardImages(); con.gridx = 0; con.gridy = 6; panel.add(s,con);
I have made a little progress. When i useJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CardImages extends JPanel{ private ImageIcon image; public CardImages() { super(); } public void paintComponent (Graphics g){ super.paintComponent(g); image = new ImageIcon("QUEEN.jpg"); image.paintIcon(this,g, 0,0); } }
the whole width of the image was displayed but only a strip at the top of the image.Java Code://con.fill = GridBagConstraints.HORIZONTAL;
Any suggestions??
Many ThanksLast edited by DarrylBurke; 03-16-2012 at 02:34 AM.
Similar Threads
-
some swing components are not displaying
By mDennis10 in forum AWT / SwingReplies: 6Last Post: 01-27-2012, 09:26 AM -
Problem with displaying an image in Java Swing
By BackInSoftware in forum Advanced JavaReplies: 10Last Post: 01-11-2011, 06:56 PM -
Getting and Displaying Swing Components From DataBase
By vivekkumar in forum AWT / SwingReplies: 1Last Post: 07-27-2010, 03:32 PM -
Trouble Displaying Image
By LankanSniper in forum Java 2DReplies: 3Last Post: 12-10-2009, 09:26 AM -
Java Swing GridBagLayout Problem
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 06-29-2008, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks