2013-01-14 109 views
-1

嘿,我有以下作为我的代码,其中我有10个jbuttons。问题是,每次我点击按钮,它都会执行setvisible(flase)设置的动作,但同时它旁边的组件也会消失。但是,如果我只是将鼠标悬停在技术上可见的组件上,它们都会再次可见。例如。如果我编译程序并运行它,程序就可以工作,但我必须将鼠标悬停在组件上才能看到它(我不希望这样)。一旦组件可见,如果我点击按钮1,按钮1的actionperformed是设置one.setVisible(false);.它的作用是什么,但一旦完成,按钮2也随之消失。但是,如果我将鼠标悬停在按钮2上,它会重新显示在屏幕上,但如果我将鼠标悬停在按钮1上,则不会再次出现。因为我已经让它消失了。Swing GUI渲染慢?

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); 
    } 
} 
} 
+5

您不要在paintComponent方法中进行布局设置。当你创建Jframe时,即在你的构造函数中执行它。 – BevynQ

+1

@BevynQ我很确定你知道'JFrame'没有'paintComponent'方法 - 但你仍然是对的 - 它吓坏了我:P – MadProgrammer

+1

你不应该调用'paintComponent()' 。操作系统将根据需要调用它。只有在需要进行自定义绘画时才会覆盖它。就像现在一样,每当操作系统要求重新绘制自己(可能每秒多次)时,您都会重新创建整个GUI。 –

回答

5

paintComponent(getGraphics());是不是如何绘画完成。这也不是如何将组件添加到容器。

永远不能依赖getGraphics来返回有效的图形上下文。摆脱paintComponent方法,并简单地从构造函数构造UI。

(个人而言),这个private Container contentPane = getContentPane();不是一个好主意。其他开发者请求setContentPane会发生什么情况?

null布局管理器是臭名昭著的与你拧UI,你最好更好地使用一个或多个布局管理器...亲自....

否则,我似乎无法复制您的问题...

+0

对不起,但我有一个Background Image类,如果调用它,它会设置背景。所以我只是取代了构造函数方法中的所有代码,然后添加(bg);在setVisible之前(true);这一切都很完美,现在感谢所有帮助过我的人。 /关闭 –

+0

@DharavPatel:你应该[接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)并回答关闭它。 –