求大神帮我看看为啥jpanel在jframe里显示不出来。。。。
importjava.awt.Color;importjava.awt.Graphics;importjava.awt.GridLayout;importjava.awt...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Myshape{
private int x=0;
private int y=0;
private int w=10;
private int h=10;
JFrame f=new JFrame();
Myline l;
JTextField xf,yf,wf,hf;
public Myshape(){
f.getContentPane();
f.setLayout(null);
f.setTitle("绘图");
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l=new Myline(x,y,w,h);
f.add(l);
l.repaint();
JPanel p=new JPanel();
p.setLayout(new GridLayout(4,1));
p.setLocation(200,0);
p.setBackground(Color.YELLOW);
xf=new JTextField(10);
p.add(xf);
yf=new JTextField(10);
p.add(yf);
wf=new JTextField(10);
p.add(wf);
hf=new JTextField(10);
p.add(hf);
JButton b=new JButton("画直线");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
l.x=Integer.parseInt(xf.getText());
l.y=Integer.parseInt(yf.getText());
l.w=Integer.parseInt(wf.getText());
l.h=Integer.parseInt(hf.getText());
xf.setText("");
yf.setText("");
wf.setText("");
hf.setText("");
l.repaint();
f.setVisible(true);
}
}
);
b.setLocation(300, 0);
f.add(b);
f.add(p);
f.setVisible(true);
}
public static void main(String args[]){
new Myshape();
}
}
class Myline extends JPanel{
int x;
int y;
int w;
int h;
public Myline(int a,int b,int c,int d){
super();
x =a;y=b;w=c;h=d;
}
public void paintComponent(Graphics g){
g.setColor(Color.red);
g.drawLine(x, y, w, h);
}
} 展开
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Myshape{
private int x=0;
private int y=0;
private int w=10;
private int h=10;
JFrame f=new JFrame();
Myline l;
JTextField xf,yf,wf,hf;
public Myshape(){
f.getContentPane();
f.setLayout(null);
f.setTitle("绘图");
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l=new Myline(x,y,w,h);
f.add(l);
l.repaint();
JPanel p=new JPanel();
p.setLayout(new GridLayout(4,1));
p.setLocation(200,0);
p.setBackground(Color.YELLOW);
xf=new JTextField(10);
p.add(xf);
yf=new JTextField(10);
p.add(yf);
wf=new JTextField(10);
p.add(wf);
hf=new JTextField(10);
p.add(hf);
JButton b=new JButton("画直线");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
l.x=Integer.parseInt(xf.getText());
l.y=Integer.parseInt(yf.getText());
l.w=Integer.parseInt(wf.getText());
l.h=Integer.parseInt(hf.getText());
xf.setText("");
yf.setText("");
wf.setText("");
hf.setText("");
l.repaint();
f.setVisible(true);
}
}
);
b.setLocation(300, 0);
f.add(b);
f.add(p);
f.setVisible(true);
}
public static void main(String args[]){
new Myshape();
}
}
class Myline extends JPanel{
int x;
int y;
int w;
int h;
public Myline(int a,int b,int c,int d){
super();
x =a;y=b;w=c;h=d;
}
public void paintComponent(Graphics g){
g.setColor(Color.red);
g.drawLine(x, y, w, h);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询