java gui编程,jpanel在添加到主界面后无法显示
代码如下,在没有布局的时候能实现packagecar;importjavax.swing.*;importjavax.swing.border.Border;import...
代码如下,在没有布局的时候能实现
package car;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Car extends JFrame {
public static void main(String[] args) {
Car frame = new Car();
frame.setTitle("car");
frame.setSize(700,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
int x1,x2,x3,x4;
public Car()
{
JLabel jl1=new JLabel("car 1");
JLabel jl2=new JLabel("car 2");
JLabel jl3=new JLabel("car 3");
JLabel jl4=new JLabel("car 4");
JTextField tf1=new JTextField(2);
JTextField tf2=new JTextField(2);
JTextField tf3=new JTextField(2);
JTextField tf4=new JTextField(2);
MakeCar car1=new MakeCar();
MakeCar car2=new MakeCar();
MakeCar car3=new MakeCar();
MakeCar car4=new MakeCar();
setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setBounds(0, 0, 300, 20);
p1.setLayout(new GridLayout(1,8));
p1.add(jl1);
p1.add(tf1);
p1.add(jl2);
p1.add(tf2);
p1.add(jl3);
p1.add(tf3);
p1.add(jl4);
p1.add(tf4);
add(p1,BorderLayout.NORTH);
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(1,4));
p2.add(car1);
p2.add(car2);
p2.add(car3);
p2.add(car4);
add(p2,BorderLayout.CENTER);
// car1.setBounds(0, 100, 200, 100);
//car2.setBounds(0,120, 200, 100);
//car3.setBounds(0, 250,200, 100);
//car4.setBounds(0,350, 200, 100);
//add(p1);
//inputListener ipListener=new inputListener();
//tf4.addActionListener(ipListener);
}
static public class MakeCar extends JPanel {
private int y = getHeight()-50;
private int x = 50;
public MakeCar() {
Timer timer = new Timer(50,new TimerListener());
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Polygon trapezia = new Polygon(); // trapezia 梯形
trapezia.addPoint(x + 10, y -20);
trapezia.addPoint(x + 40, y -20);
trapezia.addPoint(x + 30, y -30);
trapezia.addPoint(x + 20, y -30);
g.setColor(Color.BLACK);
g.fillOval(x + 30, y - 10, 10, 10);
g.fillOval(x + 10, y - 10, 10, 10);
g.setColor(Color.gray);
g.fillRect(x, y - 20, 50, 10);
g.setColor(Color.red);
g.fillPolygon(trapezia);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//if(x >250)
//x = 50;
x = x + 10;
repaint();
}
}
}
//class inputListener implements ActionListener{
// public void actionPerformed(ActionEvent e)
// {
// x1=Integer.parseInt(tf1.getText());
// x2=Integer.parseInt(tf2.getText());
// x3=Integer.parseInt(tf3.getText());
// x4=Integer.parseInt(tf4.getText());
//
// }
// }
} 展开
package car;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
public class Car extends JFrame {
public static void main(String[] args) {
Car frame = new Car();
frame.setTitle("car");
frame.setSize(700,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
int x1,x2,x3,x4;
public Car()
{
JLabel jl1=new JLabel("car 1");
JLabel jl2=new JLabel("car 2");
JLabel jl3=new JLabel("car 3");
JLabel jl4=new JLabel("car 4");
JTextField tf1=new JTextField(2);
JTextField tf2=new JTextField(2);
JTextField tf3=new JTextField(2);
JTextField tf4=new JTextField(2);
MakeCar car1=new MakeCar();
MakeCar car2=new MakeCar();
MakeCar car3=new MakeCar();
MakeCar car4=new MakeCar();
setLayout(new BorderLayout());
JPanel p1=new JPanel();
p1.setBounds(0, 0, 300, 20);
p1.setLayout(new GridLayout(1,8));
p1.add(jl1);
p1.add(tf1);
p1.add(jl2);
p1.add(tf2);
p1.add(jl3);
p1.add(tf3);
p1.add(jl4);
p1.add(tf4);
add(p1,BorderLayout.NORTH);
JPanel p2=new JPanel();
p2.setLayout(new GridLayout(1,4));
p2.add(car1);
p2.add(car2);
p2.add(car3);
p2.add(car4);
add(p2,BorderLayout.CENTER);
// car1.setBounds(0, 100, 200, 100);
//car2.setBounds(0,120, 200, 100);
//car3.setBounds(0, 250,200, 100);
//car4.setBounds(0,350, 200, 100);
//add(p1);
//inputListener ipListener=new inputListener();
//tf4.addActionListener(ipListener);
}
static public class MakeCar extends JPanel {
private int y = getHeight()-50;
private int x = 50;
public MakeCar() {
Timer timer = new Timer(50,new TimerListener());
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Polygon trapezia = new Polygon(); // trapezia 梯形
trapezia.addPoint(x + 10, y -20);
trapezia.addPoint(x + 40, y -20);
trapezia.addPoint(x + 30, y -30);
trapezia.addPoint(x + 20, y -30);
g.setColor(Color.BLACK);
g.fillOval(x + 30, y - 10, 10, 10);
g.fillOval(x + 10, y - 10, 10, 10);
g.setColor(Color.gray);
g.fillRect(x, y - 20, 50, 10);
g.setColor(Color.red);
g.fillPolygon(trapezia);
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//if(x >250)
//x = 50;
x = x + 10;
repaint();
}
}
}
//class inputListener implements ActionListener{
// public void actionPerformed(ActionEvent e)
// {
// x1=Integer.parseInt(tf1.getText());
// x2=Integer.parseInt(tf2.getText());
// x3=Integer.parseInt(tf3.getText());
// x4=Integer.parseInt(tf4.getText());
//
// }
// }
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询