嘿,我有以下作为我的代码,其中我有10个jbuttons。问题是每次我点击按钮它都会执行自己设置setVisible(flase)的动作,但是同时它旁边的组件也会消失。但是,如果我只是悬停在技术上仍然可见的组件上,它们都会再次变得可见。例如。如果我编译程序并运行它,该程序可以工作,但我必须将鼠标悬停在组件上以使它们可见(我不希望这样)。一旦组件可见,如果我单击按钮1,则按钮1的actionperformed是设置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);
}
}
}
答案 0 :(得分:5)
这paintComponent(getGraphics());
不是绘画的方式。组件也不是如何添加到容器中的。
您永远不能依赖getGraphics
来返回有效的图形上下文。摆脱paintComponent
方法,只需从构造函数构建UI。
(就个人而言),这个private Container contentPane = getContentPane();
并不是一个好主意。如果其他开发人员调用setContentPane
会怎样?
null
布局管理员因为使用你的UI而臭名昭着,你最好更好地使用一个或多个布局管理器......个人......
否则,我似乎无法复制你的问题......