[英]Swing GUI Rendering slow?
嘿,我的代碼如下,其中有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);
}
}
}
此paintComponent(getGraphics());
不是繪畫的完成方式。 同樣也不是將組件添加到容器的方式。
您永遠不能依靠getGraphics
返回有效的圖形上下文。 擺脫paintComponent
方法,只需從構造函數構造UI。
(個人),此private Container contentPane = getContentPane();
這不是一個好主意。 如果其他開發人員調用setContentPane
會發生什么?
null
布局管理器因與您的UI緊密相關而臭名昭著,您最好使用一個或多個布局管理器...親自...
否則,我似乎無法復制您的問題...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.