10 Replies - 1284 Views - Last Post: 06 February 2010 - 09:22 PM Rate Topic: -----

#1 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Setting JButton sizes

Posted 06 February 2010 - 05:19 PM

I tried to set he size of a JButton by using null layout of the button's container and then setting bounds and size of the button. However, the button is nowhere on the screen.

Anyone know how to solve this?
Is This A Good Question/Topic? 0
  • +

Replies To: Setting JButton sizes

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Setting JButton sizes

Posted 06 February 2010 - 05:20 PM

Did you do setVisible(true)?

Could we see your code please?
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Setting JButton sizes

Posted 06 February 2010 - 05:44 PM

Better Question, did you add() it?

Is it being covered up by anything else?

And like Erik said, did you setVisible(true)?

All these questions will help us help you, along with seing the code you are trying to use.
Was This Post Helpful? 0
  • +
  • -

#4 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Setting JButton sizes

Posted 06 February 2010 - 05:56 PM

code for the button is:

JFrame mainFrame = new JFrame("Test");

JPanel numberPane = new Panel(null);
mainFrame.add(numberPane);

JButton one = new JButton("1");
one.setSize(50,50);
numberPane.add(one);



Was This Post Helpful? 0
  • +
  • -

#5 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Setting JButton sizes

Posted 06 February 2010 - 05:59 PM

Change this line:
JPanel numberPane = new Panel(null);

to this:
JPanel numberPane = new JPanel(null);

Otherwise it won't compile


Also, add this line:
mainFrame.setVisible(true);

