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;


}

4

6 回答 6

1

我不确定重新定义init方法是否是个好主意。当我查看http://jtf.acm.org/javadoc/student/acm/program/ConsoleProgram.html我希望您只实现运行方法。在不调用 super.init() 的情况下覆盖 init 对我来说看起来很奇怪。

也许我会更好地直接从 JApplet 派生您在 Applet 编程的第一步。

于 2013-01-12T20:14:55.427 回答
0

您是否尝试过在 init() 方法开始时调用 super.init() ?

于 2013-01-18T01:09:50.153 回答
0

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

于 2013-02-16T23:36:11.600 回答
0

假如说

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

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

无论如何,代码中几乎没有“不好”的东西:

  1. 首先,命名约定问题:类名应该是“Button”,首字母大写(除此之外,这对于 Applet 来说是个糟糕的名称)
  2. 其次,在添加组件之前应该附加动作监听器;
  3. 第三,正如 Oracle 文档在这里所建议的那样,构建 GUI 的代码应该是在事件调度程序线程上运行的作业。您可以通过使用 SwingUtilities.invokeAndWait(Runnable() 将构建 gui 代码包装在 Runnable 中来做到这一点
于 2013-01-03T21:36:28.603 回答
-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);
   }
}
于 2013-01-06T09:49:40.023 回答
-2

你有没有声明重绘方法...???

您正在使用摆动。它需要声明重绘。

请定义自定义重绘 mwthod

于 2012-09-06T09:47:41.867 回答