Results 1 to 3 of 3
  1. #1
    karq is offline Member
    Join Date
    May 2010
    Posts
    63
    Rep Power
    0

    Default JButton dosent align center

    Hey everyone!!

    My problem:

    I have a jButton added to my layout and its aligned center, but when I add a jLabel, then the button aligns automatically left or right and I cant get it to center, how can I fix this?

    Here are my jlabel and jButton codes:

    jLabel:
    Java Code:
    ImageIcon aboutLogo = new ImageIcon(getClass().getResource("/myapp/appresources/24_save.png"));
    String labelText ="<html><center><h1>My app</h1><br />" +
     "this is some text ajeee " +
     "Here is some more text<br />" +
     " and some more text</center></html>";
    jAboutText = new javax.swing.JLabel(labelText,aboutLogo,JLabel.CENTER);
    jAboutText.setVerticalTextPosition(JLabel.BOTTOM);
    jAboutText.setHorizontalTextPosition(JLabel.CENTER);
    jButton:
    Java Code:
    jAboutOKButton = new javax.swing.JButton();
    
    jAboutOKButton.setText("OK");
    jAboutOKButton.setAlignmentX(JButton.CENTER_ALIGNMENT);
    jAboutOKButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jAboutOKButtonActionPerformed(evt);
        }
    });

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    setAlignment is not doing what you think it's doing (check the API to see what it does). You need to instead read up and use appropriate layout managers.

  3. #3
    deepaksurthi is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Layout

    Hi

    in order to set button anywhere in your frame . then you need to work with LayoutManager.

    Java Code:
    JPanel panel = new JPanel();
    
    panel.setLayout(null);
    
    JButton button = new JButton("any button");
    
    button.setBounds(x,y,height,width); //(10,10,100,25);
    
    panel.add(button);
    frame.add(panel);

Similar Threads

  1. Why dosent my test run?!?
    By karq in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 11:50 PM
  2. Why dosent return new string
    By karq in forum New To Java
    Replies: 8
    Last Post: 08-04-2010, 09:51 AM
  3. Java: How do i align the text in the center?
    By nicacutenica in forum New To Java
    Replies: 5
    Last Post: 06-29-2010, 07:50 PM
  4. why dosent the 2d array change?
    By TheBreadCat in forum New To Java
    Replies: 11
    Last Post: 05-15-2010, 02:20 PM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •