我有一个JFrame,一个主要的JPanel,只有JFrame的左半部分,第二个JPanel。 当我在主JPanel中单击JButton时,第二个JPanel变为可见。
我要在主JPanel类中创建一个paintComponent,绘制一个immage,一个圆圈,然后全部工作。
当我改变这个圆圈的颜色时会出现问题。我每次重绘()时都会改变R-G-B; 圆的颜色改变了,但是如果我点击主JPanel中的JButton来打开第二个JPanel我会看到它一段时间它变得不可见。
我想因为它重新绘制了第二个主要JPanel ..
我如何解决这个问题?谢谢你!。
P.S。 我用super.paintComponent(g); 在JPanel的costructor中,我执行this.repaint()立即调出第一页的组件,否则我应该最小化并重新出现窗口。
//Panel extends JPanel
//at the end of Constructor
this.repaint();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
setOpaque(true);
g.drawImage(img, 210, 50, 580,580,this);
g.setColor(Color.black);
g.drawOval(480,40,40,40);
g.fillOval(480,40,40,40); //Various..
repaint(); //With this i can't saw the second JPanel that it's overlapped
} //whit the main JPanel
Alessandro Amedei,佛罗伦萨。
27-01-2015 这是我的代码..
MAIN PANEL
package a;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
// @author Alessandro Amedei
public class FirstPanel extends JPanel{
Image img = null;
FirstPanel(){
LotL LotL = new LotL();
FPListener FPL = new FPListener();
this.setVisible(true);
this.setSize(1000,1000);
this.setLayout(null);
this.setBackground(Color.white);
this.addMouseListener(FPL);
Main.f1.add(this);
Button start = new Button("Start Game!");
Button options = new Button("Options");
start.setVisible(true);
start.setSize(200,80);
start.setLayout(null);
start.setLocation(400,650);
start.addActionListener(LotL);
start.setBackground(Color.green);
start.identificatore=1;
this.add(start);
options.setVisible(true);
options.setSize(200,70);
options.setLayout(null);
options.setLocation(400,750);
options.addActionListener(LotL);
options.setBackground(Color.green);
options.identificatore=2;
this.add(options);
img= Toolkit.getDefaultToolkit().getImage("ok.jpg");
this.repaint();
}
int B=30;
int R=80;
int G=90;
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
setOpaque(true);
g.drawImage(img, 210, 65, 580,580,this);
g.setColor(new Color(R,G,B));
g.drawOval(455,20,90,90);
g.fillOval(455,20,90,90);
if(B==150)
B=0;
if(R==170)
R=0;
if(G==160)
G=0;
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
}
B++;
R++;
G++;
repaint();
}
}
第二小组
package a;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
//@author Alessandro Amedei
public class OptionsPanel extends JPanel {
static JSlider rgb1 = new JSlider(0,255);
static JSlider rgb2 = new JSlider(0,255);
static JSlider rgb3 = new JSlider(0,255);
static SpinnerNumberModel noba = new SpinnerNumberModel();
static JTextField fg1 = new JTextField();
static JTextField fg2 = new JTextField();
OptionsPanel(){
this.setVisible(false);
this.setSize(350,1000);
this.setLayout(null);
this.setBackground(new Color(255,150,0));
Main.f1.add(this);
JSpinner nob = new JSpinner(noba);
JLabel l1= new JLabel("Options");
JLabel l2= new JLabel("Background Color");
JLabel g1= new JLabel("Left Button Color");
JLabel g2= new JLabel("Right Button Color");
JLabel nob1 = new JLabel("Number of Checkers for Player");
JLabel ng1= new JLabel("Name of Left Player");
JLabel ng2= new JLabel("Name of Right Player");
rgb1.setVisible(true);
rgb1.setSize(240,40);
rgb1.setLocation(55,90);
this.add(rgb1);
rgb1.setBackground(new Color(rgb1.getValue(),255,0));
rgb1.addChangeListener(new SListener());
rgb2.setVisible(true);
rgb2.setSize(240,40);
rgb2.setLocation(55,180);
this.add(rgb2);
rgb2.setBackground(new Color(255,0,rgb1.getValue()));
rgb2.addChangeListener(new SListener());
rgb3.setVisible(true);
rgb3.setSize(240,40);
rgb3.setLocation(55,270);
this.add(rgb3);
rgb3.setBackground(new Color(0,rgb1.getValue(),255));
rgb3.addChangeListener(new SListener());
nob.setVisible(true);
nob.setSize(60,30);
nob.setLocation(55,360);
noba.setMaximum(10);
noba.setMinimum(3);
noba.setValue(3);
this.add(nob);
fg1.setVisible(true);
fg1.setSize(150,30);
fg1.setLocation(55,450);
this.add(fg1);
fg2.setVisible(true);
fg2.setSize(150,30);
fg2.setLocation(55,540);
this.add(fg2);
Font f= new Font("arial",Font.ITALIC,20);
Font f2= new Font("arial",Font.BOLD,13);
l1.setVisible(true); //Etichetta OPTIONS
l1.setSize(70,20);
l1.setLocation(140,10);
l1.setFont(f);
this.add(l1);
l2.setVisible(true);
l2.setSize(120,30);
l2.setLocation(55,65);
l2.setFont(f2);
this.add(l2);
g1.setVisible(true);
g1.setSize(150,30);
g1.setLocation(55,155);
g1.setFont(f2);
this.add(g1);
g2.setVisible(true);
g2.setSize(150,30);
g2.setLocation(55,245);
g2.setFont(f2);
this.add(g2);
nob1.setVisible(true);
nob1.setSize(200,30);
nob1.setLocation(55,335);
nob1.setFont(f2);
this.add(nob1);
ng1.setVisible(true);
ng1.setSize(200,30);
ng1.setLocation(55,425);
ng1.setFont(f2);
this.add(ng1);
ng2.setVisible(true);
ng2.setSize(200,30);
ng2.setLocation(55,515);
ng2.setFont(f2);
this.add(ng2);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
好的,如果我使用repaint()更改主JPanel中圆圈的颜色;由于主JPanel的repaint()方法引起的刷新,我无法显示选项面板。 如何在主JPanel中更改圆圈的颜色并同时显示选项面板? (不包括创建另一个我绘制圆圈的面板)。 对不起,我的英语不好! 非常感谢你。 Alessandro Amedei,佛罗伦萨。