简体   繁体   中英

Java: How to compare Dimension objects

I'm trying to set the preferred sizes of my JButtons to the same thing. Basically there are two buttons, and based on which one is larger, I want to set the other to that size.

Problem is, I can't seem to get the operand right for comparing dimension objects. I'll past my code below, but I know the if statements don't work. If someone could just fill me in real quick that would be great.

ballotNameButton = new JButton("<html><center>Edit<br>Ballot Name</center></html>");
        addOfficeButton = new JButton("<html><center>Add<br>Office</center></html>");
        Dimension bnbSize = ballotNameButton.getPreferredSize();
        Dimension aobSize = addOfficeButton.getPreferredSize();
        if(bnbSize > aobSize){
            addOfficeButton.setPreferredSize(bnbSize);
        }else if(aobSize > bnbSize){
            ballotNameButton.setPreferredSize(aobSize);
        }

Anyway, just a quick question. Tried Googling it every which way and couldn't find the answer, I'm sure I'm phrasing something wrong there but help would be appreciated.

You can't use > and < to compare most objects like you mean to. Some objects implement the Comparable interface and can be compared with the compareTo() method. If this is java.awt.Dimension , it does not implement Comparable , so you have to create a custom Comparator and define what fields you want to compare.

For example, if you wanted to compare the objects based on the area, the code would be something like this:

class DimensionComparator implements Comparator<Dimension> {
    private static double area(Dimension d) {
        return d.getWidth() * d.getHeight();
    }

    public int compare(Dimension d1, Dimension d2) {
        return Double.compare(area(d1), area(d2));
    }
}

The method compare() should return:

  • 1 , if d1 is greater than d2
  • 0 , if d1 is equal to d2
  • -1 , if d1 is less than d2

Then you should create the comparator instance inside your class:

private static Comparator<Dimension> dimComparator = new DimensionComparator();

And use compare() to compare the objects:

if (dimComparator.compare(bnbSize, aobSize) > 0) {
    addOfficeButton.setPreferredSize(bnbSize);
} else if (dimComparator.compare(aobSize, bnbSize) > 0) {
    ballotNameButton.setPreferredSize(aobSize);
}

If you just want to compare a specific field, such as height, just do:

if (bnbSize.getHeight() > aobSize.getHeight()) {
    addOfficeButton.setPreferredSize(bnbSize);
} else if (aobSize.getHeight() > bnbSize.getHeight()) {
    ballotNameButton.setPreferredSize(aobSize);
}

A Dimension object has both width and height. You can compare the width or height to each other but what would it mean to compare both dimensions at once? For example, if one is 2x1 and the other is 1x2, which is larger? They have the same area, but one is taller and the other is wider.

For your application I expect you're either interested in the height or width, so you could change your if-statements to focus on that. For example:

if (bnbSize.width > aobSizel.width)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM