473,383 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How can I put JLabel in the middle of my JPanel

I've been spending a lot of hours fixing this, I'm a 2nd year college student in Computer Science, my professor lack time teaching us about this and she gave us our final project for the semester and I really need help. I want to put the JLabel on the middle of my JFrame, but the JLabel won't position itself, I've used several methods to fix it but nothing won't work. I use a cardpanel for this quiz game and array of string. Another thing that's troubling me is about how to fit the texts on the frame. Hope someone can help me.This is my code actually far from done. Please help me











Expand|Select|Wrap|Line Numbers
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.GridLayout;
  5. import java.awt.Window;
  6. import java.util.Random;
  7. import java.awt.CardLayout;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import javax.swing.*;
  11.  
  12. public class MAINFRAMES extends JFrame {
  13.  
  14.     String[] adulteasy = 
  15.     {
  16.     "What’s full of holes but still holds water?", 
  17.     //Sponge
  18.     "What is the best month for a parade?", 
  19.     //March
  20.     "Pronounced as one letter, And written with three, Two letters there are, And two only in me. I'm double, I'm single, I'm black, blue, and gray, I'm read from both ends, And the same either way.",
  21.     //An Eye
  22.     "What two words have thousands of letters in them?", "At night they come without being fetched. By day they are lost without being stolen. What are they?",
  23.     //Post Office
  24.     "When one does not know what it is, then it is something; but when one knows what it is, then it is nothing.",
  25.     //A Riddle
  26.     "It cannot be seen, cannot be felt, Cannot be heard, and cannot be smelt. It lies behind stars and under hills, And empty holes it fills. It comes first and follows after, Ends life, kills laughter.",
  27.     //Dark
  28.     "A box without hinges, key or lid, Yet golden treasure inside is hid.",
  29.     //Egg
  30.     "What goes up the chimney when down, but can't go down the chimney when up?",
  31.     //Umbrella
  32.     "Each morning I appear, To lie at your feet All day, I will follow you, No matter how fast you run, Yet I nearly perish In the midday sun."
  33.     //Shadow
  34.     };
  35.  
  36.     int length = adulteasy.length;
  37.     int rand = (int) (Math.random() * length);
  38.     int length1 = adulteasy.length;
  39.     int rand1 = (int) (Math.random() * length);
  40.     int length2 = adulteasy.length;
  41.     int rand2 = (int) (Math.random() * length);
  42.     int length3 = adulteasy.length;
  43.     int rand3 = (int) (Math.random() * length);
  44.     int length4 = adulteasy.length;
  45.     int rand4 = (int) (Math.random() * length);
  46.     int length5 = adulteasy.length;
  47.     int rand5 = (int) (Math.random() * length);
  48.     int length6 = adulteasy.length;
  49.     int rand6 = (int) (Math.random() * length);
  50.     int length7 = adulteasy.length;
  51.     int rand7 = (int) (Math.random() * length);
  52.     int length8 = adulteasy.length;
  53.     int rand8 = (int) (Math.random() * length);
  54.     int length9 = adulteasy.length;
  55.     int rand9 = (int) (Math.random() * length);
  56.  
  57.     private int currentCard = 1;
  58.     private JPanel cardPanel;
  59.     private CardLayout cl;
  60.  
  61.     public MAINFRAMES() {
  62.  
  63.         setTitle("The Riddle Republic");
  64.         setSize(500, 600);
  65.         cardPanel = new JPanel();
  66.         cardPanel.setLayout(null);
  67.  
  68.         // getContentPane().add(cardPanel);
  69.         cl = new CardLayout();
  70.         cardPanel.setLayout(cl);
  71.  
  72.         JPanel q1 = new JPanel(new GridLayout(1, 0, 0, 0));
  73.         JPanel q2 = new JPanel ();
  74.         JPanel q3 = new JPanel ();
  75.         JPanel q4 = new JPanel ();
  76.         JPanel q5 = new JPanel ();
  77.         JPanel q6 = new JPanel ();
  78.         JPanel q7 = new JPanel ();
  79.         JPanel q8 = new JPanel ();
  80.         JPanel q9 = new JPanel ();
  81.         JPanel q10 = new JPanel ();
  82.  
  83.  
  84.         JLabel qq1 = new JLabel (adulteasy[rand], SwingConstants.CENTER);
  85.         qq1.setHorizontalAlignment(SwingConstants.CENTER);
  86.         qq1.setVerticalAlignment(SwingConstants.CENTER);
  87.         JLabel qq2 = new JLabel (adulteasy[rand1]);
  88.         qq2.setLocation(500, 500);
  89.         JLabel qq3 = new JLabel (adulteasy[rand2]);
  90.         qq3.setLocation(450, 200);
  91.         JLabel qq4 = new JLabel (adulteasy[rand3]);
  92.         qq4.setLocation(450, 200);
  93.         JLabel qq5 = new JLabel (adulteasy[rand4]);
  94.         qq5.setLocation(450, 200);
  95.         JLabel qq6 = new JLabel (adulteasy[rand5]);
  96.         qq6.setLocation(450, 200);
  97.         JLabel qq7 = new JLabel (adulteasy[rand6]);
  98.         qq7.setLocation(450, 200);
  99.         JLabel qq8 = new JLabel (adulteasy[rand7]);
  100.         qq8.setLocation(450, 200);
  101.         JLabel qq9 = new JLabel (adulteasy[rand8]);
  102.         qq9.setLocation(450, 200);
  103.         JLabel qq10 = new JLabel(adulteasy[rand9]);
  104.         qq10.setLocation(450, 200);
  105.  
  106.         q1.add (qq1);
  107.         q2.add (qq2);     
  108.         q3.add (qq3); 
  109.         q4.add (qq4); 
  110.         q5.add (qq5); 
  111.         q6.add (qq6); 
  112.         q7.add (qq7); 
  113.         q8.add (qq8); 
  114.         q9.add (qq9); 
  115.         q10.add (qq10); 
  116.  
  117.  
  118.  
  119.  
  120.         cardPanel.add(q1, "1");
  121.         cardPanel.setBounds (300,300,180, 80);
  122.         cardPanel.add(q2, "2");
  123.         cardPanel.add(q3, "3");
  124.         cardPanel.add(q4, "4");
  125.         cardPanel.add(q5, "5");
  126.         cardPanel.add(q6, "6");
  127.         cardPanel.add(q7, "7");
  128.         cardPanel.add(q8, "8");
  129.         cardPanel.add(q9, "9");
  130.         cardPanel.add(q10, "10");
  131.  
  132.         JPanel buttonPanel = new JPanel();
  133.         JButton firstBtn = new JButton("First");
  134.         JButton nextBtn = new JButton("Next");
  135.         JButton previousBtn = new JButton("Previous");
  136.         JButton lastBtn = new JButton("Last");
  137.         buttonPanel.add(firstBtn);
  138.         buttonPanel.add(nextBtn);
  139.         buttonPanel.add(previousBtn);
  140.         buttonPanel.add(lastBtn);
  141.  
  142.  
  143.         firstBtn.addActionListener(new ActionListener() {
  144.             public void actionPerformed(ActionEvent arg0) {
  145.                 cl.first(cardPanel);
  146.                 currentCard = 1
  147.                 ;
  148.  
  149.             }
  150.  
  151.         });
  152.  
  153.         lastBtn.addActionListener(new ActionListener() {
  154.             public void actionPerformed(ActionEvent arg0) {
  155.                 cl.last(cardPanel);
  156.                 currentCard = 10;
  157.  
  158.             }
  159.         });
  160.  
  161.         nextBtn.addActionListener(new ActionListener() {
  162.             public void actionPerformed(ActionEvent arg0) {
  163.                 if (currentCard < 10) {
  164.                     currentCard += 1;
  165.                     cl.show(cardPanel, "" + (currentCard));
  166.  
  167.                 }
  168.             }
  169.         });
  170.  
  171.         previousBtn.addActionListener(new ActionListener() {
  172.             public void actionPerformed(ActionEvent arg0) {
  173.                 if (currentCard > 1) {
  174.                     currentCard -= 1;
  175.                     cl.show(cardPanel, "" + (currentCard));
  176.                 }
  177.             }
  178.         });
  179.  
  180.         getContentPane().add(cardPanel, BorderLayout.NORTH);
  181.         getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  182.  
  183.     }
  184.     public static void main(String[] args) {
  185.         MAINFRAMES cl = new MAINFRAMES();
  186.         cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  187.         cl.setVisible(true);
  188.  
  189.     }
  190. }
