调整大小时,JButton会消失

时间:2012-09-01 23:29:06

标签: java swing jbutton

任何人都知道或知道为什么我的按钮在调整applet大小后会消失?

这是我的代码:


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;


}

6 个答案:

答案 0 :(得分:1)

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

也许我会更好地直接从JApplet派生出来进行Applet编程的第一步。

答案 1 :(得分:0)

假设

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

代码应该工作,不需要重绘(除非你想做一些特定于应用程序的优化)。我只是复制并粘贴了你的代码(通过解释上面的两个假设),我看到applet和按钮在调整大小时不会消失。

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

  1. 首先,一个命名约定问题:类名应该是“Button”,第一个字母大写(在它之上,它是一个可怜的Applet名称)
  2. 其次,在添加组件之前应该附加动作侦听器;
  3. 第三,正如Oracle doc建议here,构建GUI的代码应该是在事件调度程序线程上运行的作业。你可以通过使用SwingUtilities.invokeAndWait(Runnable()
  4. 将build gui代码包装在Runnable中来实现。

答案 2 :(得分:0)

您是否尝试在init()方法的开头调用super.init()?

答案 3 :(得分:0)

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

答案 4 :(得分:-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);
   }
}

答案 5 :(得分:-2)

你宣布了重绘方法...... ???

你正在使用秋千。它需要宣布重画。

请定义自定义重绘mwthod