当前位置: 技术问答>java相关
初学java一个弱智问题:如何隐藏JButton
来源: 互联网 发布时间:2017-04-06
本文导语: import java.awt.*; import java.awt.event.*; import javax.swing.*; import MyWindowListener; public class HideButton extends JFrame { public JButton east,west,south,north,centre; public Container c; public HideButton() { JButton east = new JButton(...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import MyWindowListener;
public class HideButton extends JFrame
{
public JButton east,west,south,north,centre;
public Container c;
public HideButton()
{
JButton east = new JButton("隐藏东方");
JButton west = new JButton("隐藏西方");
JButton south = new JButton("隐藏南方");
JButton north = new JButton("隐藏北方");
JButton centre = new JButton("隐藏中央");
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(east,BorderLayout.EAST);
c.add(south,BorderLayout.SOUTH);
c.add(west,BorderLayout.WEST);
c.add(north,BorderLayout.NORTH);
c.add(centre,BorderLayout.CENTER);
east.addActionListener(new ButtonListener());
south.addActionListener(new ButtonListener());
west.addActionListener(new ButtonListener());
north.addActionListener(new ButtonListener());
centre.addActionListener(new ButtonListener());
setSize(300,200);
show();
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command.equals("隐藏东方"))
east.setVisible(false);
if(command.equals("隐藏南方"));
south.setVisible(false);
if(command.equals("隐藏西方"));
west.setVisible(false);
if(command.equals("隐藏北方"));
north.setVisible(false);
if(command.equals("隐藏中央"));
centre.setVisible(false);
}
}
public static void main(String[] args)
{
HideButton app = new HideButton();
app.addWindowListener(new MyWindowListener());
}
}
//编译可以通过,但是运行时不正常,估计是setVisible的问题
import java.awt.event.*;
import javax.swing.*;
import MyWindowListener;
public class HideButton extends JFrame
{
public JButton east,west,south,north,centre;
public Container c;
public HideButton()
{
JButton east = new JButton("隐藏东方");
JButton west = new JButton("隐藏西方");
JButton south = new JButton("隐藏南方");
JButton north = new JButton("隐藏北方");
JButton centre = new JButton("隐藏中央");
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(east,BorderLayout.EAST);
c.add(south,BorderLayout.SOUTH);
c.add(west,BorderLayout.WEST);
c.add(north,BorderLayout.NORTH);
c.add(centre,BorderLayout.CENTER);
east.addActionListener(new ButtonListener());
south.addActionListener(new ButtonListener());
west.addActionListener(new ButtonListener());
north.addActionListener(new ButtonListener());
centre.addActionListener(new ButtonListener());
setSize(300,200);
show();
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command.equals("隐藏东方"))
east.setVisible(false);
if(command.equals("隐藏南方"));
south.setVisible(false);
if(command.equals("隐藏西方"));
west.setVisible(false);
if(command.equals("隐藏北方"));
north.setVisible(false);
if(command.equals("隐藏中央"));
centre.setVisible(false);
}
}
public static void main(String[] args)
{
HideButton app = new HideButton();
app.addWindowListener(new MyWindowListener());
}
}
//编译可以通过,但是运行时不正常,估计是setVisible的问题
|
你的代码写得太麻烦了,帮你改改,如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HideButton extends JFrame
{
public JButton east,west,south,north,centre;
public Container c;
public HideButton()
{
JButton east = new JButton("隐藏东方");
JButton west = new JButton("隐藏西方");
JButton south = new JButton("隐藏南方");
JButton north = new JButton("隐藏北方");
JButton centre = new JButton("隐藏中央");
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(east,BorderLayout.EAST);
c.add(south,BorderLayout.SOUTH);
c.add(west,BorderLayout.WEST);
c.add(north,BorderLayout.NORTH);
c.add(centre,BorderLayout.CENTER);
east.addActionListener(new ButtonListener());
south.addActionListener(new ButtonListener());
west.addActionListener(new ButtonListener());
north.addActionListener(new ButtonListener());
centre.addActionListener(new ButtonListener());
setSize(300,200);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
show();
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
((JButton)e.getSource()).setVisible(false);
}
}
public static void main(String[] args)
{
HideButton app = new HideButton();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HideButton extends JFrame
{
public JButton east,west,south,north,centre;
public Container c;
public HideButton()
{
JButton east = new JButton("隐藏东方");
JButton west = new JButton("隐藏西方");
JButton south = new JButton("隐藏南方");
JButton north = new JButton("隐藏北方");
JButton centre = new JButton("隐藏中央");
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(east,BorderLayout.EAST);
c.add(south,BorderLayout.SOUTH);
c.add(west,BorderLayout.WEST);
c.add(north,BorderLayout.NORTH);
c.add(centre,BorderLayout.CENTER);
east.addActionListener(new ButtonListener());
south.addActionListener(new ButtonListener());
west.addActionListener(new ButtonListener());
north.addActionListener(new ButtonListener());
centre.addActionListener(new ButtonListener());
setSize(300,200);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
show();
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
((JButton)e.getSource()).setVisible(false);
}
}
public static void main(String[] args)
{
HideButton app = new HideButton();
}
}
|
而且还没有setActionCommand()
|
Button bt = new Button();
bt.setVisible(false);
bt.setVisible(false);
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。