【问题标题】:set location on event doesn't work在事件上设置位置不起作用
【发布时间】:2017-12-16 10:45:16
【问题描述】:

我有以下代码,我尝试使一些 jLabels/jComboBox 可见/不可见,并移动那些可见的位置 在 jRadioButton 点击​​时。

问题是位置仅在第二次点击时更新。

    private void SingleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

   this.serverLabel.setVisible(true);
   this.serverList.setVisible(true);


   this.serverLabel.setLocation(this.hostGroupLabel.getLocation().x, this.cpuCountSpinner.getLocation().y);
   this.serverList.setLocation(this.cpuCountSpinner.getLocation().x, this.cpuCountSpinner.getLocation().y);

   this.jXDatePickerStartDate.setLocation(153, jXDatePickerStartDate.getLocation().y);

   this.requestedRamLabel.setVisible(false);
   this.ramText.setVisible(false);
   this.cpuLabel.setVisible(false);
   this.cpuCountSpinner.setVisible(false);

}  

【问题讨论】:

  • 您可能想看看this answer,即使它可能无法解决您的问题。也只有我们不能真正帮助你的行动。发布更多代码
  • 我正在检测 JRadioButton 状态变化,但正如我提到的,只有在他已经点击后再次点击按钮时,位置正在改变。
  • 1) 您可能需要重新绘制框架。 2) 永远不要使用 setLocation 方法。请改用适当的布局管理器。
  • 您是否尝试过调用revalidate 触发新的布局通道和repaint 触发新的绘制通道
  • 当我使用 revalidate 然后重新绘制它根本不起作用。

标签: java swing layout-manager


【解决方案1】:

我不知道为什么它对你不起作用,也许你正在其他地方修改你的 GUI,或者根本没有触发事件。这是工作示例。您应该将代码粘贴到实际将侦听器附加到按钮的位置。你做对了吗?它应该是这样的:

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            singleButtonActionPerformed(e)
        }
    });

或使用 Lambda 的 Java8 变体

button.addActionListener(this::singleButtonActionPerformed)

但使用this 取决于上下文。它应该是 hta 持有给定方法的对象。

这是带有按钮和单选按钮的工作示例(按照建议)

公共类 SettingLocation {

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    f.setContentPane(p);

    p.setLayout(new FlowLayout());

    JButton b = new JButton("Click me");
    b.setBounds(150, 150,100,100);
    p.add(b);

    JRadioButton rb=new JRadioButton("Exmaple radio");
    p.add(rb);
    rb.addActionListener(new ActionListener() {
        Random r = new Random();

        @Override
        public void actionPerformed(ActionEvent e) {
            rb.setLocation(r.nextInt(300), r.nextInt(300));
        }
    });

    b.addActionListener(new ActionListener() {
        Random r = new Random();

        @Override
        public void actionPerformed(ActionEvent e) {
            b.setLocation(r.nextInt(300), r.nextInt(300));
        }
    });
    f.setVisible(true);

}
}

【讨论】:

  • JRadioButton 做例子,因为这似乎是问题
  • @XtremeBaumer ezpz - 完成,没有问题 - 单选按钮添加了更改位置。我敢打赌 OP 没有在任何地方附加动作侦听器,因为他只展示了他的一些假设处理事件的方法,但他没有展示他是如何附加它的。
  • 由于 p 使用的是 FlowLayout,因此在重新验证容器时,您对位置所做的任何修改都将无效 - 尝试调整窗口大小,看看会发生什么
  • @MadProgrammer 它按原样工作。将布局管理器设置为 null 也可以 - 事先为两个组件设置适当的边界。
  • 重新验证容器,或者通过调用revalidate调整窗口大小,组件将根据布局管理器的需要重新定位。避免使用null 布局,像素完美的布局是现代 ui 设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多