Topic: setVisable()的问题

  Print this page

1.setVisable()的问题 Copy to clipboard
Posted by: Pink
Posted on: 2005-10-31 16:42

不知道怎么回事,setVisable(true)放在setSize()后面就能显示Button,在前面就不行


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

public class EventTest extends JFrame implements ActionListener{
  private JButton testJButton;
  public EventTest(){
    super("Event Test");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    testJButton = new JButton("JButton");
    testJButton.addActionListener(this);
    container.add(testJButton);
    setLocation(400, 300);
    //setVisible(true);
    setSize(200,72);
    setVisible(true);
  }

  public static void main(String[] args) {
    EventTest eventTest = new EventTest();
    eventTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  public void actionPerformed(ActionEvent event){
    JOptionPane.showMessageDialog(this, "JButton clicked");
  }  
}

2.Re:setVisable()的问题 [Re: Pink] Copy to clipboard
Posted by: bluecrystal
Posted on: 2005-10-31 16:48

我给挪到gui版了
Smile

3.Re:setVisable()的问题 [Re: Pink] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-11-06 17:25

To Pink
The default JFrame size is icon. That is why. However, you don't need to setSize(), you can use pack() to let Swing handle it by itself.

I modified your code to illustrate the concepts better. More buttons are added.


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

public class EventTest extends JFrame implements ActionListener{
private JButton bn1, bn2, bn3;
public EventTest(){
super("Event Test");
Container container = getContentPane();
container.setLayout(new FlowLayout());
bn1 = new JButton("JButton 1");
bn2 = new JButton("JButton 2");
bn3 = new JButton("JButton 3");
bn1.addActionListener(this);
bn2.addActionListener(this);
bn3.addActionListener(this);
container.add(bn1);
container.add(bn2);
container.add(bn3);
setLocation(400, 300);
pack();
setVisible(true);
}

public static void main(String[] args) {
EventTest eventTest = new EventTest();
eventTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent event){
Object o = event.getSource();
if (o instanceof JButton) {
JButton bn = (JButton)o;
JOptionPane.showMessageDialog(this, bn.getText() + " clicked");
}
}
}

4.Re:setVisable()的问题 [Re: Pink] Copy to clipboard
Posted by: Pink
Posted on: 2005-11-08 11:28

谢谢两位,不过还是没明白,默认大小是icon,为什么就不显示按钮呢,好像只要用鼠标点一下窗口的边界或改变一下窗口大小就显示按钮了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923