嘿,我有以下代码,其中有 10 个 jbutton。问题是每次我单击按钮时,它都会执行设置自身 setVisible(flase) 的操作,但与此同时,它旁边的组件也会消失。但是,如果我只是将鼠标悬停在技术上仍然可见的组件上,它们就会再次变得可见。例如。如果我编译程序并运行它,程序可以工作,但我必须将鼠标悬停在组件上才能使它们可见(我不希望这样)。一旦组件可见,如果我单击按钮 1,则按钮 1 的操作是设置 one.setVisible(false);。它确实如此,但一旦完成,按钮 2 也会随之消失。但是,如果我将鼠标悬停在 button2 上,它会重新出现在屏幕上,但如果我将鼠标悬停在 button1 上,它就不会再次出现。正如我所做的那样,它消失了。
package dealORnodeal;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
public class Deal extends JFrame implements ActionListener
{
private Container contentPane = getContentPane();
private JButton one = new JButton("1"),two = new JButton("2"),three = new JButton("3"),
four = new JButton("4"),five = new JButton("5"),ones = new JButton("6"),twos = new JButton("7"),
threes = new JButton("8"),fours = new JButton("9"),fives = new JButton("10");
private JTextArea text = new JTextArea(266,103);
private JMenu menu1 = new JMenu("JumpTo");
private JMenuBar bar1 = new JMenuBar();
private ImagePanel bg = new ImagePanel(new ImageIcon("bg.jpg").getImage());
public Deal()
{
paintComponent(getGraphics());
}
public void paintComponent(Graphics g)
{
setTitle("Deal Or No Deal");
setSize(800,850);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
contentPane.add(bg);
JMenuItem item1;
item1 = new JMenuItem("Start Game");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Rules");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Credits");
item1.addActionListener(this);
menu1.add(item1);
item1 = new JMenuItem("GoTo Menu");
item1.addActionListener(this);
menu1.add(item1);
bar1.add(menu1);
setJMenuBar(bar1);
//GAME CODE
text.setBounds(266, 200, 266,103);
text.append("Welcome to The World ");
one.setBounds(25,151,195,55);
one.setBackground(new Color(255,215,0));
one.addActionListener(this);
two.setBounds(25,251,195,55);
two.setBackground(new Color(255,215,0));
two.addActionListener(this);
three.setBounds(25,347,195,55);
three.setBackground(new Color(255,215,0));
three.addActionListener(this);
four.setBounds(25,447,195,55);
four.setBackground(new Color(255,215,0));
four.addActionListener(this);
five.setBounds(25,547,195,55);
five.setBackground(new Color(255,215,0));
five.addActionListener(this);
ones.setBounds(583,151,195,55);
ones.setBackground(new Color(255,215,0));
ones.addActionListener(this);
twos.setBounds(583,251,195,55);
twos.setBackground(new Color(255,215,0));
twos.addActionListener(this);
threes.setBounds(583,347,195,55);
threes.setBackground(new Color(255,215,0));
threes.addActionListener(this);
fours.setBounds(583,447,195,55);
fours.setBackground(new Color(255,215,0));
fours.addActionListener(this);
fives.setBounds(583,547,195,55);
fives.setBackground(new Color(255,215,0));
fives.addActionListener(this);
Container contentPane2 = new Container();
add(one);
add(two);
add(three);
add(four);
add(five);
add(ones);
add(twos);
add(threes);
add(fours);
add(fives);
add(text);
//GAME CODE END
invalidate();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
//Game Boxes Response
if(e.getSource()==one)
{
one.setVisible(false);
}
if(e.getSource()==two)
{
two.setVisible(false);
}
if(e.getSource()==three)
{
three.setVisible(false);
}
if(e.getSource()==four)
{
four.setVisible(false);
}
if(e.getSource()==five)
{
five.setVisible(false);
}
if(e.getSource()==ones)
{
ones.setVisible(false);
}
if(e.getSource()==twos)
{
twos.setVisible(false);
}
if(e.getSource()==threes)
{
threes.setVisible(false);
}
if(e.getSource()==fours)
{
fours.setVisible(false);
}
if(e.getSource()==fives)
{
fives.setVisible(false);
}
}
}