and it will work fine. (the frame's a bit small however)

This post has been edited by erik.price: 06 February 2010 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Setting JButton sizes

Posted 06 February 2010 - 06:09 PM

View Posterik.price, on 06 February 2010 - 04:59 PM, said:

Change this line:
JPanel numberPane = new Panel(null);

to this:
JPanel numberPane = new JPanel(null);

Otherwise it won't compile


Also, add this line:
mainFrame.setVisible(true);

and it will work fine. (the frame's a bit small however)


i already made the frame visible later in the code...still not working
tried to make button and pane visible but not workig either
Was This Post Helpful? 0
  • +
  • -

#7 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Setting JButton sizes

Posted 06 February 2010 - 06:11 PM

It works fine for me. The only thing I changed was this:
JPanel numberPane = new Panel(null);

to this:
JPanel numberPane = new JPanel(null);


Are you getting any compile errors, or will it just not show?

This post has been edited by erik.price: 06 February 2010 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Setting JButton sizes

Posted 06 February 2010 - 06:12 PM

Something else may be taking the same space on the content pane. Do you mind showing us your full code! Problems can happen anywhere!
Was This Post Helpful? 0
  • +
  • -

#9 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Setting JButton sizes

Posted 06 February 2010 - 06:27 PM

sure here it is, its a computer project for school

im trying to make the number buttons custom in size


import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.undo.*;
import java.io.*;

public class CheckoutPro extends JPanel implements ActionListener
{
  private static JFrame mainFrame = new JFrame("Checkout PRO");
  private static JTextField barcodeField;
  private static JTextArea sales;
  
  public CheckoutPro()
  {
    JMenuBar menuBar = new JMenuBar();
    mainFrame.setJMenuBar(menuBar);
    
    JMenu file = new JMenu("File");
    JMenu preferences = new JMenu("Preferences");
    JMenu about = new JMenu("About");
    menuBar.add(file);
    menuBar.add(preferences);
    menuBar.add(about);
    
    JMenuItem newMI = new JMenuItem("New");
    newMI.setMnemonic(KeyEvent.VK_D);
    newMI.setActionCommand("New");
    newMI.addActionListener(this);
    JMenuItem open = new JMenuItem("Open");
    open.setMnemonic(KeyEvent.VK_D);
    open.setActionCommand("Open");
    open.addActionListener(this);
    JMenuItem print = new JMenuItem("Print");
    print.setMnemonic(KeyEvent.VK_D);
    print.setActionCommand("Print");
    print.addActionListener(this);
    JMenuItem quit = new JMenuItem("Quit");
    quit.setMnemonic(KeyEvent.VK_D);
    quit.setActionCommand("Quit");
    quit.addActionListener(this);
    file.add(newMI);
    file.add(open);
    file.add(print);
    file.add(quit);
    
    JMenuItem options = new JMenuItem("Options");
    options.setMnemonic(KeyEvent.VK_D);
    options.setActionCommand("Options");
    options.addActionListener(this);
    preferences.add(options);
    
    JMenuItem info = new JMenuItem("Info");
    info.setMnemonic(KeyEvent.VK_D);
    info.setActionCommand("Info");
    info.addActionListener(this);
    about.add(info);
    
    JPanel mainPane = new JPanel(new GridLayout(2,1));
    mainFrame.add(mainPane);
    
    JPanel upperPane = new JPanel(new GridLayout(1,2));
    mainPane.add(upperPane);
    
    JPanel lowerPane = new JPanel();
    mainPane.add(lowerPane);
    
    JPanel borderPane = new JPanel(new FlowLayout());
    upperPane.add(borderPane);
    
    JPanel numberPane = new JPanel(null);
    borderPane.add(numberPane);
    
    JPanel functionPane = new JPanel(new GridLayout(5,6));
    lowerPane.add(functionPane);
    
    sales = new JTextArea();
    sales.setEditable(false);
    upperPane.add(sales);
    
    barcodeField = new JTextField(20);
    numberPane.add(barcodeField);
    
    JButton one = new JButton("1");
    one.setMnemonic(KeyEvent.VK_D);
    one.setActionCommand("one");
    one.addActionListener(this);
    one.setSize(50,50);
    numberPane.add(one);
    
    JButton two = new JButton("2");
    two.setMnemonic(KeyEvent.VK_D);
    two.setActionCommand("two");
    two.addActionListener(this);
    numberPane.add(two);
    
    JButton three = new JButton("3");
    three.setMnemonic(KeyEvent.VK_D);
    three.setActionCommand("three");
    three.addActionListener(this);
    numberPane.add(three);
    
    JButton four = new JButton("4");
    four.setMnemonic(KeyEvent.VK_D);
    four.setActionCommand("four");
    four.addActionListener(this);
    numberPane.add(four);
    
    JButton five = new JButton("5");
    five.setMnemonic(KeyEvent.VK_D);
    five.setActionCommand("five");
    five.addActionListener(this);
    numberPane.add(five);
    
    JButton six = new JButton("6");
    six.setMnemonic(KeyEvent.VK_D);
    six.setActionCommand("six");
    six.addActionListener(this);
    numberPane.add(six);
    
    JButton seven = new JButton("7");
    seven.setMnemonic(KeyEvent.VK_D);
    seven.setActionCommand("seven");
    seven.addActionListener(this);
    numberPane.add(seven);
    
    JButton eight = new JButton("8");
    eight.setMnemonic(KeyEvent.VK_D);
    eight.setActionCommand("eight");
    eight.addActionListener(this);
    numberPane.add(eight);
    
    JButton nine = new JButton("9");
    nine.setMnemonic(KeyEvent.VK_D);
    nine.setActionCommand("nine");
    nine.addActionListener(this);
    numberPane.add(nine);
    
    JButton zero = new JButton("0");
    zero.setMnemonic(KeyEvent.VK_D);
    zero.setActionCommand("zero");
    zero.addActionListener(this);
    numberPane.add(zero);
    
    JButton backSpace = new JButton();
    backSpace.setMnemonic(KeyEvent.VK_D);
    backSpace.setActionCommand("backSpace");
    backSpace.addActionListener(this);
    numberPane.add(backSpace);
    
  }
  
  public void actionPerformed(ActionEvent e)
  {
    if("New".equalsIgnoreCase(e.getActionCommand()))
    {
      
    }
    if("Open".equalsIgnoreCase(e.getActionCommand()))
    {
      
    }
    if("Print".equalsIgnoreCase(e.getActionCommand()))
    {
      
    }
    if("Quit".equalsIgnoreCase(e.getActionCommand()))
    {
      int n = JOptionPane.showConfirmDialog(mainFrame,"Do you really want to quit?", "Quit", JOptionPane.YES_NO_OPTION);
      if(n == 0)
      {
        System.exit(0);
      }
    }
    if("Options".equalsIgnoreCase(e.getActionCommand()))
    {
      
    }
    if("Info".equalsIgnoreCase(e.getActionCommand()))
    {
      JOptionPane.showMessageDialog(mainFrame,"CheckoutPro created by Bliss Chang\nFeb. 6, 2010\nV.1.0","Checkout Pro v.1.0",JOptionPane.INFORMATION_MESSAGE);
    }
    if("one".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "1");
    }
    if("two".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "2");
    }
    if("three".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "3");
    }
    if("four".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "4");
    }
    if("five".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "5");
    }
    if("six".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "6");
    }
    if("seven".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "7");
    }
    if("eight".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "8");
    }
    if("nine".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "9");
    }
    if("zero".equalsIgnoreCase(e.getActionCommand()))
    {
      barcodeField.setText(barcodeField.getText() + "0");
    }
    if("backSpace".equalsIgnoreCase(e.getActionCommand()))
    {
      try
      {
        String original = barcodeField.getText();
        barcodeField.setText(original.substring(0,original.length() - 1));
      }
      catch(StringIndexOutOfBoundsException ee)
      {
      }
    }
    
  }
  
  public static void createGUI()
  {
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setSize(800,800);
    mainFrame.setLocation(360,20);
    
    CheckoutPro programFrame = new CheckoutPro();
    programFrame.setOpaque(true);
    
    mainFrame.setVisible(true);
  }
  
  public static void main(String[] args)
  {
    createGUI();
  }
}




compiles fine for me
Was This Post Helpful? 0
  • +
  • -

#10 bcranger   User is offline

  • D.I.C Lover
  • member icon

Reputation: 253
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Setting JButton sizes

Posted 06 February 2010 - 07:26 PM

bump
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Setting JButton sizes

Posted 06 February 2010 - 09:22 PM

Your problem seems to be your wierd organization for your JPanels. Unless I'm using CardLayout, I try to stick with one JPanel per frame so as to avoid having multiple JPanels competing for space (as is happening here). Also, why does your class extend JPanel if you don't add anything to it? The whole point of a JPanel is to contain other GUI Components. I think you might want to completely redo your GUI organization to focus around one JPanel. You might find GridBagLayout helpful as it gives you a lot of control over the positioning of your components.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1