setSize()不起作用?

时间:2015-08-18 23:47:07

标签: java swing user-interface button

我有一个程序,它带有两个按钮,一个是常规的,另一个是根据鼠标滚动而改变的图片。目前,由于图片很大,JButton自定义也非常大,我可以更改自定义的大小并保持图像(和滚动图像)成比例吗?我尝试过setSize,但它没有做任何事情。任何反馈都将不胜感激!

 custom.setSize(50, 50);

以下是我的所有代码:

主要课程:

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);
    }
}

其他课程:

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
    }
}





}

3 个答案:

答案 0 :(得分:1)

正如一些用户所提到的,您可以覆盖getPreferredSize()getMinimum/MaximumSize()。或者您可以调用setter方法来选择首选,最大和最小尺寸。如果设置首选大小和最大大小......

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

那么你通常会得到那么大的。

答案 1 :(得分:0)

首先看一下Laying Out Components Within a Container。在Swing和许多其他UI框架中,您实际上并不控制组件的大小或位置,而是通过不同的机制提供提示和约束,允许布局管理员在您之前做出最终决策。

现在,在您突然认定布局管理员是“坏主意”和“您可以做得更好”之前,避免使用null /绝对布局,像素完美布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正

如果您“确实”想要影响组件的大小,那么您应该覆盖getPreferredSize(和/或getMinimumSize / getMaximumSize)方法并返回所需的值。请注意,这些只是提示,一些布局管理器会忽略它们(或者仅用于初始计算)

答案 2 :(得分:0)

如果使用BorderLayout并在具有边框布局的容器上调用setSize方法,则实际上可以使setSize起作用。 FlowLayout很糟糕,而且很难控制。除非我对GridLayout有特定的需求,否则我对所有容器都使用BorderLayout。

BorderLayout的优点是您不需要包括所有区域。如果要一个大面板和一个小底面板,只需将大面板放在BorderLayout.CENTER中,将小面板放在BorderLayout.SOUTH中。南面板将足够大以容纳其元素,而中心将占据所有剩余空间。整个容器的大小实际上就是您要说的大小!您甚至可以只在中心区域使用BorderLayout。