Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Button Size

  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Oklahoma City, OK
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Button Size

    I'm trying to set the size of the two buttons that I've created and can't seem to get the setSize() method to work. The purpose of the exercise I'm working on is to create custom exceptions, but I wanted to make it look good as well.

    Any help is appreciated.

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class Payroll extends JFrame{
    JLabel nameLabel = new JLabel("Employee Name:\n");
    JLabel idLabel = new JLabel("Employee ID:\n");
    JLabel payLabel = new JLabel("Rate of Pay:\n");
    JLabel hoursLabel = new JLabel("Hours Worked:\n");
    JTextField jtfName = new JTextField();
    JTextField jtfID = new JTextField();
    JTextField jtfPayRate = new JTextField();
    JTextField jtfHours = new JTextField();
    static JButton jbSubmit = new JButton("Submit");
    static JButton jbClose = new JButton("Close");
    static JPanel jpLabels = new JPanel();
    static JPanel jpInput = new JPanel();
    static JPanel jpTotal = new JPanel();
    String name = "";
    String id = "";
    String strPayRate = "";
    String strHours = "";
    double pay = 0.0;
    double hours = 0.0;
    String n;

    public Payroll() throws InvalidNameException, InvalidIDException{
    nameLabel.setHorizontalAlignment(JLabel.RIGHT);
    idLabel.setHorizontalAlignment(JLabel.RIGHT);
    payLabel.setHorizontalAlignment(JLabel.RIGHT);
    hoursLabel.setHorizontalAlignment(JLabel.RIGHT);



    jpLabels.setLayout(new GridLayout(4, 1));
    jpLabels.add(nameLabel);
    jpLabels.add(idLabel);
    jpLabels.add(payLabel);
    jpLabels.add(hoursLabel);

    jpInput.setLayout(new GridLayout(4, 1));
    jpInput.add(jtfName);
    jpInput.add(jtfID);
    jpInput.add(jtfPayRate);
    jpInput.add(jtfHours);

    jpTotal.setLayout(new GridLayout(2, 2));
    jpTotal.add(jpLabels);
    jpTotal.add(jpInput);
    jpTotal.add(jbSubmit);
    jpTotal.add(jbClose);
    add(jpTotal);

    jbSubmit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
    strPayRate = jtfPayRate.getText();
    strHours = jtfHours.getText();

    try{
    if (jtfName.getText() == null || jtfName.getText().length() == 0){
    throw new InvalidNameException();
    }
    }
    catch(Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered a name.");
    }

    try{
    if (jtfID.getText() == null || jtfID.getText().length() == 0){
    throw new InvalidIDException();
    }
    }
    catch(Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered an ID.");
    }
    try{
    if (strPayRate == null || strPayRate.length() == 0){
    throw new InvalidPayRateException();
    }
    }
    catch (Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered a Pay Rate");
    }

    try{
    if (strHours == null || strHours.length() == 0){
    throw new InvalidHoursException();
    }
    }
    catch (Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered any Hours");
    }

    pay = Double.valueOf(strPayRate.trim()).doubleValue();
    System.out.println(pay);

    try{
    if (pay < 0.0){
    throw new InvalidPayRateException();
    }
    }
    catch(Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered a Pay Rate.");
    }

    hours = Double.valueOf(strHours.trim()).doubleValue();
    System.out.println(hours);

    try{
    if (hours < 0.0){
    throw new InvalidHoursException();
    }
    }
    catch(Exception ex){
    JOptionPane.showMessageDialog(null, "You have not entered any Hours.");
    }
    }
    });
    }

    public static void main(String[] args) throws InvalidNameException, InvalidIDException{
    final Payroll frame = new Payroll();
    frame.setSize(300, 300);
    //frame.pack();
    frame.setTitle("Payroll");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    jbClose.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){frame.dispose();}});
    }
    }



    public class InvalidNameException extends Exception {
    public InvalidNameException(){
    super("Invalid Name");
    }
    }



    public class InvalidPayRateException extends Exception {
    public InvalidPayRateException(){
    super("Invalid Pay Rate");
    }
    }



    public class InvalidHoursException extends Exception {
    public InvalidHoursException(){
    super("Invalid Hours");
    }
    }



    public class InvalidIDException extends Exception {
    public InvalidIDException(){
    super("Invalid ID");
    }
    }

  • #2
    New Coder
    Join Date
    Feb 2010
    Location
    Manchester, UK
    Posts
    69
    Thanks
    0
    Thanked 13 Times in 13 Posts
    your question is googlable:

    use:
    setPreferredSize(new Dimension(100,100));
    it's because of layout managers that setSize() does not work
    read more on layouts plz

    oh, and GridLayout is specific...
    Last edited by ShaneC; 01-24-2011 at 10:47 PM. Reason: Removed hostile comments


  •  

    Posting Permissions

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