setSize() not working?

筅森魡賤 提交于 2021-01-27 18:29:10

问题


I have a program which takes two buttons, one that is regular and one that has a picture that changes depending on mouse roll over. Currently, since the picture is large, JButton custom is very large as well, can I change the size of custom and keep the image (and roll over image) proportionate? I have tried setSize, and it does't do anything. Any feedback would be appreciated!

 custom.setSize(50, 50);

Here is all of my code:

Main class:

package Buttons;

import javax.swing.JFrame;

public class Main_buttons{

public static void main(String[] args) {

    ButtonClass press = new ButtonClass();
    press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    press.setSize(500,500);
    press.setVisible(true);
    }
}

Other class:

package Buttons;

import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JButton; //BUTTON!!!
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box

public class ButtonClass extends JFrame {

private JButton regular;
private JButton custom;

public ButtonClass() { // Constructor
    super("The title"); // Title
    setLayout(new FlowLayout()); // Default layout

    regular = new JButton("Regular Button");
    add(regular);

    Icon b = new ImageIcon(getClass().getResource("img.png"));
    Icon x = new ImageIcon(getClass().getResource("swag.png"));
    custom = new JButton("Custom", b);
    custom.setRolloverIcon(x); //When you roll over the button that says custom the image will change from b to x
    custom.setSize(50, 50);
    add(custom);

    Handlerclass handler = new Handlerclass();
    regular.addActionListener(handler);
    custom.addActionListener(handler);


}

public class Handlerclass implements ActionListener { // This class is inside the other  class


    public void actionPerformed(ActionEvent eventvar) { // This will happen
                                                        // when button is
                                                        // clicked
        JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand()));//Opens a new window with the name of the button
    }
}





}

回答1:


As some users have mentioned, you can override getPreferredSize() and getMinimum/MaximumSize(). Or you could just call the setter methods for the preferred, maximum, and minimum sizes. If you set the preferred size and the maximum size...

custom.setPreferredSize(new Dimension(50, 50));
custom.setMaximumSize(new Dimension(50, 50));

then you will usually get that size.




回答2:


Start by having a look at Laying Out Components Within a Container. In Swing, and many other UI frameworks, you don't actually control the size or position of components, but instead, through different mechanisms, provide hints and constraints that allow layout managers to make the final decisions before you.

Now, before you suddenly decide that layout managers a "bad idea" and "you can do better", avoid using null/absolute layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

If you "really" want to effect the size of a component, then you should override the getPreferredSize (and/or getMinimumSize/getMaximumSize) method and return your required values. Beware though, these are only hints and some layout managers will ignore them (or only use them for their initial calculations)




回答3:


You can actually get setSize to work if you use BorderLayout and call the setSize method on the container that has the border layout. FlowLayout is terrible and very hard to control. I use BorderLayout for all containers unless I have a specific need for GridLayout.

The advantage of BorderLayout is that you don't need to include all the regions. If you want one large panel and one small bottom panel, just put the large one in BorderLayout.CENTER and the small one in BorderLayout.SOUTH. The south panel will be just big enough to contain its elements while the center will occupy all remaining space. And the container as a whole really will be the size you say you want it to be! You can even use BorderLayout with only the center region.



来源:https://stackoverflow.com/questions/32084346/setsize-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!