我试图弄清楚如何将JPanel添加到我的主框架中。但是,小组处于不同的级别。本质上,我需要用户按下开始按钮,一旦按下它需要创建一个类的对象(这个类创建一个JPanel)并添加到主框架。我的问题是,一旦我按下开始按钮就没有任何反应。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
public class Display extends JFrame{
private JPanel right,left,center,south;
private JButton start, stop,car1,car2,car3,car4;
private JTextArea text1,text2;
private TitledBorder title1,title2;
private JLabel label,label2,label3;
private RaceDisplay rd;
private Environment env;
public Display() {
super("CAR GAME");
/* ---------------------------------
* BOARD PANELS
-----------------------------------*/
//right panel uses a different layout
right = new JPanel();
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
right.setBackground(Color.GRAY);
//center panel uses default layout
center = new JPanel();
//left panel uses a different layout
left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
left.setBackground(Color.GRAY);
//south panel
south = new JPanel();
south.setBackground(Color.GRAY);
/* ---------------------------------------
* Text area used to diaply the results.
------------------------------------------*/
text1 = new JTextArea();
text2 = new JTextArea();
// ------------>car images to be used in the Car class<------------------
ImageIcon img = new ImageIcon("./images/Car1-small.gif");
ImageIcon img2 = new ImageIcon("./images/car2-small.gif");
ImageIcon img3 = new ImageIcon("./images/car3-small.gif");
ImageIcon img4 = new ImageIcon("./images/car4-small.gif");
ImageIcon imgFlag = new ImageIcon("./images/flag1.png");
ImageIcon imgFlag2 = new ImageIcon("./images/flag2.png");
label2 = new JLabel(imgFlag);
label3 = new JLabel(imgFlag2);
center.add(label3);
label = new JLabel("BEST TEAM EVER RACE GAME");
label.setFont(new Font("Georgia", Font.CENTER_BASELINE, 16));
/* ----------------------------------------------------
* creates the buttons and adds the proper image to them
--------------------------------------------------------*/
car1 = new JButton("BRITISH MOTOR COMPANY",img);
car2=new JButton("FAST AND FURIOUS",img2);
car3=new JButton("SCOOBY GANG",img3);
car4=new JButton("SPEEDY CADDY",img4);
start=new JButton("START");
stop = new JButton("STOP");
/* ----------------------------------------------------
* creates the title border and adds them to panels
--------------------------------------------------------*/
title1 = new TitledBorder("RESULTS");
title2 = new TitledBorder("CHOOSE YOUR RACER!");
//adds the title borders to the Panels.
right.setBorder(title1);
left.setBorder(title2);
/* ----------------------------------------------------
* This TextArea is added to the right Panel and it where
* the result will be displayed
--------------------------------------------------------*/
text1 = new JTextArea(" ",100,30);
right.add(text1);
text1.setLineWrap(true);
/* ----------------------------------------------------
* adds the buttons to the proper panels
--------------------------------------------------------*/
south.add(start);
south.add(stop);
left.add(car1);
left.add(car2);
left.add(car3);
left.add(car4);
left.add(label);
left.add(label2);
/* ----------------------------------------------------
* adds the panels to the main Frame at proper location
--------------------------------------------------------*/
add(right,BorderLayout.EAST);
add(left,BorderLayout.WEST);
add(south,BorderLayout.SOUTH);
add(center,BorderLayout.CENTER);
/* -------------------------------------------------
* Gives actions to the buttons
---------------------------------------------------- */
car1.addActionListener(new Car1Button());
car2.addActionListener(new Car2Button());
car3.addActionListener(new Car3Button());
car4.addActionListener(new Car4Button());
start.addActionListener(new Start());
/* ----------------------------------------------------
* sets up the main frame's components
--------------------------------------------------------*/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1900,700);
setVisible(true);
}//end of constructor
/**
*
*/
private class Start implements ActionListener{
public void actionPerformed(ActionEvent event){
rd = new RaceDisplay();
add(rd);
}
}
这是创建面板的另一个类。
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class RaceDisplay extends JPanel implements ActionListener{
private Image img1,img2,img3,img4;
private int velX;
private int x;
private Timer tm;
private Environment env;
private Car car;
public RaceDisplay(){
tm = new Timer(30,this);
x=0;
//velX=car.getSpeed();
velX=x;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
ImageIcon car1 = new ImageIcon("./images/Car1-small.gif");
ImageIcon car2 = new ImageIcon("./images/car2-small.gif");
ImageIcon car3 = new ImageIcon("./images/car3-small.gif");
ImageIcon car4 = new ImageIcon("./images/car4-small.gif");
img1 = car1.getImage();
img2 = car2.getImage();
img3= car3.getImage();
img4= car4.getImage();
g.drawImage(img1,x,100,null);
g.drawImage(img2,x,200,null);
g.drawImage(img3,x,300,null);
g.drawImage(img4,x,400,null);
tm.start();
}
//method runs the images from left to right
public void actionPerformed(ActionEvent e) {
x = x+velX;
if(x>=600){
x=0;
x=x+velX;
// // this.wait();
// } catch (InterruptedException ex) {
// Logger.getLogger(RaceDisplay.class.getName()).log(Level.SEVERE, null, ex);
// }
repaint();
}
repaint();
}
public int getX(){
return x;
}
}
答案 0 :(得分:1)
将组件添加到可见GUI时,基本代码为:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint();
答案 1 :(得分:0)
你到底想要什么?我的意思是你想要添加的汽车开始运行???对于那种情况,你需要写一个线程。你的开始按钮工作得很好。