【问题标题】:setSize() not working?setSize() 不工作?
【发布时间】:2015-11-12 02:16:12
【问题描述】:

我有一个程序,它有两个按钮,一个是常规按钮,一个是根据鼠标滚动而变化的图片。目前,由于图片很大,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
    }
}





}

【问题讨论】:

  • 是的,我已经看过那个帖子了,但是由于某种原因,setPreferredSize() 也不起作用...?知道为什么吗?
  • @Billybobsteven “知道为什么吗?” - 不是没有一个代码示例来证明它不起作用......另外,你真的想要一个 40, 000 像素的按钮高的?不知道我知道有多少屏幕变得那么大......
  • 每次更改像素尺寸时,此按钮的大小都会保持不变。那 40, 000 像素的高度是我不断改变的大小。将其从 50、50 更改为 100、50 更改为 90、75 等后,按钮和宽度都没有改变。
  • 那是因为FlowLayout 使用组件的preferredSize 来决定组件的布局方式,覆盖您传递给setSize 的任何内容

标签: java swing user-interface button


【解决方案1】:

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

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

那么你通常会得到那个尺寸。

【讨论】:

  • 这个像素大小会重新调整到任何屏幕显示器吗?
  • 组件的大小不会超过您在此处设置的大小,因为调用了setMaximumSize。如果您希望组件根据屏幕监视器大小进行自我调整,那么您不能将这些值硬编码到函数调用中。您需要在程序中计算适当的值并使用它们。
【解决方案2】:

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

现在,在你突然决定布局管理器是一个“坏主意”和“你可以做得更好”之前,请避免使用null/absolute 布局,像素完美布局是现代 UI 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 2012-07-15
      • 2020-05-04
      • 1970-01-01
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-21
      相关资源
      最近更新 更多