当调整大小时JButton消失了

8
有人知道为什么我调整小程序大小后,我的按钮会消失吗?
这是我的代码:

import java.awt.event.*;
import javax.swing.*;
import acm.program.*;

public class button extends ConsoleProgram {
public void init(){
hiButton = new JButton("hi"); add(hiButton, SOUTH); addActionListeners();
}
public void actionPerformed(ActionEvent e){ if(hiButton == e.getSource()){ println("hello") ; }
} private JButton hiButton;
}

你确定这是一个 applet 吗?它是 JApplet 吗? - obataku
是的,顶部显示它是一个小程序。但是,它确实是一个ACM程序。 - Elias Sanchez
闪烁还是完全消失? - Jimmt
这不是作业,只是通过斯坦福大学的YouTube频道学习Java。无论如何,似乎答案并不是使用ACM lol.. - Elias Sanchez
你可以尝试在Java 1.5下运行它,就像他们在这里所做的一样(链接:https://dev59.com/wWfWa4cB1Zd3GeqPjrQq)。 - Catalina Island
@AndrewThompson,“作业”标签已被弃用,应逐步淘汰。 - Vala
6个回答

1

我不确定重新定义init方法是否是一个好主意。当我查看http://jtf.acm.org/javadoc/student/acm/program/ConsoleProgram.html时,我期望你只实现run方法。覆盖init而不调用super.init()对我来说看起来很奇怪。

也许对于你在Applet编程中的第一步来说,直接从JApplet派生会更好。


0
你尝试过在init()方法开头调用super.init()吗?

0

尝试明确为您的控制台使用布局,然后使用相对定位。


0

假设

  • 您的ConsoleProgram扩展(直接或间接)JApplet
  • 您将SOUTH声明为具有值BorderLayout.SOUTH的静态final变量(否则您的代码不会编译)

代码应该可以工作,无需重新绘制(除非您想进行一些特定于应用程序的优化)。我只是复制并粘贴了您的代码(通过明确上述两个假设),我看到小程序和按钮在调整大小时不会消失。

无论如何,代码中有一些“不好”的地方:

  1. 首先,命名约定问题:类名应为“Button”,第一个字母大写(此外,这是一个糟糕的小程序名称)
  2. 其次,在添加组件之前应附加操作侦听器;
  3. 第三,正如Oracle文档建议here,构建GUI的代码应该是在事件分派线程上运行的作业。您可以通过使用SwingUtilities.invokeAndWait(Runnable()将构建gui代码包装在Runnable中来实现

-1

调整 Applet 中按钮的大小:

public class Button extends JApplet implements ActionListener {

   private JButton button;

   public void init() {
      Container container = getContentPane();
      container.setLayout(null);
      container.setBackground(Color.white);
      button = new JButton("Press Me");
      button.setSize(getWidth()/2,20);
      button.setLocation(getWidth()/2-button.getSize().width/2, getHeight()/2-button.getSize().height/2);
      container.add(button);
      button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
      int width = (button.getSize().width == getWidth()/2) ? getWidth()/4 : getWidth()/2;
      int height = button.getSize().height;
      button.setSize(width,height);
      button.setLocation(getWidth()/2-width/2, getHeight()/2-height/2);
   }
}

调整JFrame中按钮的大小:

public class Button extends JFrame implements ActionListener {
   private JButton button;

   public Button(String title) {
      Container container = getContentPane();
      container.setLayout(null);
      container.setBackground(Color.white);
      setTitle(title);
      setSize(400,400);
      button = new JButton("Press Me");
      button.setSize(getWidth()/2,20);
      button.setLocation(getWidth()/2-button.getSize().width/2,
                     getHeight()/2-button.getSize().height/2);
      container.add(button);
      button.addActionListener(this);
    }

   public void actionPerformed(ActionEvent e) {
      int width = (button.getSize().width == getWidth()/2) ? getWidth()/4 : getWidth()/2;
      int height = button.getSize().height;
      button.setSize(width,height);
      button.setLocation(getWidth()/2-width/2, getHeight()/2-height/2);
   }

   public static void main(String[] args) {
      Button button = new Button("Test");
      button.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      button.setVisible(true);
   }
}

-1 用于推荐空布局(即手动调整组件大小/位置):无论问题是什么,那都不是Swing解决问题的方式。 - kleopatra
@kleopatra 使用“空布局”进行“绝对定位”的问题是什么? 文档中有说明(http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html),我的代码是一个例子。 - Ahmed Hassanien
主要问题在于,当任何影响布局的子元素的任何属性发生变化时,您必须重新执行所有计算 - 这正是管理器所做的。手动执行不仅浪费时间(您必须重新编写大量代码),而且很难正确完成:您需要监听所有这些更改,但仍可能无法收到所有通知(例如缺少手动重新验证)。 - kleopatra

-2

你是否声明了repaint方法...???

你正在使用swing。它需要声明一个repaint。

请定义一个自定义的repaint方法


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接