程序如下...jpanel加到JFrame中不能显示...jpanel的布局是null,,,去掉jpanle的null布启就可以显示...这是为什么,,但是我想让panel的布局是null形式的 import java.awt.*; import java.awt.event.*; import javax.comm.*;import javax.swing.*; import javax.swing.border.*; public class d extends JFrame{
JPanel p = new JPanel(); //存放工具设置的
private JButton b1 = new JButton("Setup"); private JButton b2 = new JButton("Close"); public JLabel l1 = new JLabel("lcd"); private String marstr="not connect,please Setup";
public int x=0; public int y=44; Graphics gg; public Timer tme1; public Timer tme2; public int speed = 10; public int screenw = 340; public int screenh = 90;
public int screensize = 2;
public d(){ Container con = this.getContentPane(); con.setLayout(new BorderLayout()); p.setBackground(Color.darkGray); p.setLayout(null);
panel上面没有东西,是看不出效果的
add(panel);
我想了下 ,你的错误有两个地方:1,没有把frame设置可见 2,如果第一个没错 那就可能是没有把panel添加到frame中。
前提是你代码没有写错!
其实 不显示也就和这两个地方有关。
给分
import java.awt.*;
import java.awt.event.*;
import javax.comm.*;import javax.swing.*;
import javax.swing.border.*;
public class d extends JFrame{
JPanel p = new JPanel(); //存放工具设置的
private JButton b1 = new JButton("Setup");
private JButton b2 = new JButton("Close");
public JLabel l1 = new JLabel("lcd");
private String marstr="not connect,please Setup";
public int x=0;
public int y=44;
Graphics gg;
public Timer tme1;
public Timer tme2;
public int speed = 10;
public int screenw = 340;
public int screenh = 90;
public int screensize = 2;
public d(){
Container con = this.getContentPane(); con.setLayout(new BorderLayout()); p.setBackground(Color.darkGray);
p.setLayout(null);
b1.setBounds(340, 0, 60, 40);
b1.setBackground(Color.BLACK);
b1.setForeground(Color.WHITE);
b1.setBorder(null);
b2.setBounds(280, 0, 60, 40);
b2.setBackground(Color.BLACK);
b2.setForeground(Color.WHITE);
b2.setBorder(null);
l1.setBounds(200, 0, 90, 60);
l1.setBackground(Color.BLACK);
l1.setForeground(Color.WHITE);
l1.setBorder(null);
p.add(b1);
p.add(b2);
con.add(p,BorderLayout.SOUTH);
con.add(l1,BorderLayout.EAST);
con.setBackground(Color.WHITE);
this.setSize(screenw,screenh);
this.setTitle("LCD not connect!!");
this.setResizable(false);
this.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.RED);
g.drawString("sdf", x,y);
} public static void main(String args[]){
d lc= new d();
}
}