Oct 5 '14 #1
0 1357

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: prabhat | last post by:
Hi, I am getting this weird behavior. I have couple of gif and jpg files that I would like to display in JLabel. It displays one jpg image but it does not display other gifs and jpgs. Any idea why...
1
by: Phil... | last post by:
I have the following code to put a label on a tabbed panel. It does not set the background though. If I put in a "slabel.getBackground()" it returns the color I chose for the set. It is like the...
2
by: reon | last post by:
Hi in this code when i clicked login button...it shows output as login... But when i clicked each time its repeating .. i want to see that only ones ... I want to clear the previous login before...
1
by: tommyny04 | last post by:
I have a method addPlayer that's supposed to add a JLabel to a JPanel and update the JPanel. The JLabel is just a label with a player's name in it. The code I've wrote doesn't work and I'm not sure...
4
by: thesti | last post by:
Hi, i have problem with a JPanel in my JInternalFrame. it's like a maze game. after the game finished, i want to display game's statistic in the same panel i draw the tiles of the maze. but...
8
by: eddiewould | last post by:
Hi, I want to write a custom widget which will act similarly to a JPanel (i.e it can contain other Components), but semantically it's not a kind of JPanel so it shouldn't extend from it. Here is...
6
by: thesti | last post by:
hi, i have a JLabel inside a JPanel of which layout manager is BoxLayout. i will display an image with the JLabel at runtime, when the application first start, it will appear as an empty box...
7
by: KiddoGuy | last post by:
Hello, I'm trying to add an icon to a JPanel in the event where a button is pressed. I have the following code: private void button1ActionPerformed(java.awt.event.ActionEvent evt) { ...
0
Matthewborg
by: Matthewborg | last post by:
I am doing an assignment which it consists of an Applet, where the user has to insert several values and the program has to draw a quadratic graph. In my applet I want to display the graph in the...
2
by: rehfeldtb | last post by:
I am trying to create a simple slot program. I have no problem with getting the initial graphics representing the reels to display. I am having trouble when it comes to changing the reels graphics....